前言
这套是对应SpringCloud Alibaba2021.0.4.0版本的nacos和seata搭建教程,对应版本依赖关系请访问:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E查阅
首先SpringCloud Alibaba2021.0.4.0对应需要的nacos和seata版本分别为:nacos2.0.4和seata1.5.2
步骤一:安装宝塔面板
此处不过多介绍略过.....
步骤二:搭建nacos2.0.4
直接在linux终端执行:
docker run --name nacos --env MODE=standalone -d -p 7848:7848 -p 9848:9848 -p 9849:9849 -p 8848:8848 nacos/nacos-server:v2.0.4
快速安装nacos2.0.4版本,默认账号密码为nacos/nacos,安装完成请及时修改。这里用的是单机启动模式,并且没有映射对外日志目录,这个不过多讲解有需要自行百度
步骤三:搭建seata1.5.2分布式事务
首先在linux服务器根目录分别创建:/data/seata/conf/application.yml文件和/data/seata/logs目录
application.yml内容如下:
server:
port: 7091
spring:
application:
name: seata-server
logging:
level:
com.alibaba.nacos.client.config.*: WARN
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:
tx-service-group: test_tx_group
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
namespace: ygxc-dev
group: SEATA_GROUP
data-id: seataServer.properties
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
namespace: ygxc-dev
cluster: default
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
其中nacos的信息换成自己服务器的
1.1 配置seata nacos文件
登录nacos,在自己配置的命名空间(如我的是ygxc-dev)创建一个seataServer.properties文件分组为SEATA_GROUP
文件内容如下:
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true&useSSL=false
store.db.user=seata
store.db.password=123456
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
#Transaction rule configuration, only for the server
server.recovery.committingRetryPeriod=1000
server.recovery.asynCommittingRetryPeriod=1000
server.recovery.rollbackingRetryPeriod=1000
server.recovery.timeoutRetryPeriod=1000
server.maxCommitRetryTimeout=-1
server.maxRollbackRetryTimeout=-1
server.rollbackRetryTimeoutUnlockEnable=false
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
# 客户端与服务端传输方式
transport.serialization=seata
transport.compressor=none
# 关闭metrics功能,提高性能
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898
1.2 配置seata分组
在seata命名空间下新建 service.vgroupMapping.test_tx_group配置,GROUP:SEATA_GROUP,内容为default
1.3 安装seata的mysql数据库
在宝塔安装mysql数据库5.6和5.7均可
创建一个数据库名为seata的数据库
账户名和密码自行随意设置,用宝塔默认的都可
然后利用宝塔的phpmyadmin登录mysql选中seata数据库执行以下sql创建seata必要的回滚事务处理表
-- -------------------------------- The script used when storeMode is 'db' --------------------------------
-- the table to store GlobalSession data
CREATE TABLE IF NOT EXISTS `global_table`
(
`xid` VARCHAR(128) NOT NULL,
`transaction_id` BIGINT,
`status` TINYINT NOT NULL,
`application_id` VARCHAR(32),
`transaction_service_group` VARCHAR(32),
`transaction_name` VARCHAR(128),
`timeout` INT,
`begin_time` BIGINT,
`application_data` VARCHAR(2000),
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`xid`),
KEY `idx_status_gmt_modified` (`status` , `gmt_modified`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
-- the table to store BranchSession data
CREATE TABLE IF NOT EXISTS `branch_table`
(
`branch_id` BIGINT NOT NULL,
`xid` VARCHAR(128) NOT NULL,
`transaction_id` BIGINT,
`resource_group_id` VARCHAR(32),
`resource_id` VARCHAR(256),
`branch_type` VARCHAR(8),
`status` TINYINT,
`client_id` VARCHAR(64),
`application_data` VARCHAR(2000),
`gmt_create` DATETIME(6),
`gmt_modified` DATETIME(6),
PRIMARY KEY (`branch_id`),
KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
`row_key` VARCHAR(128) NOT NULL,
`xid` VARCHAR(128),
`transaction_id` BIGINT,
`branch_id` BIGINT NOT NULL,
`resource_id` VARCHAR(256),
`table_name` VARCHAR(32),
`pk` VARCHAR(36),
`status` TINYINT NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`row_key`),
KEY `idx_status` (`status`),
KEY `idx_branch_id` (`branch_id`),
KEY `idx_xid_and_branch_id` (`xid` , `branch_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
CREATE TABLE IF NOT EXISTS `distributed_lock`
(
`lock_key` CHAR(20) NOT NULL,
`lock_value` VARCHAR(20) NOT NULL,
`expire` BIGINT,
primary key (`lock_key`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);
这里注意!需要用到的功能库需要有“undo_log”表,也就是说你另一个数据库是需要用到seata的请添加下面这个表,否则seata无法生效
CREATE TABLE `undo_log` (
`id` bigint NOT NULL AUTO_INCREMENT,
`branch_id` bigint NOT NULL,
`xid` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`context` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int NOT NULL,
`log_created` datetime NOT NULL,
`log_modified` datetime NOT NULL,
`ext` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB;
1.4 执行脚本安装seata
docker run --name seata \
--restart always \
-p 7091:7091 \
-p 8091:8091 \
-v /data/seata/conf/application.yml:/seata-server/resources/application.yml \
-v /data/seata/logs:/root/logs \
-e SEATA_IP=120.78.1.1 \
-e SEATA_PORT=8091 \
-d seataio/seata-server:1.5.2
值得注意的是其中SEATA_IP=120.78.1.1是公网ip,需要外部可以访问到seata服务的
随后seata就安装成功并且成功注册到nacos中,其中请注意自己服务器是否有开放对应的端口,特别是阿里云和腾讯云的服务器安全组是否放行,分别放行7091、8091、7848、9848、9849、8848端口(自己的业务请不要占用这些端口)
另外也可以浏览器访问:http://127.0.0.1:7091/进入seata界面密码在application.yml中console中配置
一些注意事项
1、在nacos创建客户端的配置seata-config.properties,并被客户端引用,和客户端其他配置放一个命名空间和分组即可
seata.application-id=${spring.application.name}
seata.tx-service-group=test_tx_group
seata.registry.type=nacos
seata.registry.nacos.server-addr=127.0.0.1:8848
seata.registry.nacos.group=SEATA_GROUP
seata.registry.nacos.namespace=bd9db9e4-c709-4706-a3c7-9f0a5b4bb3f9
seata.registry.nacos.application=seata-server
seata.registry.nacos.cluster=default
seata.config.type=nacos
seata.config.nacos.server-addr=127.0.0.1:8848
seata.config.nacos.group=SEATA_GROUP
seata.config.nacos.namespace=bd9db9e4-c709-4706-a3c7-9f0a5b4bb3f9
seata.service.vgroup-mapping.test_tx_group=default
2、在客户端需要用到的模块加入com.alibaba.cloud:spring-cloud-starter-alibaba-seata,不需要再额外引入seata-starter模块,都加入可能会出现分布式事务失效的bug。这里注意SpringBoot和SpringCloud相关版本,是否兼容(查看1.中的文档),选择合适的版本或者适当调整seata版本。不需要额外配置,spring-cloud-starter-alibaba-seata会自动进行数据源代理。如果需要进行测试可以使用RootContext.getXID()方法在各个服务中获取xid,如果xid不一致或者为null,则会出现分布式事务失效的情况,需要排查问题。
3、seata.service.vgroup-mapping.test_tx_group的值实际上就是服务端配置中cluster的值,即default
4、使用上只需要在最外层添加注解@GlobalTransactional即可生效,至于生效的模式和下层包含@Transactional注解时的情况,可以通过查看seata-server日志以及开发环境打断点调试进入seata 7091端口的后台查看了解。
结尾
附赠seata各个版本的sql执行列表:https://gitee.com/seata-io/seata/blob/v1.5.2/script/server/db/mysql.sql
nacos在docker中的版本列表:https://hub.docker.com/r/nacos/nacos-server/tags?page=2