Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。
版本说明
下载Seata
根据springcloud alibaba版本说明下载对应的Seata版本,否则可能会出现各种版本不兼容的问题。
下载地址:https://github.com/seata/seata/releases
安装Seata
资源包下载地址:https://github.com/seata/seata/tree/develop/script
以下步骤按照DB存储为例。
- 解压Seata安装包
- 如果使用DB存储,需要在数据库中创建表。(执行资源包中的sql文件script -> server -> db -> mysql.sql)
- 打开配置文件(seata -> conf -> file.conf)
- 修改store.mode=“db”
- 修改数据库连接配置url、username、password
到这里就完成了Seata的基本配置,执行seata/bin/seata-server.bat启动Seata,默认使用本地的配置。
Seata配置Nacos配置中心、注册中心
- 打开配置文件(seata -> conf -> registry.conf)
- 修改registry.type=“nacos”
- 修改注册中心nacos连接配置
- 修改config.type=“nacos”
- 修改配置中心nacos连接配置
- 将资源包script放在seata安装目录下
- 打开配置文件(seata -> script -> config-center -> config.txt)
- 修改store.mode=“db”
- 修改数据库连接配置url、username、password
- 运行脚本就可以将配合同步至nacos
- 在(seata -> script -> config-center -> nacos)目录下运行命令
# 如果nacos不在本地,可以通过参数指定nacos服务的IP、端口、groupId、命名空间
liunx: ./nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t 命名空间
# python脚本中分组是固定写死的,自定义需要修改脚本
python: nacos-config.py localhost:8848 命名空间
执行seata/bin/seata-server.bat再次启动Seata使用就是nacos中的配置了。
Seata在liunx中启动参数
./seata-server.sh -p 8092 -n 1
-h: 注册到注册中心的ip
-p: Server rpc 监听端口
-m: 全局事务会话信息存储模式,file、db、redis,优先读取启动参数 (Seata-Server 1.3及以上版本支持redis)
-n: Server node,多个Server时,需区分各自节点,用于生成不同区间的transactionId,以免冲突
-e: 多环境配置参考 http://seata.io/en-us/docs/ops/multi-configuration-isolation.html
Seata详细参数配置:https://seata.io/zh-cn/docs/user/configurations.html
Springcloud整合Seata客户端
- 添加Maven依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
- 添加application.yml配置
seata:
# 事务分组 config.txt中的service.vgroupMapping.my_test_tx_group
tx-service-group: my_test_tx_group
# seata注册中心配置 参考 seata安装目录 -> conf -> registry.conf
registry:
type: nacos
nacos:
application: seata-server # seata服务名
server-addr: localhost:8848 # nacos服务地址
group: SEATA_GROUP # nacos分组 默认为:SEATA_GROUP
namespace: public # nacos命名空间
cluster: default # 集群名称 需要与config.txt中的service.vgroupMapping.my_test_tx_group的配置一致
username: nacos # nacos用户名
password: nacos # nacos密码
# seata配置中心配置 参考 seata安装目录 -> conf -> registry.conf
config:
type: nacos
nacos:
server-addr: localhost:8848 # nacos服务地址
group: SEATA_GROUP # nacos分组
namespace: public # nacos命名空间
username: nacos # nacos用户名
password: nacos# nacos密码
- 以上内容添加后就可以使用注解@GlobalTransactional处理分布式事务。