docker下安装Seata 1.5.1并注册、配置在nacos,存储于MySQL

Seata 1.5.1整合nacos

环境:centos7.6 docker20.0.5 nacos1.4.2 seata1.5.1 mysql5.7.34(seata1.5.1原生不支持MySQL8需要自己添加驱动,麻烦建议使用5.7,够用了)

seata在1.5版本后将配置文件改为了使用易读性更强的yaml文件(也可以继续使用以前的配置,如果你愿意开倒车的话)

我先默认已经安装好了nacos和MySQL为前提进行以下操作:

前置准备:先拉镜像启动并拷贝镜像内自带的配置文件

docker pull seata-server:1.5.1

启动seata-servcer实例,防火墙开放相应端口

docker run --name seata-server -p 8091:8091 -p 7091:7091 seataio/seata-server:1.5.1

使用自定义配置文件

自定义配置文件需要通过挂载文件的方式实现,将宿主机上的 application.yml 挂载到容器中相应的目录

首先将resources目录文件拷出的临时容器

docker cp seata-serve:/seata-server/resources /var/docker/seata/config/

此时你可以在本地看见容器内的resources目录已经完整拷贝进了宿主机目录

我们只需要改动application.yml里的配置,如何写?application.example.yml有完整的配置,直接复制过来,ip改宿主机的地址。

例如:

server:
  port: 7091

spring:
  application:
    name: seata-server

logging:
  config: classpath:logback-spring.xml
  file:
    path: ${user.home}/logs/seata
  extend:
    logstash-appender:
      destination: 127.0.0.1:4560
    kafka-appender:
      bootstrap-servers: 127.0.0.1:9092
      topic: logback_to_logstash
#这里我改了登录时默认的用户id和psw
console:
  user:
    username: admin
    password: admin

seata:
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos
    nacos:
      server-addr: localhost:8848
      namespace:
      group: SEATA_GROUP
      username:
      password:
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key: ""
      #secret-key: ""
      data-id: seataServer.properties
  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos
    preferred-networks: 30.240.*
    nacos:
      application: seata-server
      server-addr: localhost:8848
      group: SEATA_GROUP
      namespace:
      cluster: default
      username:
      password:
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key: ""
      #secret-key: "" 
  store:
    # support: file 、 db 、 redis 这里选择MySQL作为存储方式,如果你需要MySQL8,那么你得自己添加驱动
    mode: db
    db:
      datasource: druid
      db-type: mysql
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/seata?rewriteBatchedStatements=true
      user: root
      password: root
      min-conn: 5
      max-conn: 100
      global-table: global_table
      branch-table: branch_table
      lock-table: lock_table
      distributed-lock-table: distributed_lock
      query-limit: 100
      max-wait: 5000
#  server:
#    service-port: 8091 #If not configured, the default is '${server.port} + 1000'
  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login

我们选择配置和注册中心都是nacos 直接将application.example.yml里关于nacos的配置复制,注意,如果你是运行在云服务器上,那么nacos和MySQL的ip应该是你的公网ip,而不是内网ip,我的云服务器用内网ip出现了无法注册的情况。

选择MySQL作为存储方式

首先给seata建表:

相较于1.4.X,1.5.X多了几张表。

官方sql链接:https://github.com/seata/seata/blob/develop/script/server/db/mysql.sql

建立seata库后,执行该sql:

-- -------------------------------- 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_status_gmt_modified` (`status` , `gmt_modified`),
    KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

-- 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 = utf8mb4;

-- 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),
    `status`         TINYINT      NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
    `gmt_create`     DATETIME,
    `gmt_modified`   DATETIME,
    PRIMARY KEY (`row_key`),
    KEY `idx_status` (`status`),
    KEY `idx_branch_id` (`branch_id`),
    KEY `idx_xid_and_branch_id` (`xid` , `branch_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

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

还记得之前docker启动起来的seata么?他的使命已经完成,rm他,(当然你也可以把我们修改好的application.yml拷进容器再启动,如果你不怕麻烦的话),然后我们再映射好路径启动一个新的容器:

  • 指定 application.yml
docker run --name seata-server         
        -p 8091:8091         
        -p 7091:7091         
        -v /var/docker/seata/config/resources:/seata-server/resources  \       
        seataio/seata-server:1.5.1

进入ncaos查看是否已经注册,进入7091端口查看服务是否正常启动。
image
image

完事,yaml文件的方式易读性高了很多,docker-compose的部署方式差别不大,官网都有。
https://seata.io/zh-cn/docs/ops/deploy-by-docker-compose.html

posted @ 2022-06-24 01:11  ChiHsien  阅读(1799)  评论(0编辑  收藏  举报