@TOC
前言:由于项目上使用微服架构,各服务都有独立的数据库,那么如何保证事务的一致性?而 Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。
一、Seata 版本选择
二、windows 下载安装步骤-安装包方式
1、下载安装包 1.6.1
2、修改 seata/seata/conf/application.yml 内容,由于使用 nacos 作为注册中心主要 seata 的 config、registry、store
修改前:
修改后
seata:
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
server-addr: 127.0.0.1:8848
namespace: 967df0d8-156b-441a-a5ab-615b4a865168
group: MY_GROUP
##if use MSE Nacos with auth, mutex with username/password attribute
#access-key: ""
#secret-key: ""
data-id: seataServer.properties
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
# preferred-networks: 30.240.*
nacos:
application: seata-tc-server
server-addr: 127.0.0.1:8848
group: MY_GROUP
namespace: 967df0d8-156b-441a-a5ab-615b4a865168
#cluster: SH
##if use MSE Nacos with auth, mutex with username/password attribute
#access-key: ""
#secret-key: ""
store:
# support: file 、 db 、 redis
mode: db
db:
datasource: druid
db-type: mysql
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true&serverTimezone=GMT
user: root
password: 123456
min-conn: 5
max-conn: 100
global-table: global_table
branch-table: branch_table
lock-table: lock_table
distributed-lock-table: distributed_lock
query-limit: 100
max-wait: 5000
3、根据 seata/seata/script/server/db/mysql.sql 建数据库表 seata
http://127.0.0.1:7091/#/login
账号:seata 密码:seata
三、linux 下载安装步骤-docker 方式
参考:链接: docker 部署 seata
docker run -d --restart always --name seata-server -p 8091:8091 -p 7091:7091 -v /usr/local/seata/seata-server:/seata-server -e SEATA_PORT=8091 -e SEATA_IP=[自己的ip地址] seataio/seata-server:1.6.1
# 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.
server:
port: 7091
spring:
application:
name: seata-tc-server
logging:
config: classpath:logback-spring.xml
file:
path: ${user.home}/logs/seata
extend:
logstash-appender:
destination: 127.0.0.1:4560
kafka-appender:
bootstrap-servers: 127.0.0.1:9092
topic: logback_to_logstash
console:
user:
username: seata
password: seata
seata:
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
server-addr: 127.0.0.1:8848
namespace: 967df0d8-156b-441a-a5ab-615b4a865168
group: MY_GROUP
##if use MSE Nacos with auth, mutex with username/password attribute
#access-key: ""
#secret-key: ""
data-id: seataServer.properties
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
# preferred-networks: 30.240.*
nacos:
application: seata-tc-server
server-addr: 127.0.0.1:8848
group: MY_GROUP
namespace: 967df0d8-156b-441a-a5ab-615b4a865168
#cluster: SH
##if use MSE Nacos with auth, mutex with username/password attribute
#access-key: ""
#secret-key: ""
store:
# support: file 、 db 、 redis
mode: db
db:
datasource: druid
db-type: mysql
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true&serverTimezone=GMT
user: root
password:123456
min-conn: 5
max-conn: 100
global-table: global_table
branch-table: branch_table
lock-table: lock_table
distributed-lock-table: distributed_lock
query-limit: 100
max-wait: 5000
server:
#service-port: 8091 #If not configured, the default is '${server.port} + 1000'
vgroup-mapping: {<!-- -->"default_tx_group" : "default"}
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
四、Spring Cloud 应用相关配置
seata 版本可以根据自己项目版本进行选择,请查看 版本说明
1、父工程 pom 文件引入
<properties>
<seata.version>1.6.1</seata.version>
</properties>
</dependencies>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>${seata.version}</version>
</dependency>
</dependencies>
2、子工程 pom 文件引入
<!--分布式事务seata-->
<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>
<version>${spring.cloud.alibaba.version}</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
</exclusion>
<exclusion>
<artifactId>seata-spring-boot-starter</artifactId>
<groupId>io.seata</groupId>
</exclusion>
</exclusions>
</dependency>
3、yml 配置
# seata 配置, 代替file.conf和registry.conf配置
# seata 配置, 代替file.conf和registry.conf配置
sfs:
nacos:
server-addr: 127.0.0.1:8848
namespace: 967df0d8-156b-441a-a5ab-615b4a865168
group: MY_GROUP
seata:
enabled: true
application-id : ${spring.application.name}
tx-service-group: default_tx_group
use-jdk-proxy: true
enable-auto-data-source-proxy: true
registry:
type: nacos
nacos:
application: seata-tc-server
server-addr: ${sfs.nacos.server-addr}
namespace: ${sfs.nacos.namespace}
group: ${sfs.nacos.group}
config:
type: nacos
nacos:
server-addr: ${sfs.nacos.server-addr}
namespace: ${sfs.nacos.namespace}
group: ${sfs.nacos.group}
service:
vgroupMapping:
default_tx_group: default
#禁用hystrix导致启动失败
feign:
hystrix:
enabled: false
4、实现类上加上
@GlobalTransactional(rollbackFor = Exception.class)
5、每个数据库上加上 undo_log
五、遇到问题
1、No available service
2023-02-06 09:51:29.885 [TID: N/A] [http-nio-8671-exec-1] INFO io.seata.tm.TransactionManagerHolder - TransactionManager Singleton io.seata.tm.DefaultTransactionManager@254a608b
2023-02-06 09:51:29.900 [TID: N/A] [http-nio-8671-exec-1] WARN c.d.pi.exception.MyExceptionHandler - No available service
io.seata.common.exception.FrameworkException: No available service
at io.seata.core.rpc.netty.AbstractNettyRemotingClient.loadBalance(AbstractNettyRemotingClient.java:266)
解决:逐步排除配置信息,请参考链接: seata 异常信息排除
2、Seata 报错 can not get cluster name in registry config ‘service.vgroupMapping.default
解决:请参考链接: seata 相关问题解决
3、get table meta of the table XXX
error: Failed to fetch schema of XXX
解决:数据表中没有主键,请参考链接: 表设置主键
4、链路中出现某服务报错,各服务不进行回滚
解决:先查看各服务的 XID 是否一致。
使用 spring-cloud-starter-alibaba-seata,已经实现 XID 传递,注意配置中禁用 feign 的 hystrix(也就是不能使用 hystrix,参考链接: hystrix 线程切换导致 threadLocal 丢失问题)
<!--分布式事务seata-->
<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>
<version>${spring.cloud.alibaba.version}</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
</exclusion>
<exclusion>
<artifactId>seata-spring-boot-starter</artifactId>
<groupId>io.seata</groupId>
</exclusion>
</exclusions>
</dependency>
feign:
hystrix:
enabled: false
参考链接: 2022 年 SpringCloud Alibaba Seata1.42 整合 springboot 处理分布式事务排坑版教程
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于