【分布式事务系列】Seata-file、db存储模式

这是我参与11月更文挑战的第7天,活动详情查看:2021最后一次更文挑战

file存储模式

Server端存储模式(store.mode)有file、db两种,file存储模式无需改动,直接启动即可。

file存储模式为单机模式,全局模式会话信息持久化在本地文件${seata_home}\bin\sessionStore\root.data中,性能较高,启动命令如下:

1
css复制代码sh seata-server.sh -p 8091 -h 127.0.0.1 -m file
db存储模式

db存储模式为高可用模式,全局事务会话信息通过db共享,性能相对差一些,操作步骤如下:

  • 创建表结构,Seata全局事务会话信息由全局事务、分支事务、全局锁构成,对应表globaltable、branchtable、lock_table
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
sql复制代码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`)
);
  • 设置事务日志存储方式,进入${seata_home}/conf/file.conf,修改store.mode=‘db’
  • 修改数据库连接
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
conf复制代码 db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
datasource = "dbcp"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "mysql"
driverClassName = "com.mysql.jdbc.Driver"
## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
url = "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true"
user = "mysql"
password = "mysql"
minConn = 5
maxConn = 100
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
  • 启动seata-server:
1
sh复制代码seata-server.sh -h 127.0.0.1 -p 8091 -m db -n 1

参数说明如下:

-h: 注册到注册中心的IP地址,Seata-Server把自己注册到注册中心,支持Nacos、Eureka、Redis、Zookeeper、Consul等。

-p:Server RPC 监听端口

-m:全局事务会话信息存储模式,包括file、db,,优先读取启动参数

-n:Server node,有多个Server时,需区分各自节点,用于生成不同区别的transactionId,来避免冲突。

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%