当前位置:   article > 正文

SpringBoot整合Seata注册到Nacos服务_seata nacos

seata nacos

项目引入pom文件

  1. <!-- SpringCloud Seata 组件-->
  2. <dependency>
  3. <groupId>com.alibaba.cloud</groupId>
  4. <artifactId>spring-cloud-alibaba-seata</artifactId>
  5. <version>${alibaba.seata}</version>
  6. <exclusions>
  7. <exclusion>
  8. <groupId>io.seata</groupId>
  9. <artifactId>seata-all</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>
  13. <dependency>
  14. <groupId>io.seata</groupId>
  15. <artifactId>seata-spring-boot-starter</artifactId>
  16. <version>${seata}</version>
  17. </dependency>

yml配置文件

  1. #====================================Seata Config===============================================
  2. seata:
  3. enabled: true
  4. tx-service-group: my_test_tx_group # 1 事务群组(可以每个应用独立取名,也可以使用相同的名字)这个需要跟nacos的配置名字保持一致
  5. registry:
  6. type: nacos
  7. nacos:
  8. server-addr: ${nacos-addr} #nacos地址 localhost:8848
  9. application: service-seata
  10. namespace: seata #seata在nacos中配置的命名空间
  11. group: SEATA_GROUP
  12. cluster: default
  13. username: nacos
  14. password: nacos
  15. config:
  16. type: nacos
  17. nacos:
  18. server-addr: ${nacos-addr} #nacos地址 localhost:8848
  19. namespace: seata #seata在nacos中配置的命名空间
  20. group: SEATA_GROUP
  21. username: nacos
  22. password: nacos

Seata文件配置

修改file.conf

  1. ## transaction log store, only used in seata-server
  2. store {
  3. ## store mode: file、db、redis
  4. mode = "db" ##将file改为db
  5. ## file store property
  6. file {
  7. ## store location dir
  8. dir = "sessionStore"
  9. # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
  10. maxBranchSessionSize = 16384
  11. # globe session size , if exceeded throws exceptions
  12. maxGlobalSessionSize = 512
  13. # file buffer size , if exceeded allocate new buffer
  14. fileWriteBufferCacheSize = 16384
  15. # when recover batch read size
  16. sessionReloadReadSize = 100
  17. # async, sync
  18. flushDiskMode = async
  19. }
  20. ## database store property
  21. db {
  22. ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
  23. datasource = "druid"
  24. ## mysql/oracle/postgresql/h2/oceanbase etc.
  25. dbType = "mysql"
  26. driverClassName = "com.mysql.jdbc.Driver"
  27. url = "jdbc:mysql://127.0.0.1:3306/db_seata" ##修改为自己的数据库连接
  28. user = "root"
  29. password = "123456"
  30. minConn = 5
  31. maxConn = 30
  32. globalTable = "global_table"
  33. branchTable = "branch_table"
  34. lockTable = "lock_table"
  35. queryLimit = 100
  36. maxWait = 5000
  37. }
  38. ## redis store property
  39. redis {
  40. host = "127.0.0.1"
  41. port = "6379"
  42. password = ""
  43. database = "0"
  44. minConn = 1
  45. maxConn = 10
  46. queryLimit = 100
  47. }
  48. }

修改regitry.conf

  1. registry {
  2. # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  3. type = "nacos" ##我们使用的是nacos,因此将type修改为nacos
  4. nacos {
  5. application = "service-seata" #application填写注册的服务名
  6. serverAddr = "127.0.0.1:8848" #填写nacos注册中心地址
  7. group = "SEATA_GROUP" #group是服务注册的分组。可不填,注:是服务分组,不是配置分组
  8. namespace = "seata" #填写服务注册时的命名空间,可不填,不填默认的时public命名空间
  9. cluster = "default" #cluster不用改
  10. username = "nacos"
  11. password = "nacos"
  12. }
  13. file {
  14. name = "file.conf"
  15. }
  16. }
  17. config {
  18. # file、nacos 、apollo、zk、consul、etcd3
  19. type = "nacos"
  20. nacos {
  21. serverAddr = "127.0.0.1:8848"
  22. namespace = "seata"
  23. group = "SEATA_GROUP"
  24. username = "nacos"
  25. password = "nacos"
  26. }
  27. file {
  28. name = "file.conf"
  29. }
  30. }

将配置导入nacos

本来我们是需要把file.conf和registry.conf放入我们的项目中的,这里直接导入到nacos。需要什么配置直接从nacos中获取。不需要放在项目中了

修改conf.txt

在conf目录下

  1. service.vgroupMapping.my_test_tx_group=default #需要跟配置文件的保持一致
  2. store.mode=db
  3. store.db.datasource=druid
  4. store.db.dbType=mysql
  5. store.db.driverClassName=com.mysql.jdbc.Driver
  6. store.db.url=jdbc:mysql://127.0.0.1:3306/db_seata?useUnicode=true #自己的数据库连接地址
  7. store.db.user=root
  8. store.db.password=123456
  9. store.db.minConn=5
  10. store.db.maxConn=30
  11. store.db.globalTable=global_table
  12. store.db.branchTable=branch_table
  13. store.db.queryLimit=100
  14. store.db.lockTable=lock_table
  15. store.db.maxWait=5000
创建命名空间

执行导入

打开git bash。

输入以下命令:

sh nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t seata -u nacos -w nacos

注:命令解析:-h -p 指定nacos的端口地址;-g 指定配置的分组,注意,是配置的分组;-t 指定命名空间id; -u -w指定nacos的用户名和密码,同样,这里开启了nacos注册和配置认证的才需要指定。

导入成功之后如下图:

数据库配置

db_seata数据库中导入数据表

branch_table

global_table

lock_table

业务数据库中导入数据表

undo_log

启动Seata

配置成功

记得主接口增加注解 @GlobalTransactional

远程调用服务增加注解 @Transactional

大功告成!!!!

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

闽ICP备14008679号