SpringBoot 项目适配分布式事务处理框架Seata 基于DB (mysql)和注册中心Nacos及其遇到问题
Seata Server安装文件
seata-1.4.1.zip
seata-server-1.4.1.tar.gz
地址:https://github.com/seata/seata
Seata Server安装配置
1.解压seata-1.4.1.zip
2.修改seata/conf/file.conf
3.修改seata/conf/registry.conf
3. 数据库配置(MySQL)
1.创建数据库名:seata
2.创建如下表:
-- -------------------------------- 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_gmt_modified_status` (`gmt_modified`, `status`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
-- 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 = utf8;
-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
`row_key` VARCHAR(128) NOT NULL,
`xid` VARCHAR(96),
`transaction_id` BIGINT,
`branch_id` BIGINT NOT NULL,
`resource_id` VARCHAR(256),
`table_name` VARCHAR(32),
`pk` VARCHAR(36),
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`row_key`),
KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
4.启动seataMySQL 数据库配置
进入seata/bin下执行
./seata-server.sh &
显示如下字样代表成功
5.Nacos 配置中心配置
解压seata-server-1.4.1.tar.gz
解压后配置目录seata-1.4.1/script/config-center/config.txt
2.再进入目录seata-1.4.1/script/config-center/nacos/
此处nacos默认你的nacos账户密码都是nacos
sh nacos-config.sh -h {nacos_ip} -p 8848 -g SEATA_GROUP -t {nacos_namespace} -u nacos -w nacos
这样就将 seata 相关配置批量的添加到 nacos 服务器上了。
Springboot工程
1.pom依赖
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.2
</version>
</dependency>
<!-- DB -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
<!--seata-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<exclusions>
<!-- 排除依赖seata-spring-boot-starter 不然或报错 SeataDataSourceBeanPostProcessor异常 -->
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
2.yml配置
spring:
#DB配置
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://ip:3306/dbname
username: root
password: root
#分布式事务配置
seata:
enabled: true
application-id: ${spring.application.name}
tx-service-group: my_test_tx_group
service:
vgroup-mapping:
my_test_tx_group: default
grouplist:
default: {seata_server_ip}:8091
enable-degrade: false
disable-global-transaction: false
config:
type: nacos
nacos:
serverAddr: {nacos_ip}:8848
group: SEATA_GROUP
userName: nacos
password: nacos
namespace: nacos_namespace
cluster: default
registry:
type: nacos
nacos:
application: seata-server # 此处名称seata-server application一致
server-addr: {nacos_ip}:8848
userName: nacos
password: nacos
namespace: nacos_namespace
cluster: default
3.在业务数据库中新增创建 undo_log 表作为数据回滚作用
-- for AT mode you must to init this sql for you business database. the seata server not need it.
CREATE TABLE IF NOT EXISTS `undo_log`
(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'increment id',
`branch_id` BIGINT(20) NOT NULL COMMENT 'branch transaction id',
`xid` VARCHAR(100) NOT NULL COMMENT 'global transaction id',
`context` VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
`rollback_info` LONGBLOB NOT NULL COMMENT 'rollback info',
`log_status` INT(11) NOT NULL COMMENT '0:normal status,1:defense status',
`log_created` DATETIME NOT NULL COMMENT 'create datetime',
`log_modified` DATETIME NOT NULL COMMENT 'modify datetime',
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';
4.最后在需要的代码方法上增加@GlobalTransactional即可
@GlobalTransactional(name = “test-service”,rollbackFor = Exception.class)
5.个人遇到坑,学习
https://github.com/seata/seata/issues/3547