Seata部署以及Spring Cloud应用配置

451 阅读4分钟

简介

Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。 Seata 作为 Spring Cloud Alibaba 的一员,用于分布式服务解决方案中的分布式事务。 本文则主要记录在 Docker 上搭建 Seata Server 的过程,以及在 Spring Cloud 应用中如何配置 Seata Client。

Seata Server版本选择

为更好地结合 Nacos 配置中心,本次使用 Seata 1.4.2版本
同时 Seata Server 与 Seata Client 版本需一致
参考:seata.io/zh-cn/docs/…

官方 Docker 容器部署

自定义Seata Server配置文件

配置可参考 Seata参数配置:seata.io/zh-cn/docs/…
registry用于配置Seata Server注册中心
registry.type注册中心类型,这里指定为nacos
config用于配置Seata Server配置中心
config.type配置中心类型,这里指定为nacos

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "172.30.74.130:8848"
    group = "SEATA_GROUP"
    namespace = ""
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"

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

启动 Seata Server

SEATA_IP,指定在 Nacos Server 注册的 IP,如果是外部 (Seata Client) 访问时,建议指定外网 IP
SEATA_PORT,指定 Seata Server 运行端口
SEATA_CONFIG_NAME,指定配置文件,Seata会自寻registry名称且.conf后缀的文件
/root/seata-config,Seata Server 容器内的配置目录

docker run -d \
  --name seata-server \
  -p 8091:8091 \
  -e SEATA_IP=119.23.47.198 \
  -e SEATA_PORT=8091 \
  -v /data/seata/config:/root/seata-config \
  -e SEATA_CONFIG_NAME=file:/root/seata-config/registry \
  seataio/seata-server:1.4.2

观察运行情况

docker logs -f seata-server

部署在自有 Docker 容器内

此部署方式是在没有可用的官方 Docker 镜像场景之下(比如没有 ARM64 架构镜像)

准备镜像

任何安装了JRE的镜像都可以用来运行Seata Server
这里用的是自己制作的用于ARM64架构平台运行的镜像

REPOSITORY		TAG					IMAGE ID			CREATED					SIZE
weiew/jre8    1.0-arm64 	0e50acc4fcec  15 months ago   84.9MB

进入到容器中下载官方 Seata RELEASE 包
GitHub Seata RELEASE:github.com/seata/seata…

获取 Seata v1.4.2

1.下载 Seata v1.4.2 压缩包

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

2.解压 Seata v1.4.2 压缩包

tar -zxvf seata-server-1.4.2.tar.gz

配置 Seata

查看./conf下的文件
README.md,Seata 相关说明及数据库相关脚本
registry.conf,Seata Server 主要的配置文件,用于配置注册中心配置中心
file.conf,用于配置 Seata Server 的事务日志存储,registry.conf中默认配置并引用了此文件

1.配置注册中心

编辑./conf/registry.conf
registry.type,指定所使用的注册中心,选用nacos

2.配置配置中心

编辑./conf/registry.conf
config.type,指定所使用的注册中心,选用nacos
config.nacos.dataId,使用的配置文件,配置内容参考config.txt
参考 config.txt:github.com/seata/seata…

3.配置示例

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "172.17.0.3:8848"
    group = "SEATA_GROUP"
    namespace = ""
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"

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

Nacos 配置文件

根据 Seata Server 的配置(registry.conf),在 Nacos 控制台新增配置seataServer.properties
配置内容参考config.txt
参考 config.txt:github.com/seata/seata…

启动 Seata Server

-h, --host,指定在 Nacos 注册的 IP,对于部署在容器内或内网环境,建议指定外网 IP
-p, --port,指定 Seata Server 启动的端口,默认 8091
-m, --storeMode,指定事务日志存储方式,支持filedbredis

sh ./bin/seata-server.sh -h 127.0.0.1 -p 8091 -m file

查看服务注册中心

访问 Nacos 控制台:localhost:8848/nacos
服务管理-服务列表
image.png

Spring Boot/Cloud 应用配置

模块及依赖管理

聚合模块的POM

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-alibaba-dependencies</artifactId>
      <version>2.2.7.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

子模块的POM

由于spring-cloud-starter-alibaba-seata中 seata 继承的版本是1.3.0
为保证 seata client 与 seata server 版本一致,另外引入1.4.2版本

<!-- spring cloud alibaba 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:1.4.2 -->
<dependency>
  <groupId>io.seata</groupId>
  <artifactId>seata-spring-boot-starter</artifactId>
  <version>1.4.2</version>
</dependency>

应用属性文件配置

application.yml 配置

seata.config.type,指定配置中心
seata.config.nacos.data-id,指定配置文件,默认值为seata.properties
seata.registry.type,指定注册中心
seata.registry.nacos.application,与 Seata Server 注册到 nacos 的服务名称一致
seata.tx-service-group,指定事务分组,通常将多个事务关联的微服务指定为同一个事务分组
seata.service.vgroup-mapping.default_tx_group,事务分组对应的集群(多对一)
*示例中seata.service.vgroup-mapping.default_tx_group=default需要能在seata.config.nacos.data-id=seataServer.properties此属性文件中找到对应的键值

seata:
  tx-service-group: default_tx_group
  service:
    vgroup-mapping:
      default_tx_group: default
  registry:
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      application: seata-server
      username: nacos
      password: nacos
  config:
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      username: nacos
      password: nacos
      data-id: seataServer.properties

参考

[1] 使用 Docker 部署 Seata Server: seata.io/zh-cn/docs/…
[2] 部署 Server:seata.io/zh-cn/docs/…
[3] GitHub Seata RELEASE:github.com/seata/seata…
[4] Seata参数配置:seata.io/zh-cn/docs/…
[5] 配置文件示例:github.com/seata/seata…
[6] Nacos注册中心:seata.io/zh-cn/docs/…
[7] Nacos配置中心:seata.io/zh-cn/docs/…
[8] 事务分组专题:seata.io/zh-cn/docs/…
[9] AT MySQL: github.com/seata/seata…