在java 项目 springboot3.3 中 调用第三方接口(乙方),如何做到幂等操作(调用方为甲方,被调用方为乙方)? 以及啥是幂等操作?
什么是幂等操作?
幂等性(Idempotence) 是指一个操作无论执行一次还是多次,对系统状态产生的影响都是相同的。在分布式系统中,由于网络不稳定、超时重试等因素,接口可能被重复调用,幂等设计能确保重复请求不会导致意外结果。
以扣款通知为例:
- 场景:用户支付成功后,甲方(电商平台)调用乙方(支付公司)的扣款通知接口。
- 幂等要求:
✅ 用户支付100元,无论通知发送1次还是N次,乙方只扣款一次。
❌ 若乙方未做幂等处理,可能导致重复扣款。
甲方(Spring Boot 调用方)幂等实现方案
甲方需确保:即使重试多次,乙方接口只处理一次业务。核心思路是通过唯一标识(如订单号)控制请求。
1. 生成唯一幂等键(Idempotency Key)
- 为每个业务请求生成全局唯一ID(如
UUID
或业务ID+时间戳
),并在调用乙方时传递该值。 - 示例格式:
String idempotencyKey = "ORDER_20240604120000_123456";
2. 在请求头/体中传递幂等键
- 乙方接口需支持接收幂等键(通常放在HTTP Header):
HttpHeaders headers = new HttpHeaders(); headers.add("Idempotency-Key", idempotencyKey); // 行业通用做法
3. 甲方本地记录请求状态
- 在数据库中存储每次请求的幂等键和状态:
id order_id idempotency_key status response 1 1001 KEY_ABC123 SUCCESS {…} - 调用前检查:若该幂等键已成功,则直接返回历史结果,不再调用乙方。
4. 重试机制结合幂等键
- 使用Spring Retry在失败时自动重试(保持相同幂等键):
import org.springframework.retry.annotation.Retryable; import org.springframework.retry.annotation.Backoff;@Service public class PaymentService {@Retryable(maxAttempts = 3, backoff = @Backoff(delay = 1000))public void notifyPayment(String orderId, String idempotencyKey) {// 调用乙方接口(携带幂等键)} }
5. 完整代码示例(Spring Boot 3.3)
@Service
public class PaymentService {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate PaymentRecordRepository repository; // 数据库访问层@Retryable(maxAttempts = 3, backoff = @Backoff(delay = 2000))@Transactionalpublic void notifyPaymentSuccess(String orderId) {// 1. 生成或获取幂等键(如从数据库读取)String idempotencyKey = generateIdempotencyKey(orderId);// 2. 检查是否已处理过PaymentRecord record = repository.findByIdempotencyKey(idempotencyKey);if (record != null && "SUCCESS".equals(record.getStatus())) {return; // 已成功,直接返回}// 3. 调用乙方接口HttpHeaders headers = new HttpHeaders();headers.add("Idempotency-Key", idempotencyKey);Map<String, String> body = Map.of("orderId", orderId, "amount", "100.00");HttpEntity<Map<String, String>> request = new HttpEntity<>(body, headers);try {ResponseEntity<String> response = restTemplate.postForEntity("https://third-party.com/api/debit", request, String.class);// 4. 保存请求结果savePaymentRecord(orderId, idempotencyKey, "SUCCESS", response.getBody());} catch (Exception e) {savePaymentRecord(orderId, idempotencyKey, "FAILED", e.getMessage());throw e; // 触发重试}}private String generateIdempotencyKey(String orderId) {// 实际业务中可组合: 业务前缀 + 订单ID + 操作类型return "PAY_" + orderId + "_DEBIT";}private void savePaymentRecord(String orderId, String key, String status, String response) {PaymentRecord record = new PaymentRecord();record.setOrderId(orderId);record.setIdempotencyKey(key);record.setStatus(status);record.setResponse(response);repository.save(record);}
}
乙方(第三方)的幂等责任
甲方依赖乙方的接口实现幂等性,乙方需:
- 接收幂等键:通过Header或Body获取甲方传递的
Idempotency-Key
。 - 存储请求状态:在自身系统记录该键对应的处理结果。
- 拒绝重复请求:
- 若相同幂等键的请求已成功,直接返回历史结果。
- 若相同幂等键的请求在处理中,返回
409 Conflict
。
关键设计原则
措施 | 目的 |
---|---|
唯一幂等键 | 全局标识请求,避免业务参数冲突(如订单号可能重复提交不同操作) |
前置状态检查 | 调用乙方前本地校验,减少无效请求 |
重试+相同幂等键 | 确保重试时乙方识别为同一请求 |
乙方幂等支持 | 甲方方案生效的前提,需与乙方约定接口规范 |
💡 注意事项:
- 幂等键需在业务维度唯一(例如退款和支付使用不同前缀)
- HTTP方法选择:POST是非幂等的,但通过幂等键可使其具备幂等性
- 网络超时场景:甲方可能未收到响应,但乙方已处理,因此需依赖乙方返回结果更新状态