当前位置:   article > 正文

spring cloud alibaba 整合SeaTa AT 模式_service.vgroupmapping.default_tx_group configurati

service.vgroupmapping.default_tx_group configuration item is required

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 如下:

  1. # Copyright 1999-2019 Seata.io Group.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. server:
  15. port: 7091
  16. spring:
  17. application:
  18. name: seata-server
  19. #不用修改
  20. logging:
  21. config: classpath:logback-spring.xml
  22. file:
  23. path: ${user.home}/logs/seata
  24. extend:
  25. logstash-appender:
  26. destination: 127.0.0.1:4560
  27. kafka-appender:
  28. bootstrap-servers: 127.0.0.1:9092
  29. topic: logback_to_logstash
  30. #seata后台的登录用户名和密码
  31. console:
  32. user:
  33. username: seata
  34. password: seata
  35. #seata配置中心
  36. seata:
  37. config:
  38. # support: nacos, consul, apollo, zk, etcd3
  39. type: nacos
  40. nacos:
  41. server-addr: 192.168.26.1:8848
  42. namespace: 20f508fc-6feb-452d-ac60-f37af20a9db5
  43. file-extension: properties
  44. group: DEFAULT_GROUP
  45. username: nacos
  46. password: nacos
  47. # 配置在nacos上的文件 复制 \seata\script\config-center\config.txt文件进行修改
  48. data-id: seata-demo.properties
  49. #seata注册中心
  50. registry:
  51. # support: nacos, eureka, redis, zk, consul, etcd3, sofa
  52. type: nacos
  53. nacos:
  54. application: seata-server #要与spring cloud的服务配置对应名称
  55. server-addr: 192.168.26.1:8848
  56. namespace: 20f508fc-6feb-452d-ac60-f37af20a9db5
  57. group: DEFAULT_GROUP
  58. username: nacos
  59. password: nacos
  60. #存储方式的选择
  61. store:
  62. # support: file 、 db 、 redis
  63. mode: db
  64. db:
  65. driver-class-name: com.mysql.cj.jdbc.Driver
  66. #自己的数据库地址 执行脚本\seata\script\server\db\mysql.sql
  67. url: jdbc:mysql://127.0.0.1:3306/seata
  68. # 自己的数据库用户名和密码
  69. username: root
  70. password: root
  71. max-idle: 10
  72. max-wait: 30000
  73. min-idle: 5
  74. initial-size: 5
  75. global-table: global_table
  76. branch-table: branch_table
  77. lock-table: lock_table
  78. query-limit: 100
  79. # server:
  80. # service-port: 8091 #If not configured, the default is '${server.port} + 1000'
  81. security:
  82. secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
  83. tokenValidityInMilliseconds: 1800000
  84. ignore:
  85. urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login

三:seata-demo.properties

  1. #For details about configuration items, see https://seata.io/zh-cn/docs/user/configurations.html
  2. #Transport configuration, for client and server
  3. transport.type=TCP
  4. transport.server=NIO
  5. transport.heartbeat=true
  6. transport.enableTmClientBatchSendRequest=false
  7. transport.enableRmClientBatchSendRequest=true
  8. transport.enableTcServerBatchSendResponse=false
  9. transport.rpcRmRequestTimeout=30000
  10. transport.rpcTmRequestTimeout=30000
  11. transport.rpcTcRequestTimeout=30000
  12. transport.threadFactory.bossThreadPrefix=NettyBoss
  13. transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
  14. transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
  15. transport.threadFactory.shareBossWorker=false
  16. transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
  17. transport.threadFactory.clientSelectorThreadSize=1
  18. transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
  19. transport.threadFactory.bossThreadSize=1
  20. transport.threadFactory.workerThreadSize=default
  21. transport.shutdown.wait=3
  22. transport.serialization=seata
  23. transport.compressor=none
  24. #Transaction routing rules configuration, only for the client
  25. #需要修改的点 default_tx_group为事务分组名称 default为springcloud微服务集群名称
  26. service.vgroupMapping.default_tx_group=default
  27. #If you use a registry, you can ignore it
  28. service.default.grouplist=127.0.0.1:8091
  29. service.enableDegrade=false
  30. service.disableGlobalTransaction=false
  31. #Transaction rule configuration, only for the client
  32. client.rm.asyncCommitBufferLimit=10000
  33. client.rm.lock.retryInterval=10
  34. client.rm.lock.retryTimes=30
  35. client.rm.lock.retryPolicyBranchRollbackOnConflict=true
  36. client.rm.reportRetryCount=5
  37. client.rm.tableMetaCheckEnable=true
  38. client.rm.tableMetaCheckerInterval=60000
  39. client.rm.sqlParserType=druid
  40. client.rm.reportSuccessEnable=false
  41. client.rm.sagaBranchRegisterEnable=false
  42. client.rm.sagaJsonParser=fastjson
  43. client.rm.tccActionInterceptorOrder=-2147482648
  44. client.tm.commitRetryCount=5
  45. client.tm.rollbackRetryCount=5
  46. client.tm.defaultGlobalTransactionTimeout=60000
  47. client.tm.degradeCheck=false
  48. client.tm.degradeCheckAllowTimes=10
  49. client.tm.degradeCheckPeriod=2000
  50. client.tm.interceptorOrder=-2147482648
  51. client.undo.dataValidation=true
  52. client.undo.logSerialization=jackson
  53. client.undo.onlyCareUpdateColumns=true
  54. server.undo.logSaveDays=7
  55. server.undo.logDeletePeriod=86400000
  56. client.undo.logTable=undo_log
  57. client.undo.compress.enable=true
  58. client.undo.compress.type=zip
  59. client.undo.compress.threshold=64k
  60. #For TCC transaction mode
  61. tcc.fence.logTableName=tcc_fence_log
  62. tcc.fence.cleanPeriod=1h
  63. #Log rule configuration, for client and server
  64. log.exceptionRate=100
  65. #Transaction storage configuration, only for the server. The file, db, and redis configuration values are optional.
  66. #需要修改存储方式
  67. store.mode=db
  68. store.lock.mode=file
  69. store.session.mode=file
  70. #Used for password encryption
  71. store.publicKey=
  72. #If `store.mode,store.lock.mode,store.session.mode` are not equal to `file`, you can remove the configuration block.
  73. store.file.dir=file_store/data
  74. store.file.maxBranchSessionSize=16384
  75. store.file.maxGlobalSessionSize=512
  76. store.file.fileWriteBufferCacheSize=16384
  77. store.file.flushDiskMode=async
  78. store.file.sessionReloadReadSize=100
  79. #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.
  80. store.db.datasource=druid
  81. store.db.dbType=mysql
  82. store.db.driverClassName=com.mysql.jdbc.Driver
  83. store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true
  84. store.db.user=root
  85. store.db.password=root
  86. store.db.minConn=5
  87. store.db.maxConn=30
  88. store.db.globalTable=global_table
  89. store.db.branchTable=branch_table
  90. store.db.distributedLockTable=distributed_lock
  91. store.db.queryLimit=100
  92. store.db.lockTable=lock_table
  93. store.db.maxWait=5000
  94. #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.
  95. store.redis.mode=single
  96. store.redis.single.host=127.0.0.1
  97. store.redis.single.port=6379
  98. store.redis.sentinel.masterName=
  99. store.redis.sentinel.sentinelHosts=
  100. store.redis.maxConn=10
  101. store.redis.minConn=1
  102. store.redis.maxTotal=100
  103. store.redis.database=0
  104. store.redis.password=
  105. store.redis.queryLimit=100
  106. #Transaction rule configuration, only for the server
  107. server.recovery.committingRetryPeriod=1000
  108. server.recovery.asynCommittingRetryPeriod=1000
  109. server.recovery.rollbackingRetryPeriod=1000
  110. server.recovery.timeoutRetryPeriod=1000
  111. server.maxCommitRetryTimeout=-1
  112. server.maxRollbackRetryTimeout=-1
  113. server.rollbackRetryTimeoutUnlockEnable=false
  114. server.distributedLockExpireTime=10000
  115. server.xaerNotaRetryTimeout=60000
  116. server.session.branchAsyncQueueSize=5000
  117. server.session.enableBranchAsyncRemove=false
  118. server.enableParallelRequestHandle=false
  119. #Metrics configuration, only for the server
  120. metrics.enabled=false
  121. metrics.registryType=compact
  122. metrics.exporterList=prometheus
  123. 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) 引入对应的依赖

  1. <dependency>
  2. <groupId>com.alibaba.cloud</groupId>
  3. <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
  4. <!--<version>2.2.8.RELEASE</version>
  5. <exclusions>
  6. <exclusion>
  7. <groupId>io.seata</groupId>
  8. <artifactId>seata-all</artifactId>
  9. </exclusion>
  10. </exclusions>-->
  11. </dependency>
  12. <!--<dependency>
  13. <groupId>io.seata</groupId>
  14. <artifactId>seata-all</artifactId>
  15. <version>1.5.2</version>
  16. </dependency>-->

2)application.yml 配置添加

  1. seata:
  2. application-id: ${spring.application.name}
  3. #application-id: seata-server
  4. tx-service-group: default_tx_group
  5. registry:
  6. type: nacos
  7. nacos:
  8. application: seata-server
  9. server-addr: 127.0.0.1:8848
  10. namespace: 20f508fc-6feb-452d-ac60-f37af20a9db5
  11. group: DEFAULT_GROUP
  12. config:
  13. type: nacos
  14. nacos:
  15. server-addr: 127.0.0.1:8848
  16. namespace: 20f508fc-6feb-452d-ac60-f37af20a9db5
  17. group: DEFAULT_GROUP
  18. data-id: seata-demo.properties

3)业务数据库中药执行一下脚本

  1. CREATE TABLE `undo_log` (
  2. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  3. `branch_id` bigint(20) NOT NULL,
  4. `xid` varchar(100) NOT NULL,
  5. `context` varchar(128) NOT NULL,
  6. `rollback_info` longblob NOT NULL,
  7. `log_status` int(11) NOT NULL,
  8. `log_created` datetime NOT NULL,
  9. `log_modified` datetime NOT NULL,
  10. PRIMARY KEY (`id`),
  11. UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
  12. ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;

八:使用,只需要添加对应的注解就可以

  1. @GlobalTransactional(name="testSeata",rollbackFor = Exception.class)
  2. @Override
  3. public void testSeata(int age) {
  4. UserInfoVo vo = new UserInfoVo();
  5. vo.setId(1L);
  6. vo.setAge(age);
  7. userInfoRepository.updateById(vo);
  8. int i = 1/0;
  9. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/68828
推荐阅读
相关标签
  

闽ICP备14008679号