Seata与Nacos的组合使用
项目版本
- JDK版本:17
- IDEA版本:2026.1
- Maven版本:3.9.10
- Nacos版本:Windows-2.5.2
- Seata版本:Windows-1.6.1
- SQL版本:阿里云RDS-MySQL8.x
- 操作系统:Windows11
项目中各个依赖版本:
<!-- SpringBoot版本 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<!-- SpringBoot版本 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2022.0.0.0-RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- bootstrap版本 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>4.0.0</version>
</dependency>
<!-- Nacos配置中心版本 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2022.0.0.0-RC1</version>
</dependency>
<!-- Nacos服务发现版本 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2022.0.0.0-RC1</version>
</dependency>
<!-- Seata版本 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2022.0.0.0-RC1</version>
</dependency>
编辑Seata配置文件
找到Seata所在的目录,找到目录下的conf文件夹。博主当前是在D:\Develop\Alibaba\Seata\seata-1.6.1\conf

打开application.yml文件。(application.example.yml是application.yml的模版文件,可参照此文件编写application.yml中的内容)

以下application.yml内容为博主文件中的内容,仅供参考。
# 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:
# Seata 应用端口
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:
# 配置中心类型,默认file(本地),可选nacos, consul, apollo, zk, etcd3
# 在 Seata 中的作用是:集中化配置管理(统一维护 Seata 规则)
config:
# support: nacos, consul, apollo, zk, etcd3
# type: file
type: nacos
nacos:
server-address: 127.0.0.1:8848
namespace: train
group: SEATA_GROUP
data-id: seataServer.yaml
# 服务发现类型,默认file(本地),可选nacos, consul, apollo, zk, etcd3
# 在 Seata 中的作用是:服务注册与发现(TM/RM 如何找到 TC)
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
# type: file
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
namespace: train
cluster: default
# 全局事务管理信息存储地方 file(本地) db(数据库) redis
store:
# support: file 、 db 、 redis
mode: db
# 若没有显式声明服务运行端口,默认为${server.port} + 1000,即7091+1000=8091
# 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
设置完毕后保存,即可去到bin文件夹下,在资源导航地址栏中输入cmd,使用命令行启动Seata

cmd seata-server.bat [options]
options的参数有以下几个:
| option | 含义 | 默认值 |
|---|---|---|
| -h | 该地址公开给注册中心,其他服务可以通过该ip访问服务器 | 0.0.0.0 |
| -p | 服务监听端口(注意:不是设置应用启动端口) | 8091 |
| -m | 日志信息与全局事务管理信息存放位置 | file(本地) |
| -help | 命令帮助 | - |
配置数据库
创建数据库,数据库名称可自定义,后续在Nacos中的配置文件中需要填写清楚。创建数据库后执行Seata根目录\script\server\db中的mysql.sql文件

执行完毕后,数据库中会有以下表:

编写 TM(服务模块) 中的配置文件
在需要使用Seata中的项目模块下的bootstrap.yaml文件中编写以下内容:
spring:
# 注册中心中显示的服务名字
application:
name: member
# 启动环境,Nacos 会根据环境读取不同的配置信息
profiles:
active: dev
cloud:
# 用于 member 服务注册到 Nacos 中并获取配置信息
# 当前 Nacos 配置信息服务于当前模块
nacos:
config:
# Nacos 服务地址(默认:127.0.0.1:8848)
server-addr: 127.0.0.1:8848
# 配置中心文件后缀,默认为 properties
file-extension: yaml
# 需要使用的命名空间-命名空间ID
namespace: train
discovery:
server-addr: 127.0.0.1:8848
namespace: train
seata:
# 事务组名称
tx-service-group: train-group
# Seata 中的 Nacos 信息用于协助当前服务发现 TC(Seata服务端)与获取 Seata 所需要的配置信息
registry:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
namespace: train
group: SEATA_GROUP
application: seata-server
config:
type: nacos
data-type: yaml
nacos:
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
namespace: train
data-id: seataServer.yaml
Seata 的 Nacos 配置信息与当前模块的 Nacos 配置信息具有不同作用,所以当你的模块服务需要注册到 Nacos 时且需要使用 Seata 搭配 Nacos 时两个 Nacos 配置信息都需要显式配置。
编写 Nacos 中的配置信息
博主所使用的命名空间为 train。随后在当前命名空间下创建 seataServer.yaml 配置信息,此配置信息的 DataId 与 group 为 Seata 的 application.yml 与 自己项目模块中的 bootstrap.yaml 所共同定义的 DataId 和 group。

此配置内容格式需要参考Seata根目录\script\config-center\config.txt内的内容!格式错误的情况下,是无法使用Seata的全局事务管理功能。配置内容为以下内容:
service:
vgroupMapping:
train-group: default
store:
mode: db
db:
dbType: mysql
driverClassName: com.mysql.cj.jdbc.Driver
url: 自己数据库的地址
user: 用户名
password: 密码
查看日志

当出现以上信息时即代表完成 TM 与 RM 的注册,即可使用 Seata 的全局事务管理功能。
530



被折叠的 条评论
为什么被折叠?



