目录
2.在需要添加分布式事务的服务中添加seata配置(application.yml)
3.在需要添加分布式事务的接口添加注解(@GlobalTransactional)
一、docker安装seata-server1.4.0
1.seata-server配置
- seata-server数据库初始化:global_table、branch_table、lock_table
-- the table to store GlobalSession data
drop table if exists `global_table`;
create table `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`)
);
-- the table to store BranchSession data
drop table if exists `branch_table`;
create table `branch_table` (
`branch_id` bigint not null,
`xid` varchar(128) not null,
`transaction_id` bigint ,
`resource_group_id` varchar(32),
`resource_id` varchar(256) ,
`lock_key` varchar(128) ,
`branch_type` varchar(8) ,
`status` tinyint,
`client_id` varchar(64),
`application_data` varchar(2000),
`gmt_create` datetime,
`gmt_modified` datetime,
primary key (`branch_id`),
key `idx_xid` (`xid`)
);
-- the table to store lock data
drop table if exists `lock_table`;
create table `lock_table` (
`row_key` varchar(128) not null,
`xid` varchar(96),
`transaction_id` long ,
`branch_id` long,
`resource_id` varchar(256) ,
`table_name` varchar(32) ,
`pk` varchar(36) ,
`gmt_create` datetime ,
`gmt_modified` datetime,
primary key(`row_key`)
);
- 修改config.txt文件内容,使用mysql进行事务日志存储
service.vgroupMapping.workbench-system-group=default
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=utf8
store.db.user=root
store.db.password=root
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
- 使用nacos-config.sh将config.txt上传到nacos配置列表
sh nacos-config.sh -h nacos-serverIP地址 -p nacos-server端口8848 -g seata的分组SEATA_GROUP
- 修改registry.conf文件内容,使用nacos作为seata-server的注册、配置中心
registry {
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "127.0.0.1:8848"
group = "SEATA_GROUP"
namespace = "public"
cluster = "default"
username = "nacos"
password = "nacos"
}
}
config {
type = "nacos"
nacos {
serverAddr = "127.0.0.1:8848"
namespace = "public"
group = "SEATA_GROUP"
username = "nacos"
password = "nacos"
}
}
- 修改file.conf文件内容,主要配置seata-server的log存储方式
## transaction log store, only used in seata-server
store {
mode = "db"
db {
datasource = "druid"
dbType = "mysql"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true"
user = "root"
password = "root"
minConn = 1
maxConn = 3
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
}
- 将registry.conf和file.conf文件远程至服务器的自定义文件夹下
2.docker运行seata-server容器
- docker拉取seata-server1.4.0官方镜像,启动seata容器
docker pull seataio/seata-server:1.4.0
docker run --name seata -p 8091:8091\
-e SEATA_IP=127.0.0.1\
-e STORE_MODE=db\
-e SEATA_CONFIG_NAME=file:/root/seata-config/registry\
-v /mydata/seata/conf:/root/seata-config\
-d seataio/seata-server:1.4.0
二、spring cloud + seata1.4.0
- spring boot(2.3.5.RELEASE)+ spring cloud(Hoxton.RELEASE)
1.添加seata1.4.0依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.2.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.0</version>
</dependency>
2.在需要添加分布式事务的服务中添加seata配置(application.yml)
seata:
enabled: true
application-id: ${spring.application.name}
#seata-server中service.vgroupMapping.workbench-system-group=default保持一致
tx-service-group: workbench-system-group
enable-auto-data-source-proxy: true
service:
vgroup-mapping:
workbench-system-group: default
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
username: "nacos"
password: "nacos"
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
username: "nacos"
password: "nacos"
cluster: default
3.为需要添加分布式事务的服务添加客户端的SQL(undo_log.sql)
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) NOT NULL,
`context` varchar(128) NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime NOT NULL,
`log_modified` datetime NOT NULL,
`ext` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
4.在需要添加分布式事务的接口添加注解(@GlobalTransactional)
@GlobalTransactional(name = "user-registry", rollbackFor = Exception.class)
public boolean register(UserParam userParam) {}