Springboot+springcloudAlibaba整合seata1.4
Springboot+springcloudAlibaba整合seata1.4
技术选型
框架 | 版本 |
---|---|
spring boot | 2.2.2.RELEASE |
spring-cloud-alibaba | 2.2.3.RELEASE |
mybatis-plus | 3.4.0 |
nacos | 2.0.3 |
seata | 1.4.0 |
准备环境
准备文件
准备数据库文件
修改配置
- 将config.txt放在与bin同级的目录
- 将nacos-config.sh放在conf目录下面
- 修改conf/file.conf
如果数据库为8.0在seata/lib/jdbc中添加相关jar包
- 修改conf/registry.conf
- 修改config.txt
环境搭建
- 启动nacos
- 在conf目录下点击nacos-config.sh或者右键点击 Git Bash Here(前提有git环境)使用命令 sh nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -u nacos -w nacos看到以下配置为推送成功
- 启动seata (window下单击seata/bin/seata-server.bat即可),启动后看nacos的服务列表
项目整合
- 将registry.conf复制到resources目录下,与application.yml同级
- pom.xml添加seata依赖
<!-- seata-->
<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.4.0</version>
</dependency>
- application.yml添加seata配置
seata:
tx-service-group: my_test_tx_group #这里要特别注意和nacos中配置的要保持一直
config:
type: nacos
nacos:
#namespace: #2.2中配置所在命名空间ID,入未配置 默认public空间
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
userName: nacos
password: nacos
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
#namespace: #2.2中配置所在命名空间ID,入未配置 默认public空间
userName: nacos
password: nacos
service:
vgroup-mapping:
my_test_tx_group: default #这里要特别注意和nacos中配置的要保持一直
- 启动项目
这样即为配置成功
使用seata做全局事务控制
假设A服务调用B服务,那么就在A服务的方法上面加入,B服务上面不用加
@GlobalTransactional(rollbackFor = Exception.class)
测试
A服务
添加插入方法
@ApiOperation(value = "添加数据")
@PostMapping("add")
@GlobalTransactional(rollbackFor = Exception.class)
public R add() throws Exception {
Boolean save = ucenterMemberService.UcenterAdd();
return R.ok().data("code", save);
}
Service层(produceService.add())为B服务的新增方法
@Override
public Boolean UcenterAdd() {
UcenterMember ucenterMember = new UcenterMember().setOpenid(RandomUtils.getFourBitRandom()).setMobile(RandomUtils.getFourBitRandom()).setNickname(RandomUtils.getFourBitRandom()).setSex(1);
boolean save = this.save(ucenterMember);
R add = produceService.add();
return save;
}
运行方法
A服务
2021-09-02 14:31:21.591 INFO 38724 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty : Flipping property: service-produce.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2021-09-02 14:31:21.701 INFO 38724 --- [nio-8120-exec-7] i.seata.tm.api.DefaultGlobalTransaction : [10.111.26.200:8091:176702167442030592] commit status: Committed
2021-09-02 14:31:22.301 INFO 38724 --- [h_RMROLE_1_1_16] i.s.c.r.p.c.RmBranchCommitProcessor : rm client handle branch commit process:xid=10.111.26.200:8091:176702167442030592,branchId=176702169723731969,branchType=AT,resourceId=jdbc:mysql://localhost:3306/service_consumers,applicationData=null
2021-09-02 14:31:22.306 INFO 38724 --- [h_RMROLE_1_1_16] io.seata.rm.AbstractRMHandler : Branch committing: 10.111.26.200:8091:176702167442030592 176702169723731969 jdbc:mysql://localhost:3306/service_consumers null
2021-09-02 14:31:22.308 INFO 38724 --- [h_RMROLE_1_1_16] io.seata.rm.AbstractRMHandler : Branch commit result: PhaseTwo_Committed
B服务
2021-09-02 14:31:22.327 INFO 38648 --- [h_RMROLE_1_1_16] i.s.c.r.p.c.RmBranchCommitProcessor : rm client handle branch commit process:xid=10.111.26.200:8091:176702167442030592,branchId=176702175260213249,branchType=AT,resourceId=jdbc:mysql://localhost:3306/service_produce,applicationData=null
2021-09-02 14:31:22.346 INFO 38648 --- [h_RMROLE_1_1_16] io.seata.rm.AbstractRMHandler : Branch committing: 10.111.26.200:8091:176702167442030592 176702175260213249 jdbc:mysql://localhost:3306/service_produce null
2021-09-02 14:31:22.348 INFO 38648 --- [h_RMROLE_1_1_16] io.seata.rm.AbstractRMHandler : Branch commit result: PhaseTwo_Committed
此时给A服务的方法制造异常
@ApiOperation(value = "添加数据")
@PostMapping("add")
@GlobalTransactional(rollbackFor = Exception.class)
public R add() throws Exception {
Boolean save = ucenterMemberService.UcenterAdd();
if (1 == 1) {
throw new Exception("制造异常");
}
return R.ok().data("code", save);
}
再次启动方法
A服务
rm handle branch rollback process:xid=10.111.26.200:8091:176703109113610240,branchId=176703109168136193,branchType=AT,resourceId=jdbc:mysql://localhost:3306/service_consumers,applicationData=null
2021-09-02 14:35:04.267 INFO 38724 --- [h_RMROLE_1_2_16] io.seata.rm.AbstractRMHandler : Branch Rollbacking: 10.111.26.200:8091:176703109113610240 176703109168136193 jdbc:mysql://localhost:3306/service_consumers
2021-09-02 14:35:04.401 INFO 38724 --- [h_RMROLE_1_2_16] i.s.r.d.undo.AbstractUndoLogManager : xid 10.111.26.200:8091:176703109113610240 branch 176703109168136193, undo_log deleted with GlobalFinished
2021-09-02 14:35:04.402 INFO 38724 --- [h_RMROLE_1_2_16] io.seata.rm.AbstractRMHandler : Branch Rollbacked result: PhaseTwo_Rollbacked
2021-09-02 14:35:04.480 INFO 38724 --- [io-8120-exec-10] i.seata.tm.api.DefaultGlobalTransaction : [10.111.26.200:8091:176703109113610240]
B服务
2021-09-02 14:35:04.411 INFO 38648 --- [h_RMROLE_1_2_16] i.s.c.r.p.c.RmBranchRollbackProcessor : rm handle branch rollback process:xid=10.111.26.200:8091:176703109113610240,branchId=176703109281382401,branchType=AT,resourceId=jdbc:mysql://localhost:3306/service_produce,applicationData=null
2021-09-02 14:35:04.412 INFO 38648 --- [h_RMROLE_1_2_16] io.seata.rm.AbstractRMHandler : Branch Rollbacking: 10.111.26.200:8091:176703109113610240 176703109281382401 jdbc:mysql://localhost:3306/service_produce
2021-09-02 14:35:04.465 INFO 38648 --- [h_RMROLE_1_2_16] i.s.r.d.undo.AbstractUndoLogManager : xid 10.111.26.200:8091:176703109113610240 branch 176703109281382401, undo_log deleted with GlobalFinished
2021-09-02 14:35:04.465 INFO 38648 --- [h_RMROLE_1_2_16] io.seata.rm.AbstractRMHandler : Branch Rollbacked result: PhaseTwo_Rollbacked
会发现状态为Rollbacked
注意事项
1.如果给B服务手动制造异常而A服务是正常这个时候想数据回滚请勿将异常进行捕捉,否则seata认为你已将异常处理,就不会回滚数据了我之前就是配置了@ControllerAdvice导致数据没有回滚
技术有限,轻喷