使用 Docker 部署 Seata Server(分布式事务解决方式)

1.获取镜像

## 使用下面命令获取最新版本的镜像,此时我的版本是1.3.0

## 或者可以使用docker pull seataio/seata-server:latest获取最新的镜像

docker pull seataio/seata-server:1.3.0

 

2.运行容器并获取配置

为方便我们对seata配置修改我们需要先运行下seata-server,然后将配置拷贝到宿主机中,完成之后删除之前配置并通过宿主机运行容器。

## 运行容器

docker run --name seata-server -p 8091:8091 -d  seataio/seata-server:1.3.0

## 将容器中的配置拷贝到/data/seata

docker cp seata-server:/seata-server /data/seata

## 完成后就会在/data/seata出现容器的配置,我们现在可以将原来容器停止并删除

docker stop seata-server

docekr rm -f seata-server

 

3.修改配置文件

进入目录/data/seata/resources中修改file.confregistry.conf中的内容

file.conf中的内容:

## transaction log store, only used in seata-server

store {

  ## store mode: filedbredis

  mode = "db" ## 原来为file

  ## 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"

    ## 因为设置为db,所以需要选择数据库,这里设置数据库及密码,seata是需要创建的,默认的表是通过脚本运行得到的

    url = "jdbc:mysql://172.18.99.38:3306/seata"

    user = "root"

    password = "admindev123$%^"

    minConn = 5

    maxConn = 30

    globalTable = "global_table"

    branchTable = "branch_table"

    lockTable = "lock_table"

    queryLimit = 100

    maxWait = 5000

  }

  ## redis store property

  redis {

    host = "127.0.0.1"

    port = "6379"

    password = ""

    database = "0"

    minConn = 1

    maxConn = 10

    queryLimit = 100

  }

registry.conf内容:

registry {

  # file nacos eurekarediszkconsuletcd3sofa

  ## 我们这里使用nacos作为注册中心,所以需要设置typenacos并设置nacos的属性

  type = "nacos"

  nacos {

    application = "seata-server"

    serverAddr = "172.18.99.36:8844"

    group = "SEATA_GROUP"

    ## 这里的namespace是你nacos服务的namespacedata-id,设置那里,到时就会在那个namespace,不设置就填public

    namespace = "public"

    cluster = "default"

    username = "nacos"

    password = "nacos"

  }

}

config {

  # filenacos apollozkconsuletcd3

  type = "file"

  file {

    name = "file.conf"

  }

}

4.初始化数据库

这里配置完成后,需要初始化数据库,sql如下

  4.1 seata库执行sql

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(96),

    `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;

  4.2 业务数据库执行

CREATE TABLE `undo_log` (

  `id` bigint(20) NOT NULL AUTO_INCREMENT,

  `branch_id` bigint(20) NOT NULL,

  `xid` varchar(100) NOT NULL,

  `context` varchar(128) NOT NULL,

  `rollback_info` longblob NOT NULL,

  `log_status` int(11) NOT NULL,

  `log_created` datetime NOT NULL,

  `log_modified` datetime NOT NULL,

  PRIMARY KEY (`id`),

  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)

)  ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

5.启动seata

docker run -d --restart always

--net=host

--name  seata-server

-p 8091:8091  -v /data/seata/seata-server:/seata-server

-e SEATA_IP=172.18.99.36 -e SEATA_PORT=8091

seataio/seata-server:1.3.0

 

6.查看nacos

 

7.环境变量

seata-server 支持以下环境变量:

SEATA_IP

可选, 指定seata-server启动的IP, IP用于向注册中心注册时使用, eureka

SEATA_PORT

可选, 指定seata-server启动的端口, 默认为 8091

STORE_MODE

可选, 指定seata-server的事务日志存储方式, 支持db file, 默认是 file

SERVER_NODE

可选, 用于指定seata-server节点ID, 1,2,3..., 默认为 1

SEATA_ENV

可选, 指定 seata-server 运行环境, dev, test , 服务启动时会使用 registry-dev.conf 这样的配置

SEATA_CONFIG_NAME

可选, 指定配置文件位置, file:/root/registry, 将会加载 /root/registry.conf 作为配置文件,如果需要同时指定 file.conf文件,需要将registry.confconfig.file.name的值改为类似file:/root/file.conf

纵有白头俱老意,奈何缘浅路芊芊.
原文地址:https://www.cnblogs.com/hanby/p/15514393.html