springcloud整合分布式事务(seata) 2023-01-24 05:21 2437阅读 0赞 一.理论部分 二.实践部分 这里整理了一些落地的分布式事务中间件: 1. [tx-lcn][] 2. [seata][] 其中tx-lcn项目git3.9K的star,5.0以后由于框架兼容了LCN(2pc)、TCC、TXC 三种事务模式,为了区分LCN模式,特此将LCN分布式事务改名为TX-LCN分布式事务框架。它是一种本地事务协调,本身并不会产生事务,2020年10月之后暂时没有新的提交记录。 seata的git18.7K的star,开源之前的内部版本在阿里经济体内部一直扮演着分布式一致性中间件的角色,帮助经济体平稳的度过历年的双11,对各BU业务进行了有力的支撑。商业化产品GTS 先后在阿里云、金融云进行售卖。Seata分TC、TM和RM三个角色,TC(Server端)为单独服务端部署,TM和RM(Client端)由业务系统集成。 ![14430142-f139824abbfe46fc.png][] image.png 因为项目是使用alibaba springcloud的,并且seata比较活跃,禁受住了双十一的考验,本文使用seata整合目前的项目。 -------------------- 官方文档:[http://seata.io/zh-cn/docs/user/quickstart.html][http_seata.io_zh-cn_docs_user_quickstart.html] 准备工作: 1.客户端依赖引入: <dependency> <groupId>io.seata</groupId> <artifactId>seata-spring-boot-starter</artifactId> <version>最新版</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> <version>2.2.1.RELEASE</version> <exclusions> <exclusion> <groupId>io.seata</groupId> <artifactId>seata-spring-boot-starter</artifactId> </exclusion> </exclusions> </dependency> 这里需要注意,需要根据自身项目的Spring Cloud Alibaba Version版本进行相应seata版本的选择,以下是Spring Cloud Alibaba个版本与seata版本的对应关系: ![14430142-36cc3e6cdeae0975.png][] image.png ![14430142-578047759f971b51.png][] image.png 如果还是无法启动,可以使用以下依赖: <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> </dependency> 2.客户端应用配置 注意波浪线处需要相同 ![14430142-ba0ad8bd3521f4cb.png][] image.png 3.服务端配置 * 修改seata/conf 中的file.conf (此次存储使用mysql) ## transaction log store, only used in seata-server store { ## store mode: file、db、redis mode = "db" ## file store property file { ## store location dir dir = "sessionStore" # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions maxBranchSessionSize = 16384 # globe session size , if exceeded throws exceptions maxGlobalSessionSize = 512 # file buffer size , if exceeded allocate new buffer fileWriteBufferCacheSize = 16384 # when recover batch read size sessionReloadReadSize = 100 # async, sync flushDiskMode = async } ## database store property db { ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc. datasource = "druid" ## mysql/oracle/postgresql/h2/oceanbase etc. dbType = "mysql" driverClassName = "com.mysql.jdbc.Driver" url = "jdbc:mysql://XXX.XX.XX.XX:3306/seata" user = "XXXX" password = "XXXXXX" minConn = 5 maxConn = 100 globalTable = "global_table" branchTable = "branch_table" lockTable = "lock_table" queryLimit = 100 maxWait = 5000 } ## redis store property redis { host = "127.0.0.1" port = "6379" password = "" database = "0" minConn = 1 maxConn = 10 maxTotal = 100 queryLimit = 100 } } * 修改seata/conf 中的\_registry.conf registry { # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa type = "nacos" loadBalance = "RandomLoadBalance" loadBalanceVirtualNodes = 10 nacos { application = "serverAddr" serverAddr = "192.168.8.131:8848" group = "SEATA_GROUP" namespace = "e945d9a7-1942-4e1b-8ed0-29fdcd67d555" cluster = "default" username = "" password = "" } eureka { serviceUrl = "http://localhost:8761/eureka" application = "default" weight = "1" } redis { serverAddr = "localhost:6379" db = 0 password = "" cluster = "default" timeout = 0 } zk { cluster = "default" serverAddr = "127.0.0.1:2181" sessionTimeout = 6000 connectTimeout = 2000 username = "" password = "" } consul { cluster = "default" serverAddr = "127.0.0.1:8500" } etcd3 { cluster = "default" serverAddr = "http://localhost:2379" } sofa { serverAddr = "127.0.0.1:9603" application = "default" region = "DEFAULT_ZONE" datacenter = "DefaultDataCenter" cluster = "default" group = "SEATA_GROUP" addressWaitTime = "3000" } file { name = "file.conf" } } config { # file、nacos 、apollo、zk、consul、etcd3 type = "file" nacos { serverAddr = "127.0.0.1:8848" namespace = "" group = "SEATA_GROUP" username = "" password = "" } consul { serverAddr = "127.0.0.1:8500" } apollo { appId = "seata-server" apolloMeta = "http://192.168.1.204:8801" namespace = "application" apolloAccesskeySecret = "" } zk { serverAddr = "127.0.0.1:2181" sessionTimeout = 6000 connectTimeout = 2000 username = "" password = "" } etcd3 { serverAddr = "http://localhost:2379" } file { name = "file.conf" } } * 执行sql,新建三张表 -- the table to store GlobalSession data drop table if exists `global_table`; create table `global_table` ( `xid` varchar(128) not null, `transaction_id` bigint, `status` tinyint not null, `application_id` varchar(32), `transaction_service_group` varchar(32), `transaction_name` varchar(128), `timeout` int, `begin_time` bigint, `application_data` varchar(2000), `gmt_create` datetime, `gmt_modified` datetime, primary key (`xid`), key `idx_gmt_modified_status` (`gmt_modified`, `status`), key `idx_transaction_id` (`transaction_id`) ); -- the table to store BranchSession data drop table if exists `branch_table`; create table `branch_table` ( `branch_id` bigint not null, `xid` varchar(128) not null, `transaction_id` bigint , `resource_group_id` varchar(32), `resource_id` varchar(256) , `lock_key` varchar(128) , `branch_type` varchar(8) , `status` tinyint, `client_id` varchar(64), `application_data` varchar(2000), `gmt_create` datetime, `gmt_modified` datetime, primary key (`branch_id`), key `idx_xid` (`xid`) ); -- the table to store lock data drop table if exists `lock_table`; create table `lock_table` ( `row_key` varchar(128) not null, `xid` varchar(96), `transaction_id` long , `branch_id` long, `resource_id` varchar(256) , `table_name` varchar(32) , `pk` varchar(36) , `gmt_create` datetime , `gmt_modified` datetime, primary key(`row_key`) ); -- the table to store seata xid data -- 0.7.0+ add context -- you must to init this sql for you business databese. the seata server not need it. -- 此脚本必须初始化在你当前的业务数据库中,用于AT 模式XID记录。与server端无关(注:业务数据库) -- 注意此处0.3.0+ 增加唯一索引 ux_undo_log -- drop table `undo_log`; CREATE TABLE `undo_log` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `branch_id` bigint(20) NOT NULL, `xid` varchar(100) NOT NULL, `context` varchar(128) NOT NULL, `rollback_info` longblob NOT NULL, `log_status` int(11) NOT NULL, `log_created` datetime NOT NULL, `log_modified` datetime NOT NULL, `ext` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; * 启动 进入/seata/bin/,执行sh seata-server.sh 1. 代码中使用 给事务发起者的方法上加上@GlobalTransactional即可,例如: @GlobalTransactional(rollbackFor = Exception.class) private int function() { ... ... } 参考:[https://zhuanlan.zhihu.com/p/183753774][https_zhuanlan.zhihu.com_p_183753774] [tx-lcn]: https://links.jianshu.com/go?to=https%3A%2F%2Fgithub.com%2Fcodingapi%2Ftx-lcn [seata]: https://links.jianshu.com/go?to=https%3A%2F%2Fgithub.com%2Fseata%2Fseata [14430142-f139824abbfe46fc.png]: /images/20221119/efccd05544fa4f27b4b7be6b057587ca.png [http_seata.io_zh-cn_docs_user_quickstart.html]: https://links.jianshu.com/go?to=http%3A%2F%2Fseata.io%2Fzh-cn%2Fdocs%2Fuser%2Fquickstart.html [14430142-36cc3e6cdeae0975.png]: /images/20221119/453d8a14252d483a8f5a841ffd236fbf.png [14430142-578047759f971b51.png]: /images/20221119/ec7af210a2f94ddc9da7a97a3e432c00.png [14430142-ba0ad8bd3521f4cb.png]: /images/20221119/dc5dc55c70b64f93ad75a7b8c3ce0cdf.png [https_zhuanlan.zhihu.com_p_183753774]: https://links.jianshu.com/go?to=https%3A%2F%2Fzhuanlan.zhihu.com%2Fp%2F183753774
相关 springcloud整合LCN分布式事务 开发工具idea、jdk8,redis、maven 首先用idea搭建eureka-server 配置文件: server.port=8761 深碍√TFBOYSˉ_/ 2022年02月14日 19:36/ 0 赞/ 155 阅读
相关 微服务 SpringCloud Alibaba Seata处理分布式事务 微服务 SpringCloud Alibaba Seata处理分布式事务 微服务 SpringCloud Alibaba Seata处理分布式事务 一、 缺乏、安全感/ 2022年10月29日 11:22/ 0 赞/ 100 阅读
相关 SpringCloud Alibaba 微服务架构(十三)- SpringCloud 整合Seata 解决分布式事务 seata官网:[http://seata.io/zh-cn/][http_seata.io_zh-cn] 前言 在当下微服务架构比较火热时,新一代微服务解决方案Spr 野性酷女/ 2022年11月20日 08:22/ 0 赞/ 148 阅读
相关 springboot整合dubbo整合seata实现分布式事务 一、seata简介 seata官网: https://github.com/seata/seata 二、seata整合dubbo 2.1 seata整 逃离我推掉我的手/ 2022年11月27日 12:08/ 0 赞/ 161 阅读
相关 springcloud整合seata springcloud整合seata 1. 添加依赖 <dependency> <groupId>com.alibaba.cloud 墨蓝/ 2022年12月03日 08:56/ 0 赞/ 36 阅读
相关 SpringCloud Alibaba Seata 分布式事务 -------------------- SpringCloud Alibaba Seata 分布式事务 1-SpringCloud Alibaba Seata 爱被打了一巴掌/ 2022年12月29日 06:22/ 0 赞/ 29 阅读
相关 Seata--分布式事务 1、Seata 分布式事务基础 1.1事务 事务指的就是一个操作单元,在这个操作单元中的所有操作最终要保持一致的行为,要么所有操作都成功,要么所有的操作都被撤销。简 左手的ㄟ右手/ 2023年01月01日 15:50/ 0 赞/ 27 阅读
相关 分布式事务--Seata-AT--SpringCloud(starter版) 原文网址:[分布式事务--Seata--AT(starter)(feign)\_IT利刃出鞘的博客-CSDN博客][--Seata--AT_starter_feign_IT_- 青旅半醒/ 2023年01月06日 05:48/ 0 赞/ 43 阅读
相关 SpringCloud 整合分布式事务组件 Seata (八) 前言 ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV 电玩女神/ 2023年01月16日 02:57/ 0 赞/ 16 阅读
相关 springcloud整合分布式事务(seata) 一.理论部分 二.实践部分 这里整理了一些落地的分布式事务中间件: 1. [tx-lcn][] 2. [seata][] 其中tx-lcn项目git3.9K的st 妖狐艹你老母/ 2023年01月24日 05:21/ 0 赞/ 2438 阅读
还没有评论,来说两句吧...