一、官方文档

Seata分布式事物解决方案中间件。

https://seata.io/zh-cn/docs/overview/what-is-seata.html

seata那个版本支持postgre数据库_Docker

https://seata.io/zh-cn/docs/overview/what-is-seata.html

Server端配置文件

https://github.com/seata/seata/tree/1.3.0/script/server

seata那个版本支持postgre数据库_Docker

https://github.com/seata/seata/tree/1.3.0/script/server

二、启动和运行

1、单节点运行

1.1、docker方式运行

$ sh /usr/local/deploy/seata-server/docker-run.sh

docker-run.sh 脚本文件

docker run --name seata-server --net=host \
        -e SEATA_IP=172.18.75.176 \
        -e SEATA_PORT=8091 \
        -e STORE_MODE=db \
        -e SERVER_NODE=1 \
        -e SEATA_CONFIG_NAME=file:/root/seata-config/registry \
        -v ./seata-config:/root/seata-config  \
        -v ./logs:/root/logs \
        -d seataio/seata-server:1.3.0

1.2、docker-compose方式运行

$ sh /usr/local/deploy/seata-server/docker-compose-run.sh

docker-compose-run.sh

docker-compose -f docker-compose.yml up -d

2、高可用多节点运行

这块后续需要调整为Helm形式多节点启动执行,目前这种先保证快速运行起来。

$ sh /usr/local/deploy/seata-server/docker-compose-run-ha.sh

docker-compose-run-ha.sh

docker-compose -f docker-compose-ha.yml up -d

三、Seata Server安装和配置

采用Docker方式安装部署。

  • 官方安装文档

https://seata.io/zh-cn/docs/ops/deploy-by-docker.html

1、Seata Docker官方镜像地址

https://hub.docker.com/r/seataio/seata-server

2、控制台拉取镜像

# latest 默认为拉取最新版本

docker pull seataio/seata-server:latest

# 拉取指定版本: 1.4.2

docker pull seataio/seata-server:1.4.2

# 查询拉取的镜像

docker images

seata那个版本支持postgre数据库_Docker_03

3、使用自定义配置文件

官方文档说明,红色为重点说明

seata那个版本支持postgre数据库_java_04

3.1、registry.conf配置文件

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

  nacos {
    # 注册到Nacos的应用名称
    application = "seata-server"
    # nacos服务器注册地址
    serverAddr = "172.18.75.176:8988"
    # 注册到nacos的组名
    group = "SEATA_GROUP"
    # 注册到nacos的命名空间
    namespace = "xxxx-global-dev"
    cluster = "default"
    username = ""
    password = ""
  }
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    weight = "1"
  }
  redis {
    serverAddr = "localhost:6379"
    db = 0
    password = ""
    cluster = "default"
    timeout = 0
  }
  zk {
    cluster = "default"
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""
    password = ""
  }
  consul {
    cluster = "default"
    serverAddr = "127.0.0.1:8500"
    aclToken = ""
  }
  etcd3 {
    cluster = "default"
    serverAddr = "http://localhost:2379"
  }
  sofa {
    serverAddr = "127.0.0.1:9603"
    application = "default"
    region = "DEFAULT_ZONE"
    datacenter = "DefaultDataCenter"
    cluster = "default"
    group = "SEATA_GROUP"
    addressWaitTime = "3000"
  }
  file {
    # 如果是File类型,需要更改,这里采用的是Nacos类型,如果是Docker运行,顺带改了
    # 指定 file.conf 配置文件,则需要在 registry.conf 文件中将 config 配置改为以下内容,name 的值为容器中对应的路径
    name = "file:/root/seata-config/file.conf"
  }
}

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

  file {
    # 指定 file.conf 配置文件,则需要在 registry.conf 文件中将 config 配置改为以下内容,name 的值为容器中对应的路径
    name = "file:/root/seata-config/file.conf"
  }
  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = ""
    group = "SEATA_GROUP"
    username = ""
    password = ""
    dataId = "seataServer.properties"
  }
  consul {
    serverAddr = "127.0.0.1:8500"
    aclToken = ""
  }
  apollo {
    appId = "seata-server"
    ## apolloConfigService will cover apolloMeta
    apolloMeta = "http://192.168.1.204:8801"
    apolloConfigService = "http://192.168.1.204:8080"
    namespace = "application"
    apolloAccesskeySecret = ""
    cluster = "seata"
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""
    password = ""
    nodePath = "/seata/seata.properties"
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
}
  • 配置Nacos注册中心信息

采用的Nacos,只需要关注nacos配置块的配置即可。

seata那个版本支持postgre数据库_分布式_05

  • 配置中心配置

配置中心采用的本地file类型

注意file的路径需要修改为Docker容器中的路径

seata那个版本支持postgre数据库_Docker_06

3.2、file.conf配置文件

## transaction log store, only used in seata-server
store {
  ## store mode: file、db、redis
  mode = "db"
  ## rsa decryption public key
  publicKey = ""
  ## file store property
  file {
    ## store location dir
    dir = "sessionStore"
    # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
    maxBranchSessionSize = 16384
    # globe session size , if exceeded throws exceptions
    maxGlobalSessionSize = 512
    # file buffer size , if exceeded allocate new buffer
    fileWriteBufferCacheSize = 16384
    # when recover batch read size
    sessionReloadReadSize = 100
    # async, sync
    flushDiskMode = async
  }

  ## 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.jdbc.Driver"
    ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
    url = "jdbc:mysql://172.18.75.176:3306/seata?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&autoReconnect=true"
    user = "xxx_global_rw"
    password = "hYO4BLE"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }

  ## redis store property
  redis {
    ## redis mode: single、sentinel
    mode = "single"
    ## single mode property
    single {
      host = "127.0.0.1"
      port = "6379"
    }
    ## sentinel mode property
    sentinel {
      masterName = ""
      ## such as "10.28.235.65:26379,10.28.235.65:26380,10.28.235.65:26381"
      sentinelHosts = ""
    }
    password = ""
    database = "0"
    minConn = 1
    maxConn = 10
    maxTotal = 100
    queryLimit = 100
  }
}

如果需要做高可用,采用db模式为最佳

只需关注db配置块的信息即可

  • 设置存储类型为db

seata那个版本支持postgre数据库_微服务_07

  • 配置db连接、用户名和密码等相关信

seata那个版本支持postgre数据库_java_08

4、新建DB库和表

db模式需要在数据库创建 global_table , branch_table , lock_table

  • 参考文档

https://seata.io/zh-cn/docs/ops/deploy-ha.html

seata那个版本支持postgre数据库_Docker

https://seata.io/zh-cn/docs/ops/deploy-ha.html

seata那个版本支持postgre数据库_微服务_10

 4.1、MySQL DB脚本

-- -------------------------------- The script used when storeMode is 'db' --------------------------------
-- the table to store GlobalSession data
CREATE TABLE IF NOT EXISTS `global_table`
(
    `xid`                       VARCHAR(128) NOT NULL,
    `transaction_id`            BIGINT,
    `status`                    TINYINT      NOT NULL,
    `application_id`            VARCHAR(32),
    `transaction_service_group` VARCHAR(32),
    `transaction_name`          VARCHAR(128),
    `timeout`                   INT,
    `begin_time`                BIGINT,
    `application_data`          VARCHAR(2000),
    `gmt_create`                DATETIME,
    `gmt_modified`              DATETIME,
    PRIMARY KEY (`xid`),
    KEY `idx_gmt_modified_status` (`gmt_modified`, `status`),
    KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store BranchSession data
CREATE TABLE IF NOT EXISTS `branch_table`
(
    `branch_id`         BIGINT       NOT NULL,
    `xid`               VARCHAR(128) NOT NULL,
    `transaction_id`    BIGINT,
    `resource_group_id` VARCHAR(32),
    `resource_id`       VARCHAR(256),
    `branch_type`       VARCHAR(8),
    `status`            TINYINT,
    `client_id`         VARCHAR(64),
    `application_data`  VARCHAR(2000),
    `gmt_create`        DATETIME(6),
    `gmt_modified`      DATETIME(6),
    PRIMARY KEY (`branch_id`),
    KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
    `row_key`        VARCHAR(128) NOT NULL,
    `xid`            VARCHAR(128),
    `transaction_id` BIGINT,
    `branch_id`      BIGINT       NOT NULL,
    `resource_id`    VARCHAR(256),
    `table_name`     VARCHAR(32),
    `pk`             VARCHAR(36),
    `gmt_create`     DATETIME,
    `gmt_modified`   DATETIME,
    PRIMARY KEY (`row_key`),
    KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

CREATE TABLE IF NOT EXISTS `distributed_lock`
(
    `lock_key`       CHAR(20) NOT NULL,
    `lock_value`     VARCHAR(20) NOT NULL,
    `expire`         BIGINT,
    primary key (`lock_key`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);

seata/script/server/db at develop · seata/seata · GitHub:fire: Seata is an easy-to-use, high-performance, open source distributed transaction solution. - seata/script/server/db at develop · seata/seata

seata那个版本支持postgre数据库_java_11

https://github.com/seata/seata/tree/develop/script/server/db

4.2、最终执行后的DB库和表

seata那个版本支持postgre数据库_Docker_12

5、Docker启动运行

特别注意:如果Nacos和MySQL数据不是采用Docker安装的,Docker容器需要访问宿主机的网络,需要特别的设置。特别是Mac系统

  • 参考文档

https://swiftflamel.com/2021/04/01/docker-container%e5%ae%b9%e5%99%a8%e5%86%85%e8%ae%bf%e9%97%ae%e5%ae%bf%e4%b8%bb%e6%9c%bahost%e6%9c%8d%e5%8a%a1/

seata那个版本支持postgre数据库_Docker

https://swiftflamel.com/2021/04/01/docker-container%E5%AE%B9%E5%99%A8%E5%86%85%E8%AE%BF%E9%97%AE%E5%AE%BF%E4%B8%BB%E6%9C%BAhost%E6%9C%8D%E5%8A%A1/How to access host port from docker container - Stack Overflow

seata那个版本支持postgre数据库_Docker

https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container/43541732#43541732

seata那个版本支持postgre数据库_分布式_15

5.1、Docker运行脚本

  • 启动环境变量参考文档
  • 使用 Docker 部署 Seata Server使用 Docker 部署 Seata Server

    seata那个版本支持postgre数据库_java_16

    https://seata.io/zh-cn/docs/ops/deploy-by-docker.html
docker run --name seata-server --net=host \
        -e SEATA_IP=172.18.75.176 \
        -e SEATA_PORT=8091 \
        -e STORE_MODE=db \
        -e SERVER_NODE=1 \
        -e SEATA_CONFIG_NAME=file:/root/seata-config/registry \
        -v /usr/local/deploy/seata-server/seata-config:/root/seata-config  \
        -v /usr/local/deploy/seata-server/logs:/root/logs \
        -d seataio/seata-server:1.4.2


  • --name seata-server 启动的容器名称
  • --net=host 本地网络模式【Nacos没有采用Docker方式启动,方便Docker容器访问宿主机网络】
  • -e SEATA_IP=172.18.75.176 指定seata-server启动的IP, 该IP用于向注册中心注册时使用, 如eureka、nacos等
  • -e SEATA_PORT=8091 指定seata-server启动的端口, 默认为 8091
  • -e STORE_MODE=db 指定seata-server的事务日志存储方式, 支持db ,file,redis(Seata-Server 1.3及以上版本支持), 默认是 file
  • -e SERVER_NODE=1 用于指定seata-server节点ID, 如 1,2,3..., 默认为 根据ip生成
  • -e SEATA_CONFIG_NAME=file:/root/seata-config/registry 指定配置文件位置
  • -v /usr/local/deploy/seata-server/seata-config:/root/seata-config 挂载配置目录卷
  • /usr/local/deploy/seata-server/logs:/root/logs 挂载日志目录卷
  • -d seataio/seata-server:1.4.2 后台启动运行镜像文件

5.2、docker-compose运行启动

# 后台运行seata-server
docker-compose -f docker-compose.yml up -d
  • docker-compose.yml配置文件(单节点)
version: "3"
services:
  # 指定服务名称
  seata-server-1:
    # 指定服务使用的镜像
    image: seataio/seata-server:1.4.2
    # 指定容器名称
    container_name: seata-server-1
    # 指定服务运行的端口
    #ports:
    #  - 8091:8091
    # 指定容器中需要挂载的文件
    volumes:
      # 挂载的配置文件目录
      - /usr/local/deploy/seata-server/seata-config:/root/seata-config
      # 挂载的日志目录
      - /usr/local/deploy/seata-server/logs:/root/logs
      # 指定容器的环境变量
    environment:
      # 指定seata-server启动的IP, 该IP用于向注册中心注册时使用,比如naocs注册中心
      # 默认使用的内网ip,seata-client通过nacos注册中心连接时,获取seata-server获取到的连接地址
      - SEATA_IP=172.18.75.176
      # 指定seata-server启动的端口, 默认为 8091
      - SEATA_PORT=8091
      # 指定seata-server的事务日志存储方式, 支持db ,file,redis(Seata-Server 1.3及以上版本支持), 默认是 file
      - STORE_MODE=db
      # 用于指定seata-server节点ID, 如 1,2,3..., 默认为 根据ip生成
      - SERVER_NODE=1
      # 指定配置文件位置
      - SEATA_CONFIG_NAME=file:/root/seata-config/registry
      # 设置网络模式
    network_mode: "host"

 很关键,很核心的配置

seata那个版本支持postgre数据库_docker_17

6、容器命令行及查看日志

6.1、进入容器

$ docker exec -it seata-server sh

seata那个版本支持postgre数据库_微服务_18

 6.1、查看日志

$ docker logs -f seata-server

这些日志可以在启动运行时,挂载映射日志目录中查看

# 挂载的日志目录

- /usr/local/deploy/seata-server/logs:/root/logs  

 

seata那个版本支持postgre数据库_Docker_19