spring cloud 集成 seata 分布式事务

spring cloud 集成 seata 分布式事务

基于 seata-server 1.6.x

序言

  • 下载 seata-server
    • 准备一个数据库 seata 专门为 seata-server 做存储,如, 可以指定
      • branch_table
      • distributed_lock
      • global_table
      • lock_table
  • 准备一个业务库,比如存放定单,库存表
    • order_tbl
    • stock_tbl
  • 启动好 nacos

工程目录

两个微服务,order 通过 feign 调用 stock 达到 下单扣库存的操作,同时要保证事务

  • svc-order-service
  • svc-stock-service

seata-server 启动

配置 application.yaml

文件在 conf 目录下, 可通过 seata给的 example 文件作相对应的改动,这里采用 nacos 为注册和配置中心,使用 mysql作为存储:

server:
  port: 7091

spring:
  application:
    name: seata-server

logging:
  config: classpath:logback-spring.xml
  file:
    path: ${user.home}/logs/seata
console:
  user:
    username: seata
    password: seata

# 自己加的
vhost:
  ip: 192.xx.xx.129

seata:
  config:
    type: nacos
    nacos:
      server-addr: ${vhost.ip}:8848
      namespace: seata
      group: SEATA_GROUP
      username:
      password:
      context-path:
      data-id: seataServer.properties
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: ${vhost.ip}:8848
      group: SEATA_GROUP
      namespace: seata
      cluster: default
  server:
    service-port: 8091 #If not configured, the default is '${server.port} + 1000'
    max-commit-retry-timeout: -1
    max-rollback-retry-timeout: -1
    rollback-retry-timeout-unlock-enable: false
    enable-check-auth: true
    enable-parallel-request-handle: true
    retry-dead-threshold: 130000
    xaer-nota-retry-timeout: 60000
    enableParallelRequestHandle: true
    recovery:
      committing-retry-period: 1000
      async-committing-retry-period: 1000
      rollbacking-retry-period: 1000
      timeout-retry-period: 1000
    undo:
      log-save-days: 7
      log-delete-period: 86400000
    session:
      branch-async-queue-size: 5000 #branch async remove queue size
      enable-branch-async-remove: false #enable to asynchronous remove branchSession
  store:
    mode: db
    session:
      mode: db
    lock:
      mode: db
    db:
      datasource: druid
      db-type: mysql
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://${vhost.ip}:3306/seata?rewriteBatchedStatements=true
      user: root
      password: 111111
      min-conn: 10
      max-conn: 100
      global-table: global_table
      branch-table: branch_table
      lock-table: lock_table
      distributed-lock-table: distributed_lock
      query-limit: 1000
      max-wait: 5000
  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login

在 nacos 增加 dataId=seataServer.properties, 单独给 seata-server 开一个 namespace ,和 nacos中业务的namespace区别开,当然两者可以相同

# 数据存储方式,db代表数据库, 可以覆盖 seata中 application.yaml中的配置
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://192.xx.xx.129:3306/seata?useUnicode=true&rewriteBatchedStatements=true&serverTimezone=GMT
 
store.db.user=root
store.db.password=111111
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
# 事务、日志等配置
server.recovery.committingRetryPeriod=3000
server.recovery.asynCommittingRetryPeriod=3000
server.recovery.rollbackingRetryPeriod=3000
server.recovery.timeoutRetryPeriod=3000
server.maxCommitRetryTimeout=-1
server.maxRollbackRetryTimeout=-1
server.rollbackRetryTimeoutUnlockEnable=false
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
 
# 客户端与服务端传输方式
transport.serialization=seata
transport.compressor=none
# 关闭metrics功能,提高性能
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898

然后启动 /bin/seata-server.bat ,看到 nacos注册中心有服务就可以了

微服务应用

这里取 svc-order-service 作为配置举例,svc-stock-service 是一样的

  1. 依赖
<dependencies>
    
    <dependency>
        <groupId>com.alibaba.cloudgroupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    dependency>
    <dependency>
        <groupId>com.alibaba.cloudgroupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
    dependency>
    
    <dependency>
        <groupId>com.alibaba.cloudgroupId>
        <artifactId>spring-cloud-starter-alibaba-seataartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
    <dependency>
        <groupId>org.mybatis.spring.bootgroupId>
        <artifactId>mybatis-spring-boot-starterartifactId>
        <version>2.3.0version>
    dependency>
    <dependency>
        <groupId>com.baomidougroupId>
        <artifactId>mybatis-plus-boot-starterartifactId>
    dependency>

    <dependency>
        <groupId>com.mysqlgroupId>
        <artifactId>mysql-connector-jartifactId>
        <version>8.0.32version>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>
    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
    dependency>
    
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-openfeignartifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-ribbonartifactId>
            exclusion>
        exclusions>
    dependency>
    
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-loadbalancerartifactId>
    dependency>
    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>druid-spring-boot-starterartifactId>
    dependency>
dependencies>
  1. 配置文件

svc-order-service/application.properties:

spring.application.name=svc-order-service
server.port=9091
spring.cloud.nacos.discovery.server-addr=192.xx.xx.129:8848
spring.cloud.nacos.discovery.namespace=dev
spring.cloud.alibaba.seata.tx-service-group=my_test_tx_group
spring.datasource.url=jdbc:mysql://192.xx.xx.129:3306/seata-biz?allowMultiQueries=true
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=111111

spring.datasource.druid.connect-properties.config.decrypt=false
spring.datasource.druid.connect-properties.druid.stat.logSlowSql=true
spring.datasource.druid.connect-properties.druid.stat.slowSqlMillis=200
spring.datasource.druid.filters=config,wall,stat
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=100
spring.datasource.druid.max-wait=60000
spring.datasource.druid.min-evictable-idle-time-millis=30000
spring.datasource.druid.min-idle=5
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.stat-view-servlet.allow=
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.reset-enable=true
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.validation-query=select 1
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.exclusions=/druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico
spring.datasource.druid.web-stat-filter.session-stat-enable=true
spring.datasource.druid.web-stat-filter.session-stat-max-count=10
spring.datasource.druid.web-stat-filter.url-pattern=/*

mybatis-plus.type-aliases-package=com.x.z.order.domain
mybatis-plus.mapper-locations=classpath*:mapper/**/*.xml
spring.main.allow-bean-definition-overriding=true


# seata 注册到nacos中的命名空间可以和应用不相同
seata.enabled=true
seata.application-id=${spring.application.name}
# 事务组
seata.tx-service-group=my_tx_group #(事务级名称可以随便取,但是在seata-server配置中心要对应)
seata.service.vgroupMapping.my_tx_group=default

# 下面和 seata-server 注册信息保持一致
seata.use-jdk-proxy=true
seata.enable-auto-data-source-proxy=true
seata.registry.type=nacos
# 和 seata-server应用名称一致
seata.registry.nacos.application=seata-server
seata.registry.nacos.server-addr=${spring.cloud.nacos.discovery.server-addr}
# 和 seata-server 配置保持一致
seata.registry.nacos.namespace=seata
seata.registry.nacos.group=SEATA_GROUP

# seata-server 所在的配置中心
seata.config.type=nacos
seata.config.nacos.server-addr=${spring.cloud.nacos.discovery.server-addr}
seata.config.nacos.namespace=seata
seata.config.nacos.group=SEATA_GROUP

  1. 向 seata-server 注册事务组

    看 配置文件中有这个

    # 事务组
    seata.tx-service-group=my_tx_group #(事务级名称可以随便取,但是在seata-server配置中心要对应)
    seata.service.vgroupMapping.my_tx_group=default
    

就需要向 seata-server 配置中心注册 dataId= service.vgroupMapping.my_tx_group, value=default(seata-server 配置的中cluster 那个属性的值)

spring cloud 集成 seata 分布式事务_第1张图片

spring cloud 集成 seata 分布式事务_第2张图片
也就是 seata-server 中 application.yml中 cluster 属性值 :
spring cloud 集成 seata 分布式事务_第3张图片

意思就是向 seata-server cluster注册进行事务组注册,如果不配置 就会出现 no available service ‘null‘ found!!问题

有了以下输出信息,就代表分布式事务注册成功:

register TM success. client version:1.3.0, server version:1.6.1,channel:[id: 0x0
register RM success. client version:1.3.0, server version:1.6.1,channel:[id: 0x8
register success, cost 53 ms, version:1.6.1,role:TMROLE,channel:[id: 0x056f8e04
register success, cost 53 ms, version:1.6.1,role:RMROLE,channel:[id: 0x82d3b8b3

问题

no available service ‘null‘ found

原因就是seata-server 注册的cluster 属性 在我们的应用程序中不匹配或者没有找到

请在 seata-server 中配置 dataId= `service.vgroupMapping.my_tx_group`, value=`default(seata-server 配置的中cluster 那个属性的值)`

good luck!

你可能感兴趣的:(cloud-gateway,cloud,seata)