金融项目高可用分布式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)
    }
}
相关推荐
与遨游于天地10 分钟前
分布式锁从Redis到Redisson的演进
数据库·redis·分布式
lilihuigz1 小时前
WordPress AI代理:开源CMS如何成为智能网络操作系统的核心驱动力 - WP站长
人工智能·开源·cms
码途漫谈1 小时前
Easy-Vibe高级开发篇阅读笔记(二)——CC教程之Claude Code MCP 完全指南
笔记·ai·开源·ai编程
xmdy58662 小时前
Flutter+开源鸿蒙实战|校园易生活Day1 项目初始化搭建+开发环境校验+工程目录规范+第三方库集成+多端屏幕适配+全局底部导航
flutter·开源·harmonyos
Peter·Pan爱编程2 小时前
第六篇:VS Code + Continue 插件:开源爱好者的低成本高自由度方案
ide·开源·ai编程
Hical_W3 小时前
从 io_context 出发,掌握 C++20 协程式异步 I/O,学会 TCP 服务器、定时器和多线程模型,结合 Hical 框架实战解读
服务器·tcp/ip·开源·c++20
Francek Chen3 小时前
【大数据存储与管理】实验3:熟悉常用的HBase操作
大数据·数据库·分布式·hbase
xmdy58663 小时前
Flutter+开源鸿蒙实战|校园易生活Day2 第三方库批量集成+全局Toast提示+网络状态监听+首页轮播图+资讯卡片布局
flutter·开源·harmonyos
小妖同学学AI3 小时前
告别手动盯盘!开源框架Freqtrade,教你用Python打造“永不下班”的AI交易员
人工智能·python·开源
呱牛do it3 小时前
第三章 · 根基 第 8 篇:客户信息的核心价值
金融