seata官網(wǎng):http://seata.io/zh-cn/index.html
seata-server: https://github.com/seata/seata/releases
SpringCloud整合Seata出現(xiàn)異常數(shù)據(jù)未回滾的解決方案
二. seata-server配置
1.1 下載seater-server
下載指定版本seata-server,本案例使用v1.6.1
版本。
1.2 修改seata/conf/application.yml配置文件(已刪除無用配置)
server:
port: 7091
spring:
application:
name: seata-server # seata-server服務(wù)名
logging:
config: classpath:logback-spring.xml
file:
path: ${user.home}/seata/runlogs # 指定日志路徑
extend:
logstash-appender:
destination: 127.0.0.1:4560
kafka-appender:
bootstrap-servers: 127.0.0.1:9092
topic: logback_to_logstash
# seata可視化web界面賬號密碼
console:
user:
username: seata
password: seata
seata:
# 配置中心
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos # 指定配置中心為nacos
nacos:
server-addr: 127.0.0.1:8848 # nacos的ip端口
group: DEFAULT_GROUP # 對應(yīng)的組,默認(rèn)為DEFAULT_GROUP
namespace: a090b021-160c-42fb-98de-b1f9a5619d97 # 對應(yīng)的命名空間,在nacos中配置
username: nacos
password: nacos
data-id: seataServer.properties # nacos中存放seata的配置文件,后面會提該文件的使用方式,相當(dāng)于seata服務(wù)啟動的時(shí)候需要注冊到nacos,并使用nacos中的配置文件
# 注冊中心與上述config同理
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
namespace: a090b021-160c-42fb-98de-b1f9a5619d97
group: DEFAULT_GROUP
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
【注意】
-
seata.config.type
與seata.registry.type
都要修改為nacos
- 修改
config
與registry
中nacos的配置,其中namespace
與group
須提前在nacos中進(jìn)行配置
二. 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名字可以自定義,只修改這個(gè)值即可
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.
# 默認(rèn)為file,一定要改為db,我們自己的服務(wù)啟動會連接不到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的數(shù)據(jù)庫,下面會提
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可以自定義,后面我們自己的服務(wù)啟動時(shí),配置文件中需要指定該group。 - 修改
store.mode
store.lock.mode
store.session.mode
這三個(gè)值為db
,才能讓seata連接到下面的數(shù)據(jù)庫中。 - 修改
store.db
配置項(xiàng)下的配置,連接到自己的數(shù)據(jù)庫。 - 該文件參數(shù)的具體作用參考官網(wǎng):seata配置文件詳解(官網(wǎng))
- 該配置源文件存放在seata目錄下:
seata/script/config-center/config.txt
三. MySQL配置
3.1 新建seata數(shù)據(jù)庫
數(shù)據(jù)庫的名稱
與上述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 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_status_gmt_modified` (`status` , `gmt_modified`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
-- 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 = utf8mb4;
-- 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),
`status` TINYINT NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`row_key`),
KEY `idx_status` (`status`),
KEY `idx_branch_id` (`branch_id`),
KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
CREATE TABLE IF NOT EXISTS `distributed_lock`
(
`lock_key` CHAR(20) NOT NULL,
`lock_value` VARCHAR(20) NOT NULL,
`expire` BIGINT,
primary key (`lock_key`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);
3.3 業(yè)務(wù)數(shù)據(jù)庫添加undo_log表
-- 注意此處0.3.0+ 增加唯一索引 ux_undo_log
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 AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
4. seata-server啟動
seata 的 /bin 目錄下 :/seata/bin 執(zhí)行下面的腳本
sh seata-server.sh -p 8091 -h 127.0.0.1
參數(shù)解釋:
-h: 將地址暴露給注冊中心,其他服務(wù)可以訪問seata服務(wù)器
-p:要偵聽的端口
啟動成功后,即可訪問 http://127.0.0.1:7091/#/login 改地址進(jìn)入seata的webui,用戶名與密碼默認(rèn)為seata
,可在上面的1.2章節(jié)中中提到的application.yml
配置項(xiàng):console.user
中修改
并且查看nacos
中的服務(wù)注冊中心是否可以看到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默認(rèn)的seata版本,以免版本不一致出現(xiàn)問題-->
<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默認(rèn)色seata版本,此處引入和seata-server版本對應(yīng)的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 # 事務(wù)分組名稱,要和服務(wù)端對應(yīng)
service:
vgroup-mapping:
mygroup: default # key是事務(wù)分組名稱 value要和服務(wù)端的機(jī)房名稱保持一致
其中tx-service-group
配置項(xiàng)要與 上述2.3章節(jié)中提到的service.vgroupMapping.mygroup=default
中的group保持一致,seata.service.vgroup-mapping.mygroup
中的mygroup
同理。
5.3 啟動服務(wù)
我們自己的服務(wù)要與seata-server
服務(wù)在同一個(gè)namespace
下
查看服務(wù)是否啟動成功
查看nacos的對應(yīng)的namespace中服務(wù)是否注冊成功
5.4 使用
在外層接口上增加@GlobalTransactional
注解文章來源:http://www.zghlxwxcb.cn/news/detail-435148.html
@GetMapping("/remoteTest")
@GlobalTransactional
public String remoteTest(){
orderService.remoteTest();
return "order success";
}
只需要在最外層方法上添加注解即可,子事務(wù)方法上不需要添加@GlobalTransactional
與@Transactional
注解。文章來源地址http://www.zghlxwxcb.cn/news/detail-435148.html
到了這里,關(guān)于SpringCloud整合Seata1.6.1部署與使用Nacos方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!