企业微信客户联系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在分布式环境下既可安全重试,又避免重复副作用,满足金融级数据一致性要求。

相关推荐
李白的天不白12 小时前
docker ps
java
NE_STOP12 小时前
Docker--Docker Swarm集群
java
两年半的个人练习生^_^12 小时前
JMM 进阶:彻底理解 CAS 实现原理
java·开发语言
wuminyu12 小时前
Java锁机制之park和unpark源码剖析
java·linux·c语言·jvm·c++
梦想的旅途213 小时前
企业微信API实现外部群消息异步推送的技术架构与实践
mysql·架构·企业微信
W_LuYi18513 小时前
手撸极简zkEVM验证器:RISC-V电路实践
java·risc-v
AI人工智能+电脑小能手13 小时前
【大白话说Java面试题 第102题】【并发篇】第2题:volatile 能否保证线程安全?
java·安全·面试
KobeSacre13 小时前
JUC 概述
java·开发语言
小bo波13 小时前
形式化方法 × UML
java·软件工程·uml·面向对象·形式化方法·tla+
就叫_这个吧14 小时前
IDEA中Javaweb项目创建+servlet,实现简单的信息录入获取
java·servlet·intellij-idea·web