环境信息
seata:1.4.2
nacos:1.4.2
jdk:1.8
注:seata版本需和nacos版本一致,不然可能出现seata服务注册不到nacos上
1. 服务端
docker-compose.yaml
version: "3.1"
services:
seata-server:
image: seataio/seata-server:1.4.2
hostname: seata-server
ports:
- 8091:8091
environment:
- SEATA_IP=110.110.10.112
- SEATA_PORT=8091
# 容器内部路径,注意加file
- SEATA_CONFIG_NAME=file:/root/seata-config/registry
expose:
- 8091
volumes:
# 需要把file.conf和registry.conf都放到./seata-server/config文件夹中
- "/opt/seata-server/config:/root/seata-config"
自定义registry.conf
docker-compose.yaml中指定的volumes放置该文件
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "110.110.10.112:8848"
group = "SEATA_GROUP"
namespace = "691c9746-1e6c-49dd-b33b-d327019cb325"
cluster = "dvp-seata"
username = ""
password = ""
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "110.110.10.112:8848"
namespace = "691c9746-1e6c-49dd-b33b-d327019cb325"
group = "SEATA_GROUP"
username = ""
password = ""
dataId = "seata-server.properties"
}
}
nacos配置
即 registry.conf 中dataId指定的配置。
在nacos上创建
dataId: seata-server.properties
group: SEATA_GROUP
transport.type=TCP
transport.server=NIO
transport.serialization=seata
transport.compressor=none
transport.heartbeat=true
transport.shutdown.wait=3
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
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
server.recovery.committingRetryPeriod=1000
server.recovery.asynCommittingRetryPeriod=1000
server.recovery.rollbackingRetryPeriod=1000
server.recovery.timeoutRetryPeriod=1000
server.maxCommitRetryTimeout=-1
server.maxRollbackRetryTimeout=-1
server.rollbackRetryTimeoutUnlockEnable=false
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://110.110.10.112:3306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=xxxxxxx
store.db.password=xxxxxxxxxx
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
创建服务端数据库
-- 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(128),
`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;
2. 客户端
bootstrap.yml
配置中心增加引入seata配置
spring:
cloud:
nacos:
config:
shared-configs:
- data-id: seata-client.yaml
group: SEATA_GROUP
refresh: true
nacos上配置seata客户端信息
dataId:seata-client.yaml
group:SEATA_GROUP
spring:
autoconfigure:
#如果项目没有使用sleuth,不需要解决feign调用冲突,则可以删除该配置
exclude: com.alibaba.cloud.seata.feign.SeataFeignClientAutoConfiguration
seata:
enabled: true
enableAutoDataSourceProxy: true
tx-service-group: dvp_group
client:
rm:
async-commit-buffer-limit: 10000
tm:
default-global-transaction-timeout: 60000
transport:
enable-client-batch-send-request: true
config:
type: nacos
nacos:
namespace: ${spring.cloud.nacos.config.namespace}
server-addr: ${spring.cloud.nacos.config.server-addr}
group: SEATA_GROUP
data-id: seata-cluster.properties
registry:
type: nacos
nacos:
namespace: ${spring.cloud.nacos.config.namespace}
server-addr: ${spring.cloud.nacos.config.server-addr}
group: SEATA_GROUP
#service:
#vgroup-mapping:
#dvp_group: dvp-cluster-dev
#enable-degrade: false
#disable-global-transaction: false
seata-cluster.properties
暂不知为何不能将该配置放入seata-client.yaml中,而且格式只能是xxx.properties格式。
dataId:seata-cluster.properties
group:SEATA_GROUP
service.vgroupMapping.dvp_group=dvp-seata
数据库表
-- 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`
(
`branch_id` BIGINT NOT NULL COMMENT 'branch transaction id',
`xid` VARCHAR(128) 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(6) NOT NULL COMMENT 'create datetime',
`log_modified` DATETIME(6) NOT NULL COMMENT 'modify datetime',
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';