SpringCloud整合Seata1.6.1部署与使用Nacos方式
seata官网:http://seata.io/zh-cn/index.html
seata-server: https://github.com/seata/seata/releases
SpringCloud整合Seata出现异常数据未回滚的解决方案
二. seata-server配置
1.1 下载seater-server
下载指定版本seata-server,本案例使用
v1.6.1
版本。
1.2 修改seata/conf/application.yml配置文件(已删除无用配置)
server:port:7091spring:application:name: seata-server # seata-server服务名logging:config: classpath:logback-spring.xml
file:path: ${user.home}/seata/runlogs # 指定日志路径extend:logstash-appender:destination: 127.0.0.1:4560kafka-appender:bootstrap-servers: 127.0.0.1:9092topic: logback_to_logstash
# seata可视化web界面账号密码console:user:username: seata
password: seata
seata:# 配置中心config:# support: nacos, consul, apollo, zk, etcd3type: nacos # 指定配置中心为nacosnacos:server-addr: 127.0.0.1:8848# nacos的ip端口group: DEFAULT_GROUP # 对应的组,默认为DEFAULT_GROUPnamespace: a090b021-160c-42fb-98de-b1f9a5619d97 # 对应的命名空间,在nacos中配置username: nacos
password: nacos
data-id: seataServer.properties # nacos中存放seata的配置文件,后面会提该文件的使用方式,相当于seata服务启动的时候需要注册到nacos,并使用nacos中的配置文件# 注册中心与上述config同理registry:# support: nacos, eureka, redis, zk, consul, etcd3, sofatype: nacos
nacos:application: seata-server
server-addr: 127.0.0.1:8848namespace: a090b021-160c-42fb-98de-b1f9a5619d97
group: DEFAULT_GROUP
cluster: default
username: nacos
password: nacos
security:secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds:1800000ignore:urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
【注意】
seata.config.type
与seata.registry.type
都要修改为nacos
- 修改
config
与registry
中nacos的配置,其中namespace
与group
须提前在nacos中进行配置
二. nacos配置
2.1 新建namespace
新建
namespace
命名空间,需与上述
seata-server
的
application.yml
中配置一致
2.2 新建seata的seataServer.properties配置文件
在上述
namespace
下新建
application.yml
中
seata.config.nacos.data-id
提到的配置文件:
seataServer.properties
seataServer.properties 已删除无用配置
#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
# 此处的mygroup名字可以自定义,只修改这个值即可
service.vgroupMapping.mygroup=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.
# 默认为file,一定要改为db,我们自己的服务启动会连接不到seata
store.mode=db
store.lock.mode=db
store.session.mode=db
#Used for password encryption
#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.
# 修改mysql的配置
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
# 指定seata的数据库,下面会提
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=banmajio
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.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
【注意】
- 修改
service.vgroupMapping.mygroup=default
该值,其中mygroup可以自定义,后面我们自己的服务启动时,配置文件中需要指定该group。 - 修改
store.mode``````store.lock.mode``````store.session.mode
这三个值为db
,才能让seata连接到下面的数据库中。 - 修改
store.db
配置项下的配置,连接到自己的数据库。 - 该文件参数的具体作用参考官网:seata配置文件详解(官网)
- 该配置源文件存放在seata目录下:
seata/script/config-center/config.txt
三. MySQL配置
3.1 新建seata数据库
数据库的名称
与上述
3.2
seata配置文件中的
store.db.url
保持一致。
3.2 新建seata配置表
在上述库中添加seata的配置表,sql文件存放在:
seata/script/server/db/mysql.sql
中
-- -------------------------------- The script used when storeMode is 'db' ---------------------------------- the table to store GlobalSession dataCREATETABLEIFNOTEXISTS`global_table`(`xid`VARCHAR(128)NOTNULL,`transaction_id`BIGINT,`status`TINYINTNOTNULL,`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,PRIMARYKEY(`xid`),KEY`idx_status_gmt_modified`(`status`,`gmt_modified`),KEY`idx_transaction_id`(`transaction_id`))ENGINE=InnoDBDEFAULTCHARSET= utf8mb4;-- the table to store BranchSession dataCREATETABLEIFNOTEXISTS`branch_table`(`branch_id`BIGINTNOTNULL,`xid`VARCHAR(128)NOTNULL,`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),PRIMARYKEY(`branch_id`),KEY`idx_xid`(`xid`))ENGINE=InnoDBDEFAULTCHARSET= utf8mb4;-- the table to store lock dataCREATETABLEIFNOTEXISTS`lock_table`(`row_key`VARCHAR(128)NOTNULL,`xid`VARCHAR(128),`transaction_id`BIGINT,`branch_id`BIGINTNOTNULL,`resource_id`VARCHAR(256),`table_name`VARCHAR(32),`pk`VARCHAR(36),`status`TINYINTNOTNULLDEFAULT'0'COMMENT'0:locked ,1:rollbacking',`gmt_create`DATETIME,`gmt_modified`DATETIME,PRIMARYKEY(`row_key`),KEY`idx_status`(`status`),KEY`idx_branch_id`(`branch_id`),KEY`idx_xid`(`xid`))ENGINE=InnoDBDEFAULTCHARSET= utf8mb4;CREATETABLEIFNOTEXISTS`distributed_lock`(`lock_key`CHAR(20)NOTNULL,`lock_value`VARCHAR(20)NOTNULL,`expire`BIGINT,primarykey(`lock_key`))ENGINE=InnoDBDEFAULTCHARSET= utf8mb4;INSERTINTO`distributed_lock`(lock_key, lock_value, expire)VALUES('AsyncCommitting',' ',0);INSERTINTO`distributed_lock`(lock_key, lock_value, expire)VALUES('RetryCommitting',' ',0);INSERTINTO`distributed_lock`(lock_key, lock_value, expire)VALUES('RetryRollbacking',' ',0);INSERTINTO`distributed_lock`(lock_key, lock_value, expire)VALUES('TxTimeoutCheck',' ',0);
3.3 业务数据库添加undo_log表
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_logCREATETABLE`undo_log`(`id`bigint(20)NOTNULLAUTO_INCREMENT,`branch_id`bigint(20)NOTNULL,`xid`varchar(100)NOTNULL,`context`varchar(128)NOTNULL,`rollback_info`longblobNOTNULL,`log_status`int(11)NOTNULL,`log_created`datetimeNOTNULL,`log_modified`datetimeNOTNULL,`ext`varchar(100)DEFAULTNULL,PRIMARYKEY(`id`),UNIQUEKEY`ux_undo_log`(`xid`,`branch_id`))ENGINE=InnoDBAUTO_INCREMENT=1DEFAULTCHARSET=utf8;
4. seata-server启动
seata 的 /bin 目录下 :/seata/bin 执行下面的脚本
sh seata-server.sh -p 8091 -h 127.0.0.1
参数解释:
-h: 将地址暴露给注册中心,其他服务可以访问seata服务器
-p:要侦听的端口
并且查看
nacos
中的服务注册中心是否可以看到
seata-server
至此,seater-server的配置与部署完成
五. SpringCloud整合Seata
5.1 pom.xml依赖引入
<!-- 注意一定要引入对版本,要引入spring-cloud版本seata,而不是springboot版本的seata--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-seata</artifactId><!-- 排除掉springcloud默认的seata版本,以免版本不一致出现问题--><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><!-- 上面排除掉了springcloud默认色seata版本,此处引入和seata-server版本对应的seata包--><dependency><groupId>io.seata</groupId><artifactId>seata-spring-boot-starter</artifactId><version>1.6.1</version></dependency>
5.2 bootstrap.yml
配置文件中加入以下配置
seata:tx-service-group: mygroup # 事务分组名称,要和服务端对应service:vgroup-mapping:mygroup: default # key是事务分组名称 value要和服务端的机房名称保持一致
5.3 启动服务
我们自己的服务要与
seata-server
服务在同一个
namespace
下
查看服务是否启动成功
查看nacos的对应的namespace中服务是否注册成功
5.4 使用
在外层接口上增加
@GlobalTransactional
注解
@GetMapping("/remoteTest")@GlobalTransactionalpublicStringremoteTest(){
orderService.remoteTest();return"order success";}
只需要在最外层方法上添加注解即可,子事务方法上不需要添加
@GlobalTransactional
与
@Transactional
注解。
版权归原作者 banmajio 所有, 如有侵权,请联系我们删除。