250802-SeataServer部署

分布式事务解决方案Seata

本文主要介绍如下内容

  1. SeataServer部署
  2. AT事务实现
  3. TCC事务实现

SeataServer部署

下载

在github上下载需要的版本,本文以1.6.1版为例

配置

使用nacos作为配置中和注册中心,同时使用mysql数据库;

解压后,修改 conf/application.yml 下的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
seata:
config:
type: nacos
nacos:
server-addr: laosan.xin:8848
namespace: "dev-wdg" # 建议与微服务命名空间保持一致
group: "DEFAULT_GROUP" # 建议与微服务组保持一致
username: "nacos"
password: "xxxxxx"
dataId: "seataServer.properties" # nacos配置中心信息
registry:
type: nacos
nacos:
serverAddr: "laosan.xin:8848" # Nacos 地址
namespace: "dev-wdg" # 建议与微服务命名空间保持一致
group: "DEFAULT_GROUP" # 建议与微服务组保持一致
cluster: "DEFAULT" # 建议与微服务组保持一致
username: "nacos" # Nacos 用户名(如果开启了鉴权)
password: "xxxxxx"

在nacos服务商添加seataServer.properties配置文件, 配置内容如下

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#For details about configuration items, see https://seata.io/zh-cn/docs/user/configurations.html
#Transport configuration, for client and server
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableTmClientBatchSendRequest=false
transport.enableRmClientBatchSendRequest=true
transport.enableTcServerBatchSendResponse=false
transport.rpcRmRequestTimeout=30000
transport.rpcTmRequestTimeout=30000
transport.rpcTcRequestTimeout=30000
transport.threadFactory.bossThreadPrefix=NettyBoss
transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
transport.threadFactory.shareBossWorker=false
transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
transport.threadFactory.clientSelectorThreadSize=1
transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
transport.threadFactory.bossThreadSize=1
transport.threadFactory.workerThreadSize=default
transport.shutdown.wait=3
transport.serialization=seata
transport.compressor=none

#Transaction routing rules configuration, only for the client
service.vgroupMapping.default_tx_group=DEFAULT
#If you use a registry, you can ignore it
#service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false

#Transaction rule configuration, only for the client
client.rm.asyncCommitBufferLimit=10000
client.rm.lock.retryInterval=10
client.rm.lock.retryTimes=30
client.rm.lock.retryPolicyBranchRollbackOnConflict=true
client.rm.reportRetryCount=5
client.rm.tableMetaCheckEnable=true
client.rm.tableMetaCheckerInterval=60000
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.rm.sagaJsonParser=fastjson
client.rm.tccActionInterceptorOrder=-2147482648
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.defaultGlobalTransactionTimeout=60000
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000
client.tm.interceptorOrder=-2147482648
client.undo.dataValidation=true
client.undo.logSerialization=jackson
client.undo.onlyCareUpdateColumns=true
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
client.undo.logTable=undo_log
client.undo.compress.enable=true
client.undo.compress.type=zip
client.undo.compress.threshold=64k
#For TCC transaction mode
tcc.fence.logTableName=tcc_fence_log
tcc.fence.cleanPeriod=1h

#Log rule configuration, for client and server
log.exceptionRate=100

#Transaction storage configuration, only for the server. The file, db, and redis configuration values are optional.
store.mode=db
store.lock.mode=db
store.session.mode=db
#Used for password encryption
store.publicKey=


#These configurations are required if the `store mode` is `db`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `db`, you can remove the configuration block.
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://47.96.121.94:4306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=xxxxxx
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.rollbackFailedUnlockEnable=false
server.distributedLockExpireTime=10000
server.xaerNotaRetryTimeout=60000
server.session.branchAsyncQueueSize=5000
server.session.enableBranchAsyncRemove=false
server.enableParallelRequestHandle=false

#Metrics configuration, only for the server
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898

启动Server

1
./bin/seata-server.sh -h 127.0.0.1

查看日志

1
2
3
4
5
6
16:08:57.188  INFO --- [                     main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 7091 (http) with context path ''
16:08:57.193 INFO --- [ main] io.seata.server.ServerApplication : Started ServerApplication in 2.182 seconds (JVM running for 3.428)
16:08:58.820 INFO --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
16:08:59.519 INFO --- [ main] i.s.core.rpc.netty.NettyServerBootstrap : Server started, service listen port: 8091
16:08:59.571 INFO --- [ main] com.alibaba.nacos.client.naming : initializer namespace from System Property :null
16:08:59.699 INFO --- [ main] com.alibaba.nacos.client.naming : [BEAT] adding beat: BeatInfo{port=8091, ip='26.26.26.1', weight=1.0, serviceName='DEFAULT_GROUP@@seata-server', cluster='DEFAULT', metadata={}, scheduled=false, period=5000, stopped=false} to beat map.

表示已经成功启动

界面如下:
seata conole

AT事务实现

数据库初始化

在各个微服务中,创建 undo_log表

1
2
3
4
5
6
7
8
9
10
11
12
13
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 DEFAULT CHARSET=utf8;

配置依赖

各个微服务引入

1
2
3
4
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>

代码入口添加全局事务

1
2
3
4
5
6
@GlobalTransactional
@Override
public void process(EventMessageReq eventMessageReq) {
// 委托给 WarnInfoService 处理完整的业务逻辑
deviceEventService.processDeviceEvent(eventMessageReq);
}

接口测试

发送测试报文,可以看到seata server 日志信息

测试报文

TCC事务实现

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×