企业微信客户联系API在Java微服务中的幂等性设计与重试机制

企业微信客户联系API在Java微服务中的幂等性设计与重试机制

1. 幂等性问题背景

企业微信客户联系API(如添加客户、发送欢迎语、打标签)在网络不稳定或服务超时场景下,可能因客户端重试导致重复操作。例如多次调用 externalcontact/add_contact_way 会生成多个相同配置的渠道活码。为保障数据一致性,必须在微服务层实现请求幂等安全重试

2. 幂等键(Idempotency Key)机制

客户端在发起请求时携带唯一幂等键(如 UUID),服务端以此作为去重依据:

java 复制代码
package wlkankan.cn.model;

public class AddContactWayRequest {
    private String idempotencyKey; // 必填,由调用方生成
    private String configName;
    private Integer type;
    private Integer scene;
    private String remark;
    // getters/setters
}

3. 幂等记录存储结构

使用数据库表记录已处理的幂等键及其结果:

sql 复制代码
CREATE TABLE idempotency_record (
    idempotency_key VARCHAR(64) PRIMARY KEY,
    business_type VARCHAR(50) NOT NULL, -- 如 "ADD_CONTACT_WAY"
    request_payload JSON NOT NULL,
    response_payload JSON,
    status TINYINT NOT NULL, -- 0: processing, 1: success, 2: failed
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
    expire_time DATETIME NOT NULL
);

4. 幂等拦截器实现

在 Spring AOP 中统一处理幂等逻辑:

java 复制代码
package wlkankan.cn.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import wlkankan.cn.idempotent.IdempotencyService;
import wlkankan.cn.model.ApiResponse;

@Aspect
@Component
public class IdempotencyAspect {

    private final IdempotencyService idempotencyService;

    public IdempotencyAspect(IdempotencyService service) {
        this.idempotencyService = service;
    }

    @Around("@annotation(wlkankan.cn.annotation.Idempotent) && args(request)")
    public Object handleIdempotent(ProceedingJoinPoint joinPoint, Object request) throws Throwable {
        String key = extractIdempotencyKey(request);
        String type = resolveBusinessType(joinPoint);

        var existing = idempotencyService.getRecord(key, type);
        if (existing != null) {
            if (existing.getStatus() == 1) {
                return ApiResponse.success(existing.getResponsePayload());
            } else if (existing.getStatus() == 0) {
                throw new IllegalStateException("请求正在处理中,请勿重复提交");
            }
        }

        // 标记为处理中
        idempotencyService.markProcessing(key, type, request);

        try {
            Object result = joinPoint.proceed();
            idempotencyService.markSuccess(key, type, result);
            return result;
        } catch (Exception e) {
            idempotencyService.markFailed(key, type, e.getMessage());
            throw e;
        }
    }

    private String extractIdempotencyKey(Object req) {
        // 通过反射获取 idempotencyKey 字段
        try {
            var field = req.getClass().getDeclaredField("idempotencyKey");
            field.setAccessible(true);
            return (String) field.get(req);
        } catch (Exception ex) {
            throw new IllegalArgumentException("缺少幂等键");
        }
    }

    private String resolveBusinessType(ProceedingJoinPoint jp) {
        return jp.getSignature().getName(); // 或自定义注解属性
    }
}

5. 幂等服务核心逻辑

java 复制代码
package wlkankan.cn.idempotent;

import com.fasterxml.jackson.databind.ObjectMapper;
import wlkankan.cn.dao.IdempotencyRecordDao;
import wlkankan.cn.model.IdempotencyRecord;

public class IdempotencyService {
    private final IdempotencyRecordDao dao;
    private final ObjectMapper jsonMapper;

    public IdempotencyService(IdempotencyRecordDao dao, ObjectMapper mapper) {
        this.dao = dao;
        this.jsonMapper = mapper;
    }

    public IdempotencyRecord getRecord(String key, String type) {
        return dao.selectByKeyAndType(key, type);
    }

    public void markProcessing(String key, String type, Object request) {
        IdempotencyRecord record = new IdempotencyRecord();
        record.setIdempotencyKey(key);
        record.setBusinessType(type);
        record.setRequestPayload(jsonMapper.valueToTree(request));
        record.setStatus((byte) 0);
        record.setExpireTime(System.currentTimeMillis() + 24 * 3600_000); // 24小时过期
        dao.insert(record);
    }

    public void markSuccess(String key, String type, Object response) {
        dao.updateStatusAndResponse(key, type, (byte) 1, jsonMapper.valueToTree(response));
    }

    public void markFailed(String key, String type, String errorMsg) {
        dao.updateStatusAndResponse(key, type, (byte) 2, jsonMapper.createObjectNode().put("error", errorMsg));
    }
}

6. 企业微信API调用与重试封装

使用 Resilience4j 实现指数退避重试,但仅对非幂等失败重试:

java 复制代码
package wlkankan.cn.client;

import io.github.resilience4j.retry.Retry;
import io.github.resilience4j.retry.RetryConfig;
import wlkankan.cn.model.AddContactWayRequest;
import wlkankan.cn.model.WeComResponse;

public class WeComClient {
    private final Retry retry;

    public WeComClient() {
        RetryConfig config = RetryConfig.custom()
            .maxAttempts(3)
            .waitDuration(java.time.Duration.ofMillis(500))
            .retryOnResult(response -> {
                WeComResponse resp = (WeComResponse) response;
                // 仅当返回临时错误(如网络超时、限流)时重试
                return resp.getErrcode() == -1 || resp.getErrcode() == 40001 || resp.getErrcode() == 429;
            })
            .build();
        this.retry = Retry.of("wecom-api", config);
    }

    public WeComResponse addContactWay(AddContactWayRequest req) {
        return Retry.decorateCallable(retry, () -> doHttpCall(req)).call();
    }

    private WeComResponse doHttpCall(AddContactWayRequest req) {
        // 实际HTTP POST到 https://qyapi.weixin.qq.com/cgi-bin/...
        return HttpClient.post("/externalcontact/add_contact_way", req);
    }
}

7. 业务方法标注幂等

java 复制代码
package wlkankan.cn.service;

import wlkankan.cn.annotation.Idempotent;
import wlkankan.cn.model.AddContactWayRequest;
import wlkankan.cn.model.ApiResponse;

@Service
public class CustomerContactService {

    private final WeComClient weComClient;

    public CustomerContactService(WeComClient client) {
        this.weComClient = client;
    }

    @Idempotent
    public ApiResponse createContactWay(AddContactWayRequest request) {
        var resp = weComClient.addContactWay(request);
        if (resp.getErrcode() != 0) {
            throw new RuntimeException("企业微信API错误: " + resp.getErrmsg());
        }
        return ApiResponse.success(resp.getConfigId());
    }
}

8. 幂等键生成规范

客户端应确保同一业务意图使用相同幂等键:

java 复制代码
// 示例:前端生成
String idempotencyKey = "ADD_CW_" + userId + "_" + System.currentTimeMillis();

或由网关统一分配:

java 复制代码
// API Gateway 拦截器
if (request.getHeader("Idempotency-Key") == null) {
    response.setHeader("Idempotency-Key", UUID.randomUUID().toString());
}

该方案通过数据库级幂等记录与智能重试策略,确保企业微信客户联系API在分布式环境下既可安全重试,又避免重复副作用,满足金融级数据一致性要求。

相关推荐
毕设源码-朱学姐1 小时前
【开题答辩全过程】以 基于JavaWeb的网上家具商城设计与实现为例,包含答辩的问题和答案
java
C雨后彩虹2 小时前
CAS与其他并发方案的对比及面试常见问题
java·面试·cas·同步·异步·
java1234_小锋4 小时前
Java高频面试题:SpringBoot为什么要禁止循环依赖?
java·开发语言·面试
2501_944525544 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 账户详情页面
android·java·开发语言·前端·javascript·flutter
计算机学姐4 小时前
基于SpringBoot的电影点评交流平台【协同过滤推荐算法+数据可视化统计】
java·vue.js·spring boot·spring·信息可视化·echarts·推荐算法
Filotimo_4 小时前
Tomcat的概念
java·tomcat
索荣荣5 小时前
Java Session 全面指南:原理、应用与实践(含 Spring Boot 实战)
java·spring boot·后端
Amumu121385 小时前
Vue Router(二)
java·前端
念越5 小时前
数据结构:栈堆
java·开发语言·数据结构