SpringCloud集成Seata配置

1.1 安装配置Seata服务端

Seata服务端下载页面提供了服务端的压缩包,也可以直接通过命令来下载seata-server-1.4.2

wget https://github.com/seata/seata/releases/download/v1.4.2/seata-server-1.4.2.tar.gz

压缩包解压后的目录如下:

[root@lizhi seata-server-1.4.2]# ll
total 24
drwxr-xr-x 3 root root     90 Feb 14 17:57 bin
drwxr-xr-x 4  502 games   156 Feb 14 17:57 conf
drwxr-xr-x 3  502 games  8192 Feb 14 13:35 lib
-rw-r--r-- 1  502 games 11365 May 13  2019 LICENSE
drwxr-xr-x 2  502 games   136 Feb 14 17:58 logs

进入到conf/目录中,修改配置文件

-rw-r--r-- 1 502 games 1859 Feb 14 13:27 file.conf
-rw-r--r-- 1 502 games 3112 Apr 25  2021 file.conf.example
drwxr-xr-x 2 502 games  114 Feb 14 10:50 logback
-rw-r--r-- 1 502 games 2222 Apr 25  2021 logback.xml
drwxr-xr-x 3 502 games   22 Apr 25  2021 META-INF
-rw-r--r-- 1 502 games 1324 Apr 25  2021 README.md
-rw-r--r-- 1 502 games 1327 Apr 25  2021 README-zh.md
-rw-r--r-- 1 502 games 1976 Feb 14 17:57 registry.conf

修改registry.conf文件,配置nacos作为注册中心和配置中心

# 注册中心
registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "SEATA_GROUP"
    namespace = "24712b7c-05ad-4b79-af97-1d202431f521"
    cluster = "default"
    username = ""
    password = ""
  }
}

# 配置中心
config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"

  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = ""
    group = "SEATA_GROUP"
    username = ""
    password = ""
    dataId = "seataServer.properties"
  }
}

除了配置注册中心和配置中心外,还需要配置数据存储方式,修改file.conf文件,将存储模式改为db,并修改数据库的连接信息

store {
  ## store mode: file、db、redis
  mode = "db"
  ## rsa decryption public key
  publicKey = ""

  ## 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"
    ## 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"
    user = "root"
    password = "lz123"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }

注意:如果使用的是MySQL8.0以上,需要将数据库驱动类的名称改为com.mysql.cj.jdbc.Driver,并在lib目录中下载mysql-connector-java-8.0.27.jar

wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.27/mysql-connector-java-8.0.27.jar

1.2 配置初始化参数

方法一:

在较低的版本中,seata-server解压完之后,在conf目录下还有一个config.txt文件,里面记录了Seata启动时必须的初始化参数,但seata-server-1.4.2中并没有这个文件

我们可以在https://github.com/seata/seata/tree/develop/script/config-center中看到这个配置文件,下载这个配置,修改里面的参数

如果配置了Nacos作为配置中心,然后再下载https://github.com/seata/seata/tree/develop/script/config-center/nacos中的nacos-config.sh,然后执行脚本,将config.txt中的初始化参数加载到nacos的配置中心

https://github.com/seata/seata/tree/develop/script/config-center下面,为每种配置中心的启动脚本都有详细的参数介绍

方法二:

方法一这种方式,相对来说是比较复杂的,在seata-server-1.4.2以后的版本中,我们可以在registry.conf中配置配置中心时,就可以通过dataId来创建配置

registry.conf的nacos配置中心的dataId默认为seataServer.properties,我们在Nacos的控制台就可以直接创建一个这样的配置,其中的配置的内容就是config.txt的内容

注:配置的时候需要特别注意,如果Seata服务端与MySQL服务端在同一台服务器,一定要注意数据库连接时会使用localhost的用户名和密码,否则就会报the {store.db.driverClassName} can't be empty,这样的问题有时候很难排查

1.3 创建数据库表

如果存储方式配置的是db类型,那么就需要创建几张表来存储事务信息

https://github.com/seata/seata/tree/develop/script/server/db提供了相应的建表语句

在建表前,首先要创建配置中指定的数据库名,比如seata

1.4 启动Seata服务端

在前面这些都配置好了之后,就可以启动seata-server了

nohup ./seata-server.sh -p 18091 -n 1 >seata.log 2>&1 &
-h: 注册到注册中心的ip
-p: Server rpc 监听端口
-m: 全局事务会话信息存储模式,file、db、redis,优先读取启动参数 (Seata-Server 1.3及以上版本支持redis)
-n: Server node,多个Server时,需区分各自节点,用于生成不同区间的transactionId,以免冲突
-e: 多环境配置参考 http://seata.io/en-us/docs/ops/multi-configuration-isolation.html

可以启动两个服务端,然后就可以在nacos控制台,看到服务注册的信息

1.5 客户端配置

SpringCloud整合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>
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.4.2</version>
</dependency>

注:由于cloud中的seata-spring-boot-starter不是最新的,所以将其排除,单独添加最新的依赖

然后就是在yml中添加配置,客户端与服务端的配置要保持一致

seata:
  enabled: true
  application-id: ${spring.application.name}
  # 客户端和服务端在同一个事务组
  tx-service-group: shop-public
  # 自动数据源代理
  enable-auto-data-source-proxy: true
  # 数据源代理模式(分布式事务方案)
  data-source-proxy-mode: AT
  # 事务群组,配置项值为TC集群名,需要与服务端保持一致
  service:
    vgroup-mapping:
      shop-public: default
  #整合nacos配置中心
  config:
    type: nacos
    nacos:
      server-addr: xxx.com:8848
      group: SEATA_GROUP
      namespace: 24712b7c-05ad-4b79-af97-1d202431f521
      data-id: seataServer.properties
  #整合nacos注册中心
  registry:
    type: nacos
    nacos:
      server-addr: xxx.com:8848
      group: SEATA_GROUP
      namespace: 24712b7c-05ad-4b79-af97-1d202431f521
      # 默认TC集群名
      cluster: default
      # 服务名,与服务端中registry.conf配置要一致
      application: seata-server

由于需要配置数据源代理模式(默认为AT),还有sagatcc等方式,这些方式都需要创建表,而相关表模型也都有准备

https://github.com/seata/seata/tree/develop/script/client中提供了相关的表模。.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值