springboot3(cloud 2022.0.0)整合seata1.7.1 (AT模式,不适合高并发)
一、第一步下载对应版本的seata服务
二、修改conf下的application.yml配置
注意:主要是连接nacos的一些配置:注册中心和服务发现的配置
1 # Copyright 1999-2019 Seata.io Group. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 15 server: 16 port: 7091 17 18 spring: 19 application: 20 name: seata-server 21 22 logging: 23 config: classpath:logback-spring.xml 24 file: 25 path: D:\seatalog\seata-server-1.7.0\seata\logs. 26 extend: 27 logstash-appender: 28 destination: 127.0.0.1:4560 29 kafka-appender: 30 bootstrap-servers: 127.0.0.1:9092 31 topic: logback_to_logstash 32 33 console: 34 user: 35 username: seata 36 password: seata 37 seata: 38 config: 39 # support: nacos, consul, apollo, zk, etcd3 40 type: nacos # 指定配置中心为nacos 41 nacos: 42 server-addr: 127.0.0.1:8848 # nacos的ip端口 43 group: DEFAULT_GROUP # 对应的组,默认为DEFAULT_GROUP 44 namespace: e984372a-17f3-44fc-aa0e-7b3c384fe94c # 对应的命名空间,在nacos中配置 45 data-id: seataServer.properties # nacos中存放seata的配置文件,后面会提该文件的使用方式,相当于seata服务启动的时候需要注册到nacos,并使用nacos中的配置文件 46 registry: 47 # support: nacos, eureka, redis, zk, consul, etcd3, sofa 48 type: nacos 49 nacos: 50 application: seata-server 51 server-addr: 127.0.0.1:8848 52 group: DEFAULT_GROUP 53 namespace: 54 cluster: default 55 username: 56 password: 57 context-path: 58 store: 59 # support: file 、 db 、 redis 60 mode: file 61 # server: 62 # service-port: 8091 #If not configured, the default is '${server.port} + 1000' 63 security: 64 secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017 65 tokenValidityInMilliseconds: 1800000 66 ignore: 67 urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.jpeg,/**/*.ico,/api/v1/auth/login
2.1配置好之后创建seata服务连接数据库,库中插入表。在 \seata\script\server\db 下有sql文件
2.2在需要用到seata的业务数据库下创建undo_log表
1 drop table `undo_log`; 2 CREATE TABLE `undo_log` ( 3 `id` bigint(20) NOT NULL AUTO_INCREMENT, 4 `branch_id` bigint(20) NOT NULL, 5 `xid` varchar(100) NOT NULL, 6 `context` varchar(128) NOT NULL, 7 `rollback_info` longblob NOT NULL, 8 `log_status` int(11) NOT NULL, 9 `log_created` datetime NOT NULL, 10 `log_modified` datetime NOT NULL, 11 `ext` varchar(100) DEFAULT NULL, 12 PRIMARY KEY (`id`), 13 UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`) 14 ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2.3:访问nacos看一下seata服务有没有被naocs发现
2.4:为seata创建命名空间,名字要与data-id一致
配置文件在 \seata\script\config-center下有个config.txt复制到naocs配置文件中 主要要修改自己的数据库地址,绑定seata表
三、双击启动bin目录下脚本,seata-server.bat
浏览器访问:http://127.0.0.1:7091 可视化界面
四、整合到项目中导入的依赖:
<!--分布式事务seata--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> </dependency>
在yaml文件中的配置:
1 seata: 2 tx-service-group: gulimall-ware # 事务分组名称,要和服务端对应 3 service: 4 vgroup-mapping: 5 gulimall-ware: default # key是事务分组名称 value要和服务端的机房名称保持一致 6 registry: # 业务服务每一个都需要配置需要在naocs发现seata服务 7 type: nacos 8 nacos: 9 server-addr: 127.0.0.1:8848 10 namespace: public 11 group: DEFAULT_GROUP 12 application: seata-server
启动项目测试
使用方法:在业务总事物方法上加上 @GlobalTransactional
如果回滚失效,基本上是没有获取到XID,查看XID方法: RootContext.getXID()
解决方法:不要导错依赖,使用:spring-cloud-starter-alibaba-seata
或者重写 RequestInterceptor的apply方法 把XID传递到远程服务请求头中。feign底层会丢失所有请求头,创建新客户端进行http调用,所以会有XID为null的问题。