SpringCloud Alibaba使用Seata处理分布式事务的技巧
seata简介
在传统的单体项目中,我们使用@transactional注解就能实现基本的acid事务了。
但是前提是:
1) 数据库支持事务(如:mysql的innodb引擎)
2) 所有业务都在同一个数据库中执行
随着微服务架构的引入,需要对数据库进行分库分表,每个服务拥有自己的数据库,这样传统的事务就不起作用了,那么我们如何保证多个服务中数据的一致性呢?
这样就出现了分布式事务,而seata就是为微服务架构而生的一种高性能、易于使用的分布式事务解决方案。
seata 中有三个基础组件:
- transaction coordinator(tc协调者):维护全局和分支事务的状态,驱动全局提交或回滚。
- transaction manager(tm事务管理):定义全局事务的范围,开启、提交或回滚一个全局事务。
- resource manager(rm资源管理):管理分支事务资源,与 tc 通讯并报告分支事务状态,管理本地事务的提交与回滚。
可以这么说一个分布式事务就是全局事务globaltransaction,而全局事务是由一个个的分支事务组成的,每个分支事务就是一个本地事务。
seata的生命周期
- tm 要求 tc 生成一个全局事务,并由 tc 生成一个全局事务xid 返回。
- xid 通过微服务调用链传播。
- rm 向 tc 注册本地事务,将其注册到 id 为 xid 的全局事务中。
- tm 要求 tc 提交或回滚xid 对应的全局事务。
- tc 驱动 xid 对应的全局事务对应的所有的分支事务提交或回滚。
seata安装和配置
安装nacos,本案例使用了nacos作为注册中心
https://github.com/alibaba/nacos/releases
下载nacos,本文使用的是windows版本1.4.0
使用命令行进入bin目录,以单机模式启动nacos
startup -m standalone
安装和配置seata
http://seata.io/zh-cn/blog/download.html
下载seata,这里是windows版本的1.4.0
解压后,进入conf目录,配置file.conf和registry.conf两个文件
file.conf主要是数据库的配置,配置如下
registry.conf 是注册中心的配置
另外conf目录中还需要一个脚本文件:nacos-config.sh 用于对nacos进行初始化配置
在seata1.4.0中是没有的,需要自行创建,内容如下:
#!/usr/bin/env bash # copyright 1999-2019 seata.io group. # # licensed under the apache license, version 2.0 (the "license"); # you may not use this file except in compliance with the license. # you may obtain a copy of the license at、 # # http://www.apache.org/licenses/license-2.0 # # unless required by applicable law or agreed to in writing, software # distributed under the license is distributed on an "as is" basis, # without warranties or conditions of any kind, either express or implied. # see the license for the specific language governing permissions and # limitations under the license. while getopts ":h:p:g:t:u:w:" opt do case $opt in h) host=$optarg ;; p) port=$optarg ;; g) group=$optarg ;; t) tenant=$optarg ;; u) username=$optarg ;; w) password=$optarg ;; ?) echo " usage option: $0 [-h host] [-p port] [-g group] [-t tenant] [-u username] [-w password] " exit 1 ;; esac done urlencode() { for ((i=0; i < ${#1}; i++)) do char="${1:$i:1}" case $char in [a-za-z0-9.~_-]) printf $char ;; *) printf '%%%02x' "'$char" ;; esac done } if [[ -z ${host} ]]; then host=localhost fi if [[ -z ${port} ]]; then port=8848 fi if [[ -z ${group} ]]; then group="seata_group" fi if [[ -z ${tenant} ]]; then tenant="" fi if [[ -z ${username} ]]; then username="" fi if [[ -z ${password} ]]; then password="" fi nacosaddr=$host:$port contenttype="content-type:application/json;charset=utf-8" echo "set nacosaddr=$nacosaddr" echo "set group=$group" failcount=0 templog=$(mktemp -u) function addconfig() { curl -x post -h "${contenttype}" "http://$nacosaddr/nacos/v1/cs/configs?dataid=$(urlencode $1)&group=$group&content=$(urlencode $2)&tenant=$tenant&username=$username&password=$password" >"${templog}" 2>/dev/null if [[ -z $(cat "${templog}") ]]; then echo " please check the cluster status. " exit 1 fi if [[ $(cat "${templog}") =~ "true" ]]; then echo "set $1=$2 successfully " else echo "set $1=$2 failure " (( failcount++ )) fi } count=0 for line in $(cat $(dirname "$pwd")/config.txt | sed s/[[:space:]]//g); do (( count++ )) key=${line%%=*} value=${line#*=} addconfig "${key}" "${value}" done echo "=========================================================================" echo " complete initialization parameters, total-count:$count , failure-count:$failcount " echo "=========================================================================" if [[ ${failcount} -eq 0 ]]; then echo " init nacos config finished, please start seata-server. " else echo " init nacos config fail. " fi
在seata的根目录,与conf同级的目录下,还需要config.txt 配置文件,默认也是没有的
只需要对mysql的配置进行修改
完整文件:
transport.type=tcp transport.server=nio transport.heartbeat=true transport.enableclientbatchsendrequest=true transport.threadfactory.bossthreadprefix=nettyboss transport.threadfactory.workerthreadprefix=nettyservernioworker transport.threadfactory.serverexecutorthreadprefix=nettyserverbizhandler transport.threadfactory.sharebossworker=false transport.threadfactory.clientselectorthreadprefix=nettyclientselector transport.threadfactory.clientselectorthreadsize=1 transport.threadfactory.clientworkerthreadprefix=nettyclientworkerthread transport.threadfactory.bossthreadsize=1 transport.threadfactory.workerthreadsize=default transport.shutdown.wait=3 service.vgroupmapping.my_test_tx_group=default service.default.grouplist=127.0.0.1:8091 service.enabledegrade=false service.disableglobaltransaction=false client.rm.asynccommitbufferlimit=10000 client.rm.lock.retryinterval=10 client.rm.lock.retrytimes=30 client.rm.lock.retrypolicybranchrollbackonconflict=true client.rm.reportretrycount=5 client.rm.tablemetacheckenable=false client.rm.tablemetacheckerinterval=60000 client.rm.sqlparsertype=druid client.rm.reportsuccessenable=false client.rm.sagabranchregisterenable=false client.rm.tccactioninterceptororder=-2147482648 client.tm.commitretrycount=5 client.tm.rollbackretrycount=5 client.tm.defaultglobaltransactiontimeout=60000 client.tm.degradecheck=false client.tm.degradecheckallowtimes=10 client.tm.degradecheckperiod=2000 client.tm.interceptororder=-2147482648 store.mode=file store.lock.mode=file store.session.mode=file store.publickey=xx store.file.dir=file_store/data store.file.maxbranchsessionsize=16384 store.file.maxglobalsessionsize=512 store.file.filewritebuffercachesize=16384 store.file.flushdiskmode=async store.file.sessionreloadreadsize=100 store.db.datasource=druid store.db.dbtype=mysql store.db.driverclassname=com.mysql.jdbc.driver store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useunicode=true&rewritebatchedstatements=true store.db.user=root store.db.password=123456 store.db.minconn=5 store.db.maxconn=30 store.db.globaltable=global_table store.db.branchtable=branch_table store.db.querylimit=100 store.db.locktable=lock_table store.db.maxwait=5000 store.redis.mode=single store.redis.single.host=127.0.0.1 store.redis.single.port=6379 store.redis.sentinel.mastername=xx store.redis.sentinel.sentinelhosts=xx store.redis.maxconn=10 store.redis.minconn=1 store.redis.maxtotal=100 store.redis.database=0 store.redis.password=xx store.redis.querylimit=100 server.recovery.committingretryperiod=1000 server.recovery.asyncommittingretryperiod=1000 server.recovery.rollbackingretryperiod=1000 server.recovery.timeoutretryperiod=1000 server.maxcommitretrytimeout=-1 server.maxrollbackretrytimeout=-1 server.rollbackretrytimeoutunlockenable=false server.distributedlockexpiretime=10000 client.undo.datavalidation=true client.undo.logserialization=jackson client.undo.onlycareupdatecolumns=true server.undo.logsavedays=7 server.undo.logdeleteperiod=86400000 client.undo.logtable=undo_log client.undo.compress.enable=true client.undo.compress.type=zip client.undo.compress.threshold=64k log.exceptionrate=100 transport.serialization=seata transport.compressor=none metrics.enabled=false metrics.registrytype=compact metrics.exporterlist=prometheus metrics.exporterprometheusport=9898
在conf目录中,使用git bash进入命令行,输入
sh nacos-config.sh 127.0.0.1
这是对seata进行初始化配置,上图表示所有配置都成功设置了
在nacos中可以看到出现了seata相关的配置
接下来在seata数据库中,新建三个表
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`) ); 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`) ); 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`) );
在项目相关的数据库中,新建表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;
最后在bin目录中,启动命令行,执行seata-server.bat 启动seata服务
项目应用seata
springcloud项目中有两个服务:订单服务和库存服务,基本业务是:
- 购买商品
- 插入订单
- 减少库存
订单详情表
drop table if exists `tb_order_detail`; create table `tb_order_detail` ( `id` bigint(20) not null auto_increment comment '订单详情id ', `order_id` bigint(20) not null comment '订单id', `sku_id` bigint(20) not null comment 'sku商品id', `num` int(11) not null comment '购买数量', `title` varchar(256) not null comment '商品标题', `own_spec` varchar(1024) default '' comment '商品动态属性键值集', `price` bigint(20) not null comment '价格,单位:分', `image` varchar(128) default '' comment '商品图片', primary key (`id`), key `key_order_id` (`order_id`) using btree ) engine=myisam auto_increment=131 default charset=utf8 comment='订单详情表';
库存表
drop table if exists `tb_stock`; create table `tb_stock` ( `sku_id` bigint(20) not null comment '库存对应的商品sku id', `seckill_stock` int(9) default '0' comment '可秒杀库存', `seckill_total` int(9) default '0' comment '秒杀总数量', `stock` int(9) not null comment '库存数量', primary key (`sku_id`) ) engine=myisam default charset=utf8 comment='库存表,代表库存,秒杀库存等信息';
父项目定义了springboot、springcloud、springcloud-alibaba的版本
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.3.10.release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-alibaba-dependencies</artifactid> <version>0.9.0.release</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupid>com.alibaba.cloud</groupid> <artifactid>spring-cloud-alibaba-dependencies</artifactid> <version>2.2.1.release</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>hoxton.sr8</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement>
子项目的依赖定义了nacos和seata客户端
<dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope> </dependency> <dependency> <groupid>com.baomidou</groupid> <artifactid>mybatis-plus-boot-starter</artifactid> <version>3.3.2</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-alibaba-nacos-discovery</artifactid> </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> <dependency> <groupid>io.seata</groupid> <artifactid>seata-spring-boot-starter</artifactid> <version>1.2.0</version> </dependency>
子项目配置文件
完整配置
server: port: 8001 spring: application: name: stock-service cloud: nacos: discovery: server-addr: localhost:8848 alibaba: seata: enabled: true enable-auto-data-source-proxy: true tx-service-group: my_test_tx_group registry: type: nacos nacos: application: seata-server server-addr: 127.0.0.1:8848 username: nacos password: nacos config: type: nacos nacos: server-addr: 127.0.0.1:8848 group: seata_group username: nacos password: nacos service: vgroup-mapping: my_test_tx_group: default disable-global-transaction: false client: rm: report-success-enable: false datasource: driver-class-name: com.mysql.cj.jdbc.driver url: jdbc:mysql://localhost:3306/eshop?servertimezone=utc&useunicode=true&usessl=false&characterencoding=utf8 username: root password: 123456
库存服务定义了减库存的方法
@restcontroller public class stockcontroller { @autowired private istockservice stockservice; @putmapping("/stock") public responseentity<stock> reduceskustock(@requestparam("skuid")long skuid, @requestparam("number")integer number){ stock stock = stockservice.getbyid(skuid); if(stock.getstock() < number){ throw new runtimeexception("库存不足,skuid:" + skuid); } stock.setstock(stock.getstock() - number); stockservice.updatebyid(stock); return responseentity.ok(stock); } }
订单服务在插入订单后,使用feign调用了减库存的服务
@service public class orderdetailserviceimpl extends serviceimpl<orderdetailmapper, orderdetail> implements iorderdetailservice { //库存服务feign @autowired private stockfeignclient stockfeignclient; // @transactional @globaltransactional(rollbackfor = {exception.class}) @override public void makeorder(orderdetail orderdetail) { this.save(orderdetail); //保存订单 int x = 11 / 0; //抛出异常 //减库存 stockfeignclient.reduceskustock(orderdetail.getskuid(),orderdetail.getnum()); } }
插订单和减库存属于两个服务,传统的@transactional已经不能保证它们的原子性了
这里使用了seata提供的@globaltransactional全局事务注解,出现任何异常后都能实现业务回滚。
测试用例:
@runwith(springrunner.class) @springboottest public class orderserviceapplicationtests { @autowired private iorderdetailservice orderdetailservice; @test public void testorder() { orderdetail orderdetail = new orderdetail(); orderdetail.setnum(100); orderdetail.setorderid(9999l); orderdetail.setprice(9999l); orderdetail.setskuid(27359021728l); orderdetail.settitle(uuid.randomuuid().tostring()); orderdetailservice.makeorder(orderdetail); } }
运行后看到启动了全局事务,发生异常后,两个服务也都能成功回滚。
以上就是springcloud alibaba使用seata 分布式事务的详细内容,更多关于springcloud alibaba分布式事务的资料请关注其它相关文章!
上一篇: 银耳汤要熬多长时间
推荐阅读
-
springcloud微服务分布式事务处理方案-seata
-
详解SpringCloud-Alibaba-Seata分布式事务
-
springboot cloud使用eureka整合分布式事务组件Seata 的方法
-
SpringCloud Alibaba使用Seata处理分布式事务的技巧
-
mysql事务处理机制的使用技巧步骤
-
mysql事务处理机制的使用技巧步骤
-
PHP实例分析了mysql事务处理的使用技巧
-
详解SpringCloud-Alibaba-Seata分布式事务
-
PHP实例分析了mysql事务处理的使用技巧
-
springboot cloud使用eureka整合分布式事务组件Seata 的方法