docker安装seata

原文链接

docker 运行 seata-server 配置nacos

拉取镜像

docker pull seataio/seata-server

运行镜像

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

获取配置文件

docker cp seata-server:/seata-server  /opt/seata/

停止并删除服务

docker stop seata-server
docker rm seata-server

启动seata

docker run -d --restart always  --name  seata-server -p 8091:8091 -e SEATA_IP=192.168.56.102  -v /opt/seata/seata-server:/seata-server  seataio/seata-server

SEATA_IP为宿主机的ip, 用于seata向nacos注册服务的ip

进入seata配置目录

cd /opt/seata/seata-server/resources

修改配置文件

修改registry.conf

registry {
  # 修改type 改为nacos
  type = "nacos"
  # 修改nacos的配置
  nacos {
    application = "seata-server"
    serverAddr = "192.168.56.102:8848"
    group = "SEATA_GROUP"
    namespace = "efc565c4-9bba-46b0-b2d5-9e2f9bd75c01"
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
  ...
  
  config {
      # 修改为nacos
      type = "nacos"
      # 修改nacos的配置
      nacos {
        serverAddr = "192.168.56.102:8848"
        namespace = "efc565c4-9bba-46b0-b2d5-9e2f9bd75c01"
        group = "SEATA_GROUP"
        username = "nacos"
        password = "nacos"
      }
  }

重新启动nacos

docker restart seata-server

注意:

这样运行会出现一个错误, 我们查看日志: docker logs seata-server, 但是我们在docker可以ping通宿主机

java.lang.RuntimeException: ErrCode:500, ErrMsg:failed to req API:/nacos/v1/ns/instance after all servers([192.168.56.102:8848]) tried: java.net.NoRouteToHostException: No route to host (Host unreachable)
at io.seata.core.rpc.netty.NettyServerBootstrap.start(NettyServerBootstrap.java:157) ~[seata-core-1.4.0-SNAPSHOT.jar:na]
at io.seata.core.rpc.netty.AbstractNettyRemotingServer.init(AbstractNettyRemotingServer.java:55) ~[seata-core-1.4.0-SNAPSHOT.jar:na]
at io.seata.core.rpc.netty.NettyRemotingServer.init(NettyRemotingServer.java:52) ~[seata-core-1.4.0-SNAPSHOT.jar:na]
at io.seata.server.Server.main(Server.java:102) ~[classes/:na]
Caused by: com.alibaba.nacos.api.exception.NacosException: failed to req API:/nacos/v1/ns/instance after all servers([192.168.56.102:8848]) tried: java.net.NoRouteToHostException: No route to host (Host unreachable)

...

错误解决方案: docker访问宿主机:No route to host (Host unreachable)

  • 解决No route to host (Host unreachable)

测试可用

firewall-cmd --zone=public --add-port=8848/tcp --permanent
firewall-cmd --reload
BCLwi8.jpg
BCL5z4.jpg

这边的ip明显是不对的, 这个ip是docker容器ip, 我们已经做了宿主机和docker容器端口映射, 我们需要把宿主机的ip让seat-server去注册

我们需要在添加一个参数: -e SEATA_IP=192.168.56.102

(前面脚本已经修改,应不会出现此问题了)

docker run -d --restart always  --name  seata-server -p 8091:8091 -e SEATA_IP=192.168.56.102  -v /opt/seata/seata-server:/seata-server  seataio/seata-server

删掉seata-server容器,重新运行脚本

配置mysql

  • 新建库

seata

  • 新建表

at模式, 这张表客户端用的, 不建表不影响启动。

CREATE TABLE IF NOT EXISTS `undo_log`
(
    `branch_id`     BIGINT(20)   NOT NULL COMMENT 'branch transaction id',
    `xid`           VARCHAR(100) NOT NULL COMMENT 'global transaction id',
    `context`       VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
    `rollback_info` LONGBLOB     NOT NULL COMMENT 'rollback info',
    `log_status`    INT(11)      NOT NULL COMMENT '0:normal status,1:defense status',
    `log_created`   DATETIME(6)  NOT NULL COMMENT 'create datetime',
    `log_modified`  DATETIME(6)  NOT NULL COMMENT 'modify datetime',
    UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = INNODB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';

下面3张表要建

-- -------------------------------- 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(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;
BCx6UI.jpg
  • file.conf配置文件
# 修改
  mode = "db"

## 修改链接配置
  db {
	datasource = "druid"
    dbType = "mysql"
    driverClassName = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://192.168.56.102:3306/seata"
    user = "root"
    password = "root"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }

重启

  • seata的版本?

2020年10月21日的最新版, 为1.3.0之后的一个版本