Seata1.4.0分布式事务解决方案研究分享

微服务流行的时代,解决了很多企业的业务问题,也带来的一些技术问题,例如最常见的分布式事务问题。当Seata的出现,分布式事务迎刃而解。Seata设计思想官网(http://seata.io/zh-cn/index.html)有详细的介绍,此处不再赘述,想更深入的理解,建议阅读源码。此次主要分享实际项目中的Seata的使用,让小伙伴少走弯路。

1 Seata服务安装包zip下载

 

1.1seata安装包解压

 

1.2安装包解压目录

 

2 Seata服务file.conf,registry.conf配置,配置文件备份后再更改。

2.1 打开conf目录(位置参考1.2图片)找到file.conf,registry.conf

2.2 file.conf配置如下图

2.2.1file.conf配置更改

 

2.3 registry.conf配置如下图。更改后要保存

 

2.3.1registry.conf注册中心更改

2.3.2registry.conf配置中心更改

3 导入Seata参数配置到nacos配置中心。就是把config.txt的参数通过脚本nacos-config.sh导入到nacos配置中心,以后项目也从nacos配置读取,维护也方便。config.txt 和nacos-config.sh脚本在seata0.9才有,下载参考上面1.1图片。windows使用nacos-config.sh脚本要安装gitbash(安装教程https://www.jianshu.com/p/e9228db0bc54)。打开seata0.9把config.txt 和nacos-config.sh脚本拷贝到1.4.0conf下如图

3.1config.txt所在路径

3.2nacos-config.sh所在路径

 

3.3namespace创建与获取

3.4 打开GitBash输入命令,sh nacos-config.sh 后面的参数用自个创建的。

sh nacos-config.sh -h nacos的ip -p nacos端口 -g nacos配置文件的组 -t 你的namespace号(若是public可省略此选项) -u nacos用户名 -w nacos密码

sh nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t 65e26baf-ec52-4f6a-bb7e-6a84eb663b5b -u nacos -w nacos 如下图

3.4.1namespace创建与获取

3.4.2config.txt导入命令

3.4.3导入成功提示

3.4.4导入成功后,nacos可以查看到配置参数

4 新建表branch_table, global_table, lock_table这三个表用seata数据库跟上面第二步介绍一致, undo_log在业务数据中创建。这些脚本也是seata0.9中可以获取到,seata0.9版本下载参考第1步。用下面也可以。

-- 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;

5 windows下启动Seata服务如下图

6 项目配置与调用

6.1 依赖包引用

          <dependency>
                <groupId>io.seata</groupId>
                <artifactId>seata-spring-boot-starter</artifactId>
                <version>${seata.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>io.seata</groupId>
                        <artifactId>seata-spring-boot-starter</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>        

6.2 yml文件配置,enableAutoDataSourceProxy要设置true不然异常回滚不了,设为true就不用再手动配置数据源代理。用了下面配置,文件File.conf与Registry.conf文件不用引到项目中,因为上面config.txt导入到nacos配置中心了,项目用到的都从nacos配置中心获取。配置后记得重启生效。下面yam文件seata配置不属于其他配置项。my_test_tx_group要与nacos配置中心得一致如图6.2.1。yam文件seata配置vgroup-mapping下my_test_tx_group值也要与与nacos配置中心得一致如图6.2.3

6.2.1nacos配置中心的

6.2.3vgroup-mapping配置与nacos配置中心得一致

       

6.3 项目中使用如下图,跨服务调用,数据库是同一个,不用seata其中微服务有异常是不会回滚的,不符合业务需求的。@GlobalTransactional一般在服务发起者中加上。

 

6.3.1开启全局事务

欢迎关注转发留言

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值