首页 > 编程知识 正文

分布式事务框架对比,分布式事务框架seata

时间:2023-05-06 03:31:53 阅读:48949 作者:4787

另一方面,Seata TCC模式是分布式全局事务,整体为两级提交模式。 全局事务由多个分支事务组成。 分支事务必须满足两阶段提交的模型要求。 这意味着每个分支事务都需要自己的东西。

一级预准备行为

两阶段commit或回滚行为

根据两个阶段的行为模式,将分支事务分为自动(分支)事务处理模式和TCC (分支)事务处理模式。

AT 模式(参考链接 TBD)基于 支持本地 ACID 事务 的 关系型数据库:

阶段预准备行为:本地事务将业务数据更新和相应的回退记录一起提交。

阶段提交行为—立即成功完成并自动异步批量清理回滚日志。

两阶段回滚操作:通过回滚日志自动生成补偿操作,完成数据回滚。

相应的,TCC 模式,不依赖于底层数据资源的事务支持:

阶段1的prepare行为:调用自定义的prepare逻辑。

两阶段commit行为:调用自定义的commit逻辑。

两阶段回滚行为:调用自定义的回滚逻辑。

TCC模式支持将自定义的分支事务合并到全局事务的管理中。

二、TCC模式的实现

@LocalTCC必须注释到接口类,并应用于SpringCloud Feign模式的TCC

@TwoPhaseBusinessAction注释try方法。 其中,name是当前tcc方法的bean名称,可以写方法名称(请记住它是全局唯一的)。 commitMethod指向提交方法,而rollbackMethod指向事务回滚方法。 如果指定了三个方法,seata将根据全局事务的成功或失败自动调用提交方法或回滚方法。

@ businessactioncontextparameter注释可以将参数传递给两级(commitMethod/rollbackMethod )方法。

BusinessActionContext是指TCC事务的上下文

1、定义TCC接口

@ localtccpublicinterfacetccactionone {/* * * prepare boolean.* * @ paramactioncontexttheactioncontext * @ paramathea * @ racthea @ twophasebusinessaction (name=' tccactionone ',commitMethod='commit ',roll back method=' roll back ' (公共boleack )/* * * commit boolean.* * @ paramaction contexttheaction context * @ returntheboolean */publicboolean commit (业务操作系统) @ paramactioncontexttheactioncontext * @ returntheboolean */publicbooleanrollback (} @ localtccpublicationtwo @ paramactioncontexttheactioncontext * @ parambtheb * @ paramlist the list * @ returntheboolean */@ twophasebusinesssaction (nation ) commitMethod='commit ',rollbackMethod='rollback ' ) publicbooleanprepare (业务连接测试上下文,@业务连接

xtParameter(paramName = "b") String b, @BusinessActionContextParameter(paramName = "c", index = 1) List list); /** * Commit boolean. * * @param actionContext the action context * @return the boolean */ public boolean commit(BusinessActionContext actionContext); /** * Rollback boolean. * * @param actionContext the action context * @return the boolean */ public boolean rollback(BusinessActionContext actionContext);} public class ResultHolder { private static Map<String, String> actionOneResults = new ConcurrentHashMap<String, String>(); private static Map<String, String> actionTwoResults = new ConcurrentHashMap<String, String>(); /** * Set action one result. * * @param txId the tx id * @param result the result */ public static void setActionOneResult(String txId, String result) { actionOneResults.put(txId, result); } /** * Get action one result string. * * @param txId the tx id * @return the string */ public static String getActionOneResult(String txId) { return actionOneResults.get(txId); } /** * Set action two result. * * @param txId the tx id * @param result the result */ public static void setActionTwoResult(String txId, String result) { actionTwoResults.put(txId, result); } /** * Get action two result string. * * @param txId the tx id * @return the string */ public static String getActionTwoResult(String txId) { return actionTwoResults.get(txId); }}

创建实现类

@Servicepublic class TccActionOneImpl implements TccActionOne { @Override public boolean prepare(BusinessActionContext actionContext, int a) { String xid = actionContext.getXid(); System.out.println("TccActionOne prepare, xid:" + xid + ", a:" + a); return true; } @Override public boolean commit(BusinessActionContext actionContext) { String xid = actionContext.getXid(); System.out.println("TccActionOne commit, xid:" + xid + ", a:" + actionContext.getActionContext("a")); ResultHolder.setActionOneResult(xid, "T"); return true; } @Override public boolean rollback(BusinessActionContext actionContext) { String xid = actionContext.getXid(); System.out.println("TccActionOne rollback, xid:" + xid + ", a:" + actionContext.getActionContext("a")); ResultHolder.setActionOneResult(xid, "R"); return true; }} @Servicepublic class TccActionTwoImpl implements TccActionTwo { @Override public boolean prepare(BusinessActionContext actionContext, String b, List list) { String xid = actionContext.getXid(); System.out.println("TccActionTwo prepare, xid:" + xid + ", b:" + b + ", c:" + list.get(1)); return true; } @Override public boolean commit(BusinessActionContext actionContext) { String xid = actionContext.getXid(); System.out.println("TccActionTwo commit, xid:" + xid + ", b:" + actionContext.getActionContext("b") + ", c:" + actionContext.getActionContext("c")); ResultHolder.setActionTwoResult(xid, "T"); return true; } @Override public boolean rollback(BusinessActionContext actionContext) { String xid = actionContext.getXid(); System.out.println("TccActionTwo rollback, xid:" + xid + ", b:" + actionContext.getActionContext("b") + ", c:" + actionContext.getActionContext("c")); ResultHolder.setActionTwoResult(xid, "R"); return true; }}

创建调用tcc接口类,这里我们编写了两个方法,一个是提交方法,一个是回滚方法。
并使用@GlobalTransactional注释

@Servicepublic class TccTransactionService { @Autowired private TccActionOne tccActionOne; @Autowired private TccActionTwo tccActionTwo; /** * 发起分布式事务 * * @return string string */ @GlobalTransactional public String doTransactionCommit() { //第一个TCC 事务参与者 boolean result = tccActionOne.prepare(null, 1); if (!result) { throw new RuntimeException("TccActionOne failed."); } List list = new ArrayList(); list.add("c1"); list.add("c2"); result = tccActionTwo.prepare(null, "two", list); if (!result) { throw new RuntimeException("TccActionTwo failed."); } return RootContext.getXID(); } /** * Do transaction rollback string. * * @param map the map * @return the string */ @GlobalTransactional public String doTransactionRollback(Map map) { //第一个TCC 事务参与者 boolean result = tccActionOne.prepare(null, 1); if (!result) { throw new RuntimeException("TccActionOne failed."); } List list = new ArrayList(); list.add("c1"); list.add("c2"); result = tccActionTwo.prepare(null, "two", list); if (!result) { throw new RuntimeException("TccActionTwo failed."); } map.put("xid", RootContext.getXID()); throw new RuntimeException("transacton rollback"); }}

在创建个controller类去测试我们的提交和回滚是否正常工作

@RequestMapping("tcc/commit") public String commit() { String txId = tccTransactionService.doTransactionCommit(); return txId; } @RequestMapping("tcc/rollback") public String rollback() { Map map = new HashMap(16); try { tccTransactionService.doTransactionRollback(map); Assert.isTrue(false, "分布式事务未回滚"); } catch (Throwable t) { Assert.isTrue(true, "分布式事务异常回滚"); System.out.println(t.getMessage()); } String txId = (String)map.get("xid"); return txId; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。