金融项目高可用分布式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)
    }
}
相关推荐
Are_You_Okkk_3 小时前
基于MonkeyCode解析AI研发新模式,根治开发低效痛点
大数据·人工智能·开源·ai编程
phltxy5 小时前
RabbitMQ集群搭——多机多节点与单机多节点
分布式·rabbitmq·ruby
2601_959985746 小时前
MHMarkets迈汇:“减重药渠道拓宽估值空间”
金融
冬奇Lab6 小时前
每日一个开源项目(第116篇):FreeDomain - 让每个人都拥有属于自己的数字身份
开源
lauo7 小时前
从FunloomAI到ibbot:当你的手机不再是“手机”,而是你的AI副脑和生产节点
人工智能·智能手机·架构·开源·github
小程故事多_809 小时前
拆解Hermes Agent技术架构,会自我迭代的开源智能体如何突破AI传统局限
人工智能·架构·开源
Hommy889 小时前
【剪映小助手】贴纸处理接口
网络·开源·github·aigc·剪映小助手·视频剪辑自动化
三十..10 小时前
Ceph分布式存储核心技术精要与运维实践指南
运维·分布式·ceph
敲星写码11 小时前
2026远程控制软件选购指南:按人群场景预算一站式锁定,ToDesk覆盖90%用户需求
开源
QiLinkOS11 小时前
从技术到资产的跃迁:企业专利布局的深层逻辑
c语言·数据结构·c++·单片机·嵌入式硬件·算法·开源