seata分布式事务配置

nacos下载https://github.com/alibaba/nacos/releases
这里用的是nacos2.0.3。

nacos配置

找到conf/nacos-mysql.sql,创建nacos数据库,初始化数据库脚本,数据库和application.properties 配置保持一致
修改 nacos/conf/application.properties 文件的以下内容。

#*************** Config Module Related Configurations ***************#
### If user MySQL as datasource:
# 指定数据源为 MySQL
spring.datasource.platform=mysql
### Count of DB:
# 数据库实例数量
db.num=1
# 数据库连接信息,如果是 MySQL 8.0+ 版本需要添加 serverTimezone=Asia/Shanghai
### Connect URL of DB:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&serverTimezone=Asia/Shanghai
db.user=root
db.password=123456

nohup sh startup.sh -m standalone & 后台启动命令
sh shutdown.sh 关闭
如果你是ubuntu,记得用 bash -f 代替 sh

vi startup.sh 单独修改单机启动。
export MODE="standalone"

在nacos上新建一个命名空间,seata专用。
下载seata1.4.2:https://github.com/seata/seata/releases
下载一个mysql-connector-java-8.0.21.jar放在lib文件夹里面,如果里面有就不需要下载了,注意看下自己的数据库版本,
和mysql驱动版本对应即可。
创建seata数据库,导入以下sql脚本
https://github.com/seata/seata/blob/develop/script/server/db/mysql.sql
在需要分布式事务的数据库,导入以下脚本
https://github.com/seata/seata/blob/develop/script/client/at/db/mysql.sql

接下来配置file.conf和register.conf

file.conf  mode 修改为db 和 下面的数据库配置修改
store {
  ## store mode: file、db、redis
  mode = "db"  
  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"
    ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
    url = "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&serverTimezone=Asia/Shanghai"
    user = "root"
    password = "123456"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
    }
}
register.conf 修改内容
registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"
  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848" #nacos地址
    group = "SEATA_GROUP"
    namespace = "1d687621-ec19-485c-8f35-4cdfe3c4e31c" #seata专用命名空间
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
}
config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"
  nacos {
    serverAddr = "127.0.0.1:8848" #nacos地址
    namespace = "1d687621-ec19-485c-8f35-4cdfe3c4e31c" #seata专用命名空间
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
    dataId = "seataServer.properties"
  }
}

https://github.com/seata/seata/blob/develop/script/config-center/nacos/nacos-config.sh 放在seata的conf目录。
https://github.com/seata/seata/blob/develop/script/config-center/config.txt 。只需要里面一部分内容即可。
在seata的conf目录上一层目录 新建文件config.txt ,使用nacos-config.sh 把config.txt的配置导入nacos。

vi config.txt  

service.vgroupMapping.wei_tx_group=default  #记住这个group,代码里面有用
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&serverTimezone=Asia/Shanghai
store.db.user=root
store.db.password=123456
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

导入配置命令,win 在 git bash 上执行,在nacos-config.sh 所在的目录打开 git bash
执行 sh nacos-config.sh -h ip地址 -p 8848 -g SEATA_GROUP -t 命名空间ID -u nacos -w nacos

启动seata: nohup sh seata-server.sh &

代码配置
seata:
  tx-service-group: wei_tx_group  #config.txt  里面的 group
  enabled: true
  enable-auto-data-source-proxy: false #设为true就不用再手动配置数据源代理
  config:
    type: nacos
    nacos:
      namespace: 1d687621-ec19-485c-8f35-4cdfe3c4e31c
      serverAddr: 127.0.0.1:8848
      group: SEATA_GROUP
      username: nacos
      password: nacos
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace: 1d687621-ec19-485c-8f35-4cdfe3c4e31c
      username: nacos
      password: nacos

引入jar包的时候注意seata版本和自己下载的版本一致。

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!--和自己下载的seata版本对应-->
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.4.2</version>
</dependency>

另一个非常重要的事情就是 版本对应关系,一定要和下面的官方版本分别对应,要不然可能遇到很多未知的bug问题。
比如 我遇到的feign的 fallback 不生效。
Spring Cloud Alibaba Version & Spring Cloud Version & Spring Boot Version 一定按照下面的链接配置。
Nacos、Seata 等其他组件的版本 暂不用那么严格按照官方来下载,自己看着来就行。
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值