springcloud分布式事务LCN实现
1 LCN介绍
利用事务管理器,统一协调各个本地事务,实现事务的一致性。
特性:
1、一致性,通过TxManager协调控制与事务补偿机制确保数据一致性
2、易用性,仅需要在业务方法上添加@TxTransaction注解即可
3、高可用,项目模块不仅可高可用部署,事务协调器也可集群化部署
4、扩展性,支持各种RPC框架扩展,支持通讯协议与事务模式扩展
官方文档:https://www.txlcn.org/zh-cn/docs/preface.html
源码参考:https://gitee.com/wangliang1991/tx-lcn/tree/4.1.0/
2 tx-manager资源管理器改造
下载资源管理器代码 tx-manager,进行改造,可自行调整注册中心和redis集成等。
修改配置application.properties
调整端口、注册中心配置,redis配置
3 分布式事务集成使用
3.1 微服务service-1改造
3.1.1 添加pom依赖配置
<!--分布式事务配置--> <dependency> <groupId>com.codingapi</groupId> <artifactId>transaction-springcloud</artifactId> <version>4.1.0</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.codingapi</groupId> <artifactId>tx-plugins-db</artifactId> <version>4.1.0</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <!--分布式事务配置-->
3.1.2 添加tx-manger地址配置
新增tx.properties
url=http://127.0.0.1:9000/tx/manager/
3.1.3 添加测试接口
@RestController @RequestMapping("/test") public class TestController { private static Logger LOGGER = LoggerFactory.getLogger(TestController.class); @Autowired private IDemoService demoService; @PostMapping(value = "/save", produces = "application/json;charset=utf-8") @ResponseBody public int save(){ return demoService.save(); } }
public interface IDemoService { List<Test> list(); int save(); }
@Service public class DemoServiceImpl implements IDemoService /*,ITxTransaction*/ { @Autowired private ITestDao testDao; private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class); @Override public List<Test> list() { return testDao.findAll(); } /** * @TxTransaction(isStart = false) //true:事务发起方 false: 事务参与方 ,默认为参与方 * 参与方添加注解@TxTransaction或者实现接口ITxTransaction效果一样。 * @return */ @Override @TxTransaction @Transactional public int save() { int rs1 = testDao.save("test-service-2"); return rs1; } }
@Mapper public interface ITestDao { @Select("SELECT * FROM t_test") List<Test> findAll(); @Insert("INSERT INTO t_test(name) VALUES(#{name})") int save(@Param("name") String name); }
3.2 微服务service-2改造
3.2.1 添加pom依赖配置
<!--分布式事务配置--> <dependency> <groupId>com.codingapi</groupId> <artifactId>transaction-springcloud</artifactId> <version>4.1.0</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.codingapi</groupId> <artifactId>tx-plugins-db</artifactId> <version>4.1.0</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <!--分布式事务配置-->
3.2.2 添加tx-manger地址配置
新增tx.properties
url=http://127.0.0.1:9000/tx/manager/
3.2.3 添加测试接口
@RestController @RequestMapping("/test") public class TestController { private static Logger LOGGER = LoggerFactory.getLogger(TestController.class); @Autowired private IDemoService demoService; @PostMapping(value = "/save", produces = "application/json;charset=utf-8") @ResponseBody public int save(){ return demoService.save(); } }
public interface IDemoService { List<Test> list(); int save(); }
@Service public class DemoServiceImpl implements IDemoService { @Autowired private ServiceFeignClient serviceFeignClient; @Autowired private ITestDao testDao; private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class); @Override public List<Test> list() { return testDao.findAll(); } @Override @TxTransaction(isStart = true) //true:事务发起方 false:事务参与方 @Transactional public int save() { int rs1 = testDao.save("test-service-2"); int rs2 = serviceFeignClient.save(); int v = 100/0; return rs1+rs2; } }
@Mapper public interface ITestDao { @Select("SELECT * FROM t_test") List<Test> findAll(); @Insert("INSERT INTO t_test(name) VALUES(#{name})") int save(@Param("name") String name); }
3.2.4 添加feign配置
@FeignClient(name = "service-1") public interface ServiceFeignClient { @PostMapping(value = "/test/save") @ResponseBody int save(); }