使用阿里seata处理分布式事务(nacos作为注册中心)
# seata相关说明
# nacos 作为注册中心,使用seata
- 版本说明(版本很重要,版本不对应,会导致应用连接不到seata服务器)
spring cloud alibaba 2.2.5.RELEASE
spring boot 2.3.2.RELEASE
spring cloud Hoxton.SR8
seata 1.3.0
1
2
3
4
2
3
4
# 一、启动seata服务器
启动nacos,导入seata_nacos_config.zip。seata_nacos_config.zip可以在百度网盘 (opens new window)下载,密码是
guq9;或者访问示例项目 (opens new window),从doc目录下载。启动seata服务。首先需要修改配置文件
registry.conf,主要修改将type改为nacos,同时修改nacos配置信息。下面是配置内容,需要修改的部分已经用注释标出了。
registry {
# 这里修改为nacos
type = "nacos"
nacos {
# nacos相关配置
application = "seata-server"
serverAddr = "127.0.0.1:8848"
group = "SEATA_GROUP"
namespace = ""
cluster = "default"
username = ""
password = ""
}
eureka {
serviceUrl = "http://localhost:8761/eureka"
application = "default"
weight = "1"
}
redis {
serverAddr = "localhost:6379"
db = 0
password = ""
cluster = "default"
timeout = 0
}
zk {
cluster = "default"
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
consul {
cluster = "default"
serverAddr = "127.0.0.1:8500"
}
etcd3 {
cluster = "default"
serverAddr = "http://localhost:2379"
}
sofa {
serverAddr = "127.0.0.1:9603"
application = "default"
region = "DEFAULT_ZONE"
datacenter = "DefaultDataCenter"
cluster = "default"
group = "SEATA_GROUP"
addressWaitTime = "3000"
}
file {
name = "file.conf"
}
}
config {
# 修改为nacos
type = "nacos"
nacos {
# nacos相关配置
serverAddr = "127.0.0.1:8848"
namespace = ""
group = "SEATA_GROUP"
username = ""
password = ""
}
consul {
serverAddr = "127.0.0.1:8500"
}
apollo {
appId = "seata-server"
apolloMeta = "http://192.168.1.204:8801"
namespace = "application"
}
zk {
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
相当重要的一点,一定要修改nacos配置中的几个项:
- service.default.grouplist 修改为seata服务器的地址和端口号
- service.vgroupMapping.my_test_tx_group 这个相当相当的关键service.vgroupMapping.xxx_group和应用配置文件一定要对应
- store.db.dbType 数据库类型,选择mysql
- store.db.driverClassName 数据库驱动,mysql8使用
com.mysql.cj.jdbc.Driver,mysql5使用com.mysql.jdbc.Driver - store.db.url 数据库地址。seata服务器需要一个数据库,新建名称为
seata的数据库,执行下面的sql语句。
-- -------------------------------- 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;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
假设数据库ip和端口号分别为127.0.0.1、3306。store.db.url 数据库地址为jdbc:mysql://127.0.0.1:3306/seata?useSSL=false&&serverTimezone=GMT%2B8&useInformationSchema=true
- store.db.user 数据库用户名
- store.db.password 数据库密码
配置文件修改完成,nacos配置也修改完成,可以启动seata了
//假设当前在bin目录下,-h 指定seata服务器ip -p 指定seata服务器端口
//windows
.\seata-server.bat -h 127.0.0.1 -p 8091
//linux
./seata-server.bat -h 127.0.0.1 -p 8091
1
2
3
4
5
2
3
4
5
# 二、应用中使用seata
# 添加依赖(涉及到的应用都需要添加)
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-seata</artifactId>
<version>2.2.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>1.3.0</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
所有加入事务的应用(@GlobalTransactional)都必须引入seata的依赖。不引用依赖的应用没有办法回滚。
# 添加配置(涉及到的应用都需要添加)
seata:
enabled: true
application-id: seata-server
#这里的名字与nacos配置中vgroupMapping.my_test_tx_group = "default"相同
tx-service-group: my_test_tx_group
enable-auto-data-source-proxy: true
# use-jdk-proxy: false
service:
vgroup-mapping:
#对应nacos中的service.vgroupMapping.my_test_tx_group=default(my_test_tx_group要对应起来)
my_test_tx_group: default
grouplist:
#对应nacos中的service.default.grouplist = "127.0.0.1:8091"相同
default: 127.0.0.1:8091
# disable-global-transaction: false
config:
type: nacos
nacos:
#一定要写上namespace
namespace: public
#这里的地址就是你的nacos的地址
serverAddr: 127.0.0.1:8848
#这里的名字就是registry.conf中 nacos的group名字
group: SEATA_GROUP
userName: nacos
password: nacos
registry:
type: nacos
nacos:
application: seata-server
#这里的地址就是你的nacos的地址,可以更换为线上
server-addr: 127.0.0.1:8848
#这里的名字就是registry.conf中 nacos的group名字
group: SEATA_GROUP
#一定要写上namespace
namespace: public
userName: nacos
password: nacos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
- nacos必须指定group、namespace、userName、password。
# 使用事务
- 需要在数据库中添加一个表,如果不同应用使用了不同的库,那么每个库都需要添加下面的表:
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;
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
在需要添加事务的方法上添加注解
@GlobalTransactional,涉及到的应用都需要添加依赖和配置,都需要添加表undo_log只需要一个@GlobalTransactional就可以了,不用方法都添加注解,但是涉及到的应用都必须引入依赖。
上次更新: 2024/06/16, 12:40:33