springcloud中Seata-1.5.2的使用
记录如何部署TC服务以及在微服务中常见的XA、AT模式的使用
seata 服务搭建
下载
https://seata.apache.org/zh-cn/release-history/seata-server
库表建立
mysql建立库并到seata\script\server\db目录下复制sql
配置application.yml
打开abc\seata\conf下的application.yml并配置,seata部分配置从application.example.yml里面copy
server:
port: 7091
spring:
application:
name: seata-server
logging:
config: classpath:logback-spring.xml
file:
path: E:/data/cloud/seata
extend:
logstash-appender:
destination: 127.0.0.1:4560
kafka-appender:
bootstrap-servers: 127.0.0.1:9092
topic: logback_to_logstash
# 可通过访问http://localhost:7091进行登录,账号如下seata/seata
console:
user:
username: seata
password: seata
seata:
config:
type: nacos
nacos:
server-addr: 192.168.150.129:8844
namespace: 685f6872-f0d1-44b1-bc07-1422f16ffa3a
group: SEATA_GROUP
username: nacos
password: nacos
registry:
type: nacos
preferred-networks: 30.240.*
nacos:
application: seata-server
server-addr: 192.168.150.129:8844
group: SEATA_GROUP
namespace: 685f6872-f0d1-44b1-bc07-1422f16ffa3a
cluster: default
username: nacos
password: nacos
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
注意!
1.不要缺少console.user和security.secretKey配置不然会过不了security安全验证
2.记得改日志地址
配置nacos
修改script\config-center的config.txt
注意修改项
1.service.vgroupMapping.seata_tx_group=default
2.store.mode=db、store.lock.mode=db、store.session.mode=db
3.修改JDBC
#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.seata_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
#-------------修改mode--------------
#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=
#If `store.mode,store.lock.mode,store.session.mode` are not equal to `file`, you can remove the configuration block.
store.file.dir=file_store/data
store.file.maxBranchSessionSize=16384
store.file.maxGlobalSessionSize=512
store.file.fileWriteBufferCacheSize=16384
store.file.flushDiskMode=async
store.file.sessionReloadReadSize=100
#-----------修改JDBC连接-----------
#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://192.168.150.129:3306/seata?useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
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.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
#These configurations are required if the `store mode` is `redis`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `redis`, you can remove the configuration block.
store.redis.mode=single
store.redis.single.host=127.0.0.1
store.redis.single.port=6379
store.redis.sentinel.masterName=
store.redis.sentinel.sentinelHosts=
store.redis.maxConn=10
store.redis.minConn=1
store.redis.maxTotal=100
store.redis.database=0
store.redis.password=
store.redis.queryLimit=100
#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.rollbackRetryTimeoutUnlockEnable=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
nacos导入配置项
进入script\config-center\nacos执行命令
sh nacos-config.sh -h nacos地址 -p nacos端口 -g SEATA分组 -t 命名空间 -u nacos账号 -w nacos密码
sh nacos-config.sh -h 192.168.150.129 -p 8844 -g SEATA_GROUP -t 685f6872-f0d1-44b1-bc07-1422f16ffa3a -u nacos -w nacos
执行完成后发现nacos多了配置如下
注意!
1.我的是mysql8所以写的是 com.mysql.cj.jdbc.Driver
2.如果你不想导入这么多nacos配置项,也可以在conf目录下的application.yml中指定data-id: seataServer.properties,然后在seataServer.properties中写入你需要用的配置
store.mode=db
store.lock.mode=db
store.session.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://192.168.150.129:3306/seata?useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
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.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
启动
seata\bin目录下根据系统使用bat或sh启动,启动成功nacos会有对应的服务,且启动窗口无报错
Seata 使用
@GlobalTransactional使用
实现XA
# seata配置
seata:
registry:
type: nacos
nacos:
server-addr: 192.168.150.129:8844
group: SEATA_GROUP
namespace: 685f6872-f0d1-44b1-bc07-1422f16ffa3a
username: nacos
password: nacos
config:
type: nacos
nacos:
server-addr: 192.168.150.129:8844
group: SEATA_GROUP
namespace: 685f6872-f0d1-44b1-bc07-1422f16ffa3a
username: nacos
password: nacos
# 必须和服务器配置一样
tx-service-group: seata_tx_group
service:
vgroup-mapping:
# 必须和服务器配置一样
seata_tx_group: default
data-source-proxy-mode: XA
实现AT(默认模式)
业务新增sql
https://github.com/apache/incubator-seata/blob/1.5.2/script/client/at/db/mysql.sql
create table 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 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',
constraint ux_undo_log
unique (xid, branch_id)
)
comment 'AT transaction mode undo table'
# seata配置
seata:
registry:
type: nacos
nacos:
server-addr: 192.168.150.129:8844
group: SEATA_GROUP
namespace: 685f6872-f0d1-44b1-bc07-1422f16ffa3a
username: nacos
password: nacos
config:
type: nacos
nacos:
server-addr: 192.168.150.129:8844
group: SEATA_GROUP
namespace: 685f6872-f0d1-44b1-bc07-1422f16ffa3a
username: nacos
password: nacos
# 必须和服务器配置一样
tx-service-group: seata_tx_group
service:
vgroup-mapping:
# 必须和服务器配置一样
seata_tx_group: default
data-source-proxy-mode: AT
启用业务代码
业务正常启动且TC服务启动窗口打印日志如下