赞
踩
spring cloud alibaba 整合SeaTa AT 模式
一、Seata版本选择
1) spring cloud对应的SeaTa版本选择
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
本文以spring cloudalibaba 2.2.9版本为例:
2)seata的下载地址:
https://github.com/seata/seata/releases
二:修改seata/seata/conf/application.yml内容,由于使用nacos作为注册中心主要seata的config、registry、store 如下:
- # Copyright 1999-2019 Seata.io Group.
-
- #
-
- # Licensed under the Apache License, Version 2.0 (the "License");
-
- # you may not use this file except in compliance with the License.
-
- # You may obtain a copy of the License at
-
- #
-
- # http://www.apache.org/licenses/LICENSE-2.0
-
- #
-
- # Unless required by applicable law or agreed to in writing, software
-
- # distributed under the License is distributed on an "AS IS" BASIS,
-
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-
- # See the License for the specific language governing permissions and
-
- # limitations under the License.
-
- server:
-
- port: 7091
-
- spring:
-
- application:
-
- name: seata-server
-
- #不用修改
-
- logging:
-
- config: classpath:logback-spring.xml
-
- file:
-
- path: ${user.home}/logs/seata
-
- extend:
-
- logstash-appender:
-
- destination: 127.0.0.1:4560
-
- kafka-appender:
-
- bootstrap-servers: 127.0.0.1:9092
-
- topic: logback_to_logstash
-
- #seata后台的登录用户名和密码
-
- console:
-
- user:
-
- username: seata
-
- password: seata
-
- #seata配置中心
-
- seata:
-
- config:
-
- # support: nacos, consul, apollo, zk, etcd3
-
- type: nacos
-
- nacos:
-
- server-addr: 192.168.26.1:8848
-
- namespace: 20f508fc-6feb-452d-ac60-f37af20a9db5
-
- file-extension: properties
-
- group: DEFAULT_GROUP
-
- username: nacos
-
- password: nacos
-
- # 配置在nacos上的文件 复制 \seata\script\config-center\config.txt文件进行修改
-
- data-id: seata-demo.properties
-
- #seata注册中心
-
- registry:
-
- # support: nacos, eureka, redis, zk, consul, etcd3, sofa
-
- type: nacos
-
- nacos:
-
- application: seata-server #要与spring cloud的服务配置对应名称
-
- server-addr: 192.168.26.1:8848
-
- namespace: 20f508fc-6feb-452d-ac60-f37af20a9db5
-
- group: DEFAULT_GROUP
-
- username: nacos
-
- password: nacos
-
- #存储方式的选择
-
- store:
-
- # support: file 、 db 、 redis
-
- mode: db
-
- db:
-
- driver-class-name: com.mysql.cj.jdbc.Driver
-
- #自己的数据库地址 执行脚本\seata\script\server\db\mysql.sql
-
- url: jdbc:mysql://127.0.0.1:3306/seata
-
- # 自己的数据库用户名和密码
-
- username: root
-
- password: root
-
- max-idle: 10
-
- max-wait: 30000
-
- min-idle: 5
-
- initial-size: 5
-
- global-table: global_table
-
- branch-table: branch_table
-
- lock-table: lock_table
-
- query-limit: 100
-
- # server:
-
- # service-port: 8091 #If not configured, the default is '${server.port} + 1000'
-
- security:
-
- secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
-
- tokenValidityInMilliseconds: 1800000
-
- ignore:
-
- urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
三:seata-demo.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
-
- #需要修改的点 default_tx_group为事务分组名称 default为springcloud微服务集群名称
-
- 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=file
-
- store.session.mode=file
-
- #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
-
- #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.jdbc.Driver
-
- store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true
-
- 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
四:对应sql的地址 seata\script\server\db\mysql.sql
五:启动seata服务 执行 ./bin/seata-server.bat 命令
六:访问后台 http://127.0.0.1:7091/#/login
账号:seata 密码:seata
七:集成springcloud alibaba
1) 引入对应的依赖
- <dependency>
-
- <groupId>com.alibaba.cloud</groupId>
-
- <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
-
- <!--<version>2.2.8.RELEASE</version>
- <exclusions>
- <exclusion>
- <groupId>io.seata</groupId>
- <artifactId>seata-all</artifactId>
- </exclusion>
- </exclusions>-->
-
- </dependency>
-
- <!--<dependency>
- <groupId>io.seata</groupId>
- <artifactId>seata-all</artifactId>
- <version>1.5.2</version>
- </dependency>-->
2)application.yml 配置添加
- seata:
-
- application-id: ${spring.application.name}
-
- #application-id: seata-server
-
- tx-service-group: default_tx_group
-
- registry:
-
- type: nacos
-
- nacos:
-
- application: seata-server
-
- server-addr: 127.0.0.1:8848
-
- namespace: 20f508fc-6feb-452d-ac60-f37af20a9db5
-
- group: DEFAULT_GROUP
-
- config:
-
- type: nacos
-
- nacos:
-
- server-addr: 127.0.0.1:8848
-
- namespace: 20f508fc-6feb-452d-ac60-f37af20a9db5
-
- group: DEFAULT_GROUP
-
- data-id: seata-demo.properties
3)业务数据库中药执行一下脚本
- 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,
-
- PRIMARY KEY (`id`),
-
- UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
-
- ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
八:使用,只需要添加对应的注解就可以
- @GlobalTransactional(name="testSeata",rollbackFor = Exception.class)
-
- @Override
-
- public void testSeata(int age) {
-
- UserInfoVo vo = new UserInfoVo();
-
- vo.setId(1L);
-
- vo.setAge(age);
-
- userInfoRepository.updateById(vo);
-
- int i = 1/0;
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。