金融项目高可用分布式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)
    }
}
相关推荐
拥抱AGI21 分钟前
Qwen3.5开源矩阵震撼发布!从0.8B到397B,不同规模模型性能、显存、速度深度对比与选型指南来了!
人工智能·学习·程序员·开源·大模型·大模型训练·qwen3.5
geinvse_seg24 分钟前
开源实战——手把手教你搭建AI量化分析平台:从Docker部署到波浪理论实战
人工智能·docker·开源·蓝耘元生代·蓝耘maas
分布式存储与RustFS24 分钟前
MinIO迎来“恶龙”?RustFS这款开源存储简直“不讲武德”
架构·rust·开源·对象存储·minio·企业存储·rustfs
FIT2CLOUD飞致云33 分钟前
新增工作流类型工具,对话时可选择模型与知识库,MaxKB开源企业级智能体平台v2.8.0版本发布
人工智能·ai·开源·智能体·maxkb
code 小楊34 分钟前
从开源折戟到闭源破局:Meta Muse Spark 全解析(含案例+调用指南)
人工智能·开源
darkb1rd36 分钟前
gemma-tuner-multimodal:实战
开源·github·好物分享
Albert Edison1 小时前
【RabbitMQ】快速入门
java·分布式·rabbitmq
a1117761 小时前
变电站数字孪生大屏ThreeJS 开源项目
前端·信息可视化·开源·html
想你依然心痛1 小时前
HarmonyOS 5.0医疗健康开发实战:构建分布式健康监测与AI预警系统
人工智能·分布式·harmonyos
weixin_446260851 小时前
释放工作效率,Multica开源管理代理平台
人工智能·开源