金融项目高可用分布式TCC-Transaction(开源框架)

跨行转账:TCC解决银行间账户余额同步问题

订单支付:Try阶段预扣库存,Confirm阶段实际扣款

对账系统:批量交易的事务一致性保障

一、添加依赖

xml 复制代码
<!-- Maven 依赖 -->
<dependency>
    <groupId>org.mengyun</groupId>
    <artifactId>tcc-transaction-core</artifactId>
    <version>1.2.0</version>
</dependency>
<dependency>
    <groupId>org.mengyun</groupId>
    <artifactId>tcc-transaction-spring</artifactId>
    <version>1.2.0</version>
</dependency>

二、SpringBoot配置

yml 复制代码
#application.yml
tcc:
  transaction:
    repository:
      type: redis  # 事务日志存储类型(支持JDBC、ZooKeeper等)
      redis:
        host: 127.0.0.1
        port: 6379
    recover:
      cron: "0 */1 * * * ?"  # 定时任务补偿间隔(默认1分钟)

三、TCC接口与实现

java 复制代码
@Conpensable(
	confirmMethod = "confirmTransfer",
	cancelMethod = "cancelTransfer",
	asyncConfirm = false //是否异步Confirm(高并发场景建议true)
)
public interface PaymentService{
	
	@Transactional
	boolean tryTransfer(
		@BusinessActionContextParameter(paramName = "fromAccount") String fromAccount,
        @BusinessActionContextParameter(paramName = "toAccount") String toAccount,
        @BusinessActionContextParameter(paramName = "amount") BigDecimal amount
	);

	boolean confirmTransfer(BusinessActionContext context);

	boolean canelTransfer(BusinessActionContext context);
}
java 复制代码
@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private AccountDao accountDao;
	
	@Override
    public boolean tryTransfer(String fromAccount, String toAccount, BigDecimal amount) {
        // 1. Try阶段:资源预留(冻结余额)
        Account account = accountDao.selectForUpdate(fromAccount);
        if (account.getBalance().compareTo(amount) < 0) {
            throw new InsufficientBalanceException();
        }
        accountDao.freezeAmount(fromAccount, amount);  // 生成冻结记录
        
        // 2. 记录事务上下文(自动由框架处理)
        return true;
    }

    @Override
    public boolean confirmTransfer(BusinessActionContext context) {
        // 3. Confirm阶段:实际扣款
        String fromAccount = context.getActionContext("fromAccount");
        BigDecimal amount = context.getActionContext("amount");
        accountDao.decreaseBalance(fromAccount, amount);
        accountDao.unfreezeAmount(fromAccount, amount);
        return true;
    }

    @Override
    public boolean cancelTransfer(BusinessActionContext context) {
        // 4. Cancel阶段:释放冻结资金
        String fromAccount = context.getActionContext("fromAccount");
        BigDecimal amount = context.getActionContext("amount");
        accountDao.unfreezeAmount(fromAccount, amount);
        return true;
    }
}

四、启动TCC事务

java 复制代码
@Service
public class TransferFacade {
    @Autowired
    private PaymentService paymentService;
    @Autowired
    private TransactionRepository transactionRepository;

    @Transactional
    public void executeTransfer(String fromAccount, String toAccount, BigDecimal amount) {
        // 1. 创建事务上下文
        TransactionContext context = new TransactionContext();
        context.setPropagated(true);

        // 2. 执行Try阶段
        boolean tryResult = paymentService.tryTransfer(fromAccount, toAccount, amount);
        if (!tryResult) {
            throw new TryPhaseException("Try阶段失败");
        }

        // 3. 模拟其他服务调用(如风控检查)
        riskCheckService.validate(fromAccount, amount);

        // 4. 事务自动提交(框架自动触发Confirm)
    }
}
相关推荐
魔猴疯猿1 天前
无人机地面站中不同的飞行模式具体含义释义(开源飞控常用的5种模式)
开源·无人机·mavlink·飞行模式
周杰伦_Jay1 天前
【OpenManus深度解析】MetaGPT团队打造的开源AI智能体框架,打破Manus闭源壁垒。包括架构分层、关键技术特点等内容
人工智能·深度学习·opencv·架构·开源
西***63471 天前
从信号处理到智能协同:高清混合矩阵全链路技术拆解,分布式系统十大趋势抢先看
网络·分布式·矩阵
算家计算1 天前
DeepSeek-OCR本地部署教程:DeepSeek突破性开创上下文光学压缩,10倍效率重构文本处理范式
人工智能·开源·deepseek
阿维的博客日记1 天前
从夯到拉的Redis和MySQL双写一致性解决方案排名
redis·分布式·mysql
RobinMin1 天前
Droid CLI 试用体验
人工智能·开源
CloudWeGo1 天前
CloudWeGo 技术沙龙暨四周年庆典活动回顾:高性能微服务技术实践与 AI 新范式
开源
阿里云云原生1 天前
20 万奖金池就位!Higress AI 网关开发挑战赛参赛指南
微服务·开源
IT技术分享社区1 天前
IT运维干货:lnav开源日志分析工具详解与CentOS实战部署
linux·运维·服务器·开源·centos