当前位置: 首页 > news >正文

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

跨行转账:TCC解决银行间账户余额同步问题
订单支付:Try阶段预扣库存,Confirm阶段实际扣款
对账系统:批量交易的事务一致性保障

一、添加依赖

<!-- 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配置

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

三、TCC接口与实现

@Conpensable(confirmMethod = "confirmTransfer",cancelMethod = "cancelTransfer",asyncConfirm = false //是否异步Confirm(高并发场景建议true)
)
public interface PaymentService{@Transactionalboolean tryTransfer(@BusinessActionContextParameter(paramName = "fromAccount") String fromAccount,@BusinessActionContextParameter(paramName = "toAccount") String toAccount,@BusinessActionContextParameter(paramName = "amount") BigDecimal amount);boolean confirmTransfer(BusinessActionContext context);boolean canelTransfer(BusinessActionContext context);
}
@Service
public class PaymentServiceImpl implements PaymentService {@Autowiredprivate AccountDao accountDao;@Overridepublic 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;}@Overridepublic 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;}@Overridepublic boolean cancelTransfer(BusinessActionContext context) {// 4. Cancel阶段:释放冻结资金String fromAccount = context.getActionContext("fromAccount");BigDecimal amount = context.getActionContext("amount");accountDao.unfreezeAmount(fromAccount, amount);return true;}
}

四、启动TCC事务

@Service
public class TransferFacade {@Autowiredprivate PaymentService paymentService;@Autowiredprivate TransactionRepository transactionRepository;@Transactionalpublic 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)}
}
http://www.dtcms.com/a/332905.html

相关文章:

  • 01数据结构-拓扑排序
  • Go语言实战案例:静态资源服务(CSS、JS、图片)
  • 迁移学习的常见研究领域(附有相关资料)
  • Kubernetes(2)pod的管理及优化
  • 数据结构初阶(17)排序算法——非比较排序、排序算法总结
  • LintCode第107题-单词拆分-新版
  • 国产操作系统之openEuler:根深叶茂
  • 力扣习题:基本计算器
  • 通过CANopen 配置闭环驱动器C5-E——易格斯igus
  • platform总线注册流程分析
  • CUDA 编程笔记:使用 CUDA 加速数组总和
  • 102、【OS】【Nuttx】【周边】文档构建渲染:安装 Esbonio 服务器
  • 【JavaEE】多线程 -- 死锁问题
  • linux服务器查看某个服务启动,运行的时间
  • 如何将 iPhone 应用程序传输到新 iPhone?
  • C++ rapidjson库使用示例
  • 【慕伏白】CTFHub 技能树学习笔记 -- Web 前置技能之HTTP协议
  • Vue 侦听器(watch 与 watchEffect)全解析3
  • 【ESP32】ESP32-P4 通过 SDIO 连接 ESP32-C6
  • FCC认证三星XR头显加速全球量产,微美全息AI+AR技术引领智能眼镜硬件创新
  • webgis-maps通用地图框架库(leaflet+mapbox)
  • 《探秘浏览器Web Bluetooth API设备发现流程》
  • K8S-Pod资源对象
  • 英语角的恢复重建!
  • 【入门级-算法-6、排序算法:排序的基本概念冒泡排序】
  • uniapp小程序ocr-navigator身份证拍照上传替换方案
  • Rust 入门 集合类型 (十六)
  • 103、【OS】【Nuttx】【周边】文档构建渲染:Sphinx 配置文件
  • Spring 框架中,@EnableScheduling和 @Scheduled详解
  • 【SpringBoot】SpringBoot 中的 Shiro、Spring Security 学习过程及碰到的问题和解决方法