第十二章 Seata快速入门
安装配置步骤
下载地址:https://github.com/seata/seata/releases
我下载的是Seata的1.5.2版本进行演示。
- 将下载的安装包解压到指定目录
- 修改conf目录下的application.yml配置文件
主要修改:
- 配置中心修改为Nacos
- Nacos注册信息
server:
port: 7091
# 服务名
spring:
application:
name: seata-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:
# 设置nacos作为配置中心,namespace可以为默认的public。
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
server-addr: 127.0.0.1:8848
namespace:
group: SEATA_GROUP
username: nacos
password: nacos
registry:
# 设置注册服务的信息
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
namespace:
cluster: default
username: nacos
password: nacos
# server:
# service-port: 8091 #If not configured, the default is '${server.port} + 1000'
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
- 在nacos配置中心中添加配置和修改配置
seata1.5.2的文件夹中为我们提供了一个git文件,可以直接帮我们添加配置到nacos,我们在seata\script\config-center\nacos路径下可以找到该文件,确保nacos已启动,直接点击运行即可。
运行完毕后可以在nacos中看见相关配置项
- 项目中application.yml的seata配置
seata:
# 设置分组,1.5以后版本默认事务分组的由my_test_tx_group 修改为 default_tx_group。
# 这里我选择新建一个my_tx_group分组进行测试
# 事务分组:seata的资源逻辑,可以按微服务的需要,在应用程序(客户端)对自行定义事务分组,每组取一个名字。
tx-service-group: my_tx_group
enabled: true
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
group : SEATA_GROUP
username: nacos
password: nacos
service:
# 配置TC集群名clusterName
vgroup-mapping:
my_tx_group: default
# 配置真实TC服务地址127.0.0.1:8091
grouplist:
default: 127.0.0.1:8091
- 在nacos配置中心添加分组配置
在这搜索service.vgroupMapping,可以看到默认分组的配置
默认TC集群名为default
我们新建一个 service.vgroupMapping.my_tx_group的配置
案例测试
相关代码地址:https://gitee.com/xin-77/spring-cloud-alibaba.git
启动order模块
成功启动后我们可以在这看到seata的相关信息
自己新建的事务分组
启动其它两个模块,成功注册到nacos。
事务成功测试
访问http://localhost:2001/order/create?userId=1&productId=1&count=10&money=100测试成功
查看后台日志
事务失败测试
在account模块添加异常方法
访问http://localhost:2001/order/create?userId=1&productId=1&count=10&money=100测试
事务失败,查看后台日志