seata搭建使用

1、下载seata上传到服务器已经数据库建表略过

2、linux服务器seata配置

file.conf

store {
  ## store mode: file、db、redis
  mode = "db"
  ## database store property
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
    datasource = "druid"
    ## mysql/oracle/postgresql/h2/oceanbase etc.
    dbType = "mysql"
    driverClassName = "com.mysql.cj.jdbc.Driver"
    url = "jdbc:mysql://localhost:3306/seata?useUnicode=true"
    user = "root"
    password = "root"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }
)

registry.conf

registry  {
  type = "nacos"
  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "SEATA_GROUP"
    namespace = "namespace"
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
}
config {
  type = "nacos"
  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = "namespace"
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
  }
}

3、nacos seata配置

service.vgroupMapping.ruoyi-system-group=default
store.mode=db
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
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.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000

我是配合若依框架使用的,这些也是从若依框架拿下来的配置,更详细的可以参考seata和若依官网。

seata搭建使用

等号前面是Data ID,后面是文件内容,以第一个为例,内容如下

seata搭建使用

此时启动seata即可

4、配合项目使用

假设使用seata的调用方为consumer服务,此时consumer需要远程调用payment服务,在comsumer中加入下方依赖:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>

加入依赖后,在远程调用的方法上加入@GlobalTransactional(rollbackFor = Exception.class)注解,并在application.yml中加上如下配置

# seata配置
seata:
  # 默认关闭,如需启用spring.datasource.dynami.seata需要同时开启
  enabled: true
  # Seata 应用编号,默认为 ${spring.application.name}
  application-id: ${spring.application.name}
  # Seata 事务组编号,用于 TC 集群名
  tx-service-group: ${spring.application.name}-group
  # 关闭自动代理
  enable-auto-data-source-proxy: true
  # 服务配置项
  service:
    # 虚拟组和分组的映射
    vgroup-mapping:
      consumer-group: default
  config:
    type: nacos
    nacos:
      serverAddr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace: namespace
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      namespace: namespace

在payment被调用的方法中加上@Transactional(rollbackFor = Exception.class)注解,并在application.yml中加上如下配置

# seata配置
seata:
  # 默认关闭,如需启用spring.datasource.dynami.seata需要同时开启
  enabled: true
  # Seata 应用编号,默认为 ${spring.application.name}
  application-id: ${spring.application.name}
  # Seata 事务组编号,用于 TC 集群名
  tx-service-group: ${spring.application.name}-group
  # 关闭自动代理
  enable-auto-data-source-proxy: true
  # 服务配置项
  service:
    # 虚拟组和分组的映射
    vgroup-mapping:
      payment-group: default
  config:
    type: nacos
    nacos:
      serverAddr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace: namespace
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      namespace: namespace

两者的application配置中除了vgroup-mapping:中的服务名称不一样外其他配置都一样,而上述的paymentconsumer的服务名要和注册到nacos中的服务名一样。

5、在seata命名空间中添加配置

名称为service.vgroupMapping.服务名-group内容为default格式为TEXT即可,如上述案例

service.vgroupMapping.consumer-groupservice.vgroupMapping.payment-group两者都需要添加

6、结束语

本人刚入职开发行业,第一次写文档,无论是技术有不正确或者不严谨的地方、还是文档格式排版不合适的地方欢迎大家指正,我一定积极学习。

上一篇:冬季实战营第四期学习报告


下一篇:冬季实战营第三期学习报告