对于做集成的公司来说,会集成各种第三方系统,要么是通过第三方系统的api,要么直接集成第三方系统的设备。如果是通过api集成,单次请求只调用一个三方系统没问题,同步调用就行,但如果同时要调用多个三方系统,并且需要三方系统都成功的时候才算该次请求成功调用,这种情况只要后面调用的系统发生报错,前面系统如果不删除产生的数据,就会遗留在三方系统中,产生脏数据。这种集成的三方系统,不是我方能够控制的,我们不能修改他们的代码。
下图是产生该问题的一个场景:
这里我尝试使用了TCC来处理多个三方系统的数据一致问题,以添加人脸为例来说明:
经过以上步骤以此来完成脏数据的删除。
TCC我们要考虑以下的几种情况:
cancel阶段:根据我方功能主键ID,操作类型(ADD)查询出所有记录,再循环调用三方删除接口
这是使用TCC来解决的多个三方数据一致性问题,这种方案数据查询,参数封装都需要手动写代码处理,比较繁琐,我之前还提供过另外一种方案,通过注解进行声明,由框架解析注解来自动处理,代码量少很多,不过我的代码还有很多要优化完善的地方,但我想这种思想是好的,可以看看https://cloud.tencent.com/developer/article/2184996
贴出TCC部分的代码看一下,源码可以查看 https://github.com/jujunchen/tcc-third-transaction:
package csdn.itsaysay.we.service.handler;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import org.mengyun.tcctransaction.api.Compensable;
import org.mengyun.tcctransaction.api.UniqueIdentity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import csdn.itsaysay.we.bean.Third;
import csdn.itsaysay.we.bean.model.FaceDO;
import csdn.itsaysay.we.bean.model.ThirdHistoryDO;
import csdn.itsaysay.we.mapper.ThirdHistoryMapper;
import csdn.itsaysay.we.third.feign.ThirdFeignClient;
import csdn.itsaysay.we.third.third1.T1FaceService;
import csdn.itsaysay.we.third.third2.T2FaceService;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class FaceAdd {
@Resource
private T1FaceService third1FaceService;
@Resource
private T2FaceService third2FaceService;
@Resource
private ThirdHistoryMapper thirdHistoryMapper;
@Resource
private ThirdFeignClient thirdFeignClient;
@Compensable(confirmMethod = "confirmFaceAdd", cancelMethod = "cancelFaceAdd")
@Transactional
public void tryFaceAdd(@UniqueIdentity FaceDO face) {
log.info("------->we try");
//把第三方接口独立成服务,通过feign去调用
// FaceAddReq req = BeanUtil.copyProperties(face, FaceAddReq.class);
// thirdFeignClient.faceAdd(req);
//直接写在当前服务里面
String id1 = third1FaceService.faceAdd(face);
thirdHistoryMapper.insert(buildModel(face.getId(), id1, Third.ADD, Third.THIRD1));
String id2 = third2FaceService.faceAdd(face);
thirdHistoryMapper.insert(buildModel(face.getId(), id2, Third.ADD, Third.THIRD2));
}
public void confirmFaceAdd(FaceDO face) {
log.info("------->we confirm");
}
public void cancelFaceAdd(FaceDO face) {
log.info("------->we cancel");
List<ThirdHistoryDO> faceThirdDOs = thirdHistoryMapper.selectList(Wrappers.<ThirdHistoryDO>lambdaQuery()
.eq(ThirdHistoryDO::getWeData, face.getId())
.eq(ThirdHistoryDO::getRtype, Third.ADD));
for (ThirdHistoryDO thirdHistoryDO : faceThirdDOs) {
//如果只有几个的话,可以单独指定,如果是不确定有几个的话,还要根据third字段唯一标识来获取应该调用哪个第三方
if (Objects.equals(thirdHistoryDO.getThird(), Third.THIRD1)) {
third1FaceService.faceDelete(thirdHistoryDO.getThirdData());
}
if (Objects.equals(thirdHistoryDO.getThird(), Third.THIRD2)) {
third1FaceService.faceDelete(thirdHistoryDO.getThirdData());
}
}
}
private ThirdHistoryDO buildModel(Integer id, String thirdId, String rtype, String third) {
ThirdHistoryDO thirdHistoryDO = new ThirdHistoryDO();
thirdHistoryDO.setWeData(String.valueOf(id));
thirdHistoryDO.setThirdData(thirdId);
thirdHistoryDO.setRtype(rtype);
thirdHistoryDO.setThird(third);
return thirdHistoryDO;
}
}