docker

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > docker > docker本地seata分布式环境搭建

docker实现本地seata分布式环境搭建详解

作者:人间处处有套路

这篇文章主要为大家详细介绍了docker实现本地seata分布式环境搭建的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考下

使用docker构建容器前提是本机已经安装docker,没有安装docker的可以使用安装包进行安装

1. docker 搭建mysql数据库

1.1 创建网桥确保mysql、nacos、seata容器能正常通信

docker network create -d bridge net-seata

1.2 准备nacos和seata数据库初始化脚本

-- -------------------------------- The script used when storeMode is 'db' --------------------------------
CREATE DATABASE IF NOT EXISTS `seata`;
CREATE DATABASE IF NOT EXISTS `nacos`;
-- -------初始化nacos建表sql----------
USE nacos;
/******************************************/
/*   表名称 = config_info                  */
/******************************************/
CREATE TABLE `config_info`
(
       `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
       `data_id` varchar(255) NOT NULL COMMENT 'data_id',
       `group_id` varchar(128) DEFAULT NULL COMMENT 'group_id',
       `content` longtext NOT NULL COMMENT 'content',
       `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
       `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
       `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
       `src_user` text COMMENT 'source user',
       `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
       `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
       `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
       `c_desc` varchar(256) DEFAULT NULL COMMENT 'configuration description',
       `c_use` varchar(64) DEFAULT NULL COMMENT 'configuration usage',
       `effect` varchar(64) DEFAULT NULL COMMENT '配置生效的描述',
       `type` varchar(64) DEFAULT NULL COMMENT '配置的类型',
       `c_schema` text COMMENT '配置的模式',
       `encrypted_data_key` text NOT NULL COMMENT '密钥',
       PRIMARY KEY (`id`),
       UNIQUE KEY `uk_configinfo_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info';

/******************************************/
/*   表名称 = config_info_aggr             */
/******************************************/
CREATE TABLE `config_info_aggr`
(
        `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
        `data_id` varchar(255) NOT NULL COMMENT 'data_id',
        `group_id` varchar(128) NOT NULL COMMENT 'group_id',
        `datum_id` varchar(255) NOT NULL COMMENT 'datum_id',
        `content` longtext NOT NULL COMMENT '内容',
        `gmt_modified` datetime NOT NULL COMMENT '修改时间',
        `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
        `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
        PRIMARY KEY (`id`),
        UNIQUE KEY `uk_configinfoaggr_datagrouptenantdatum` (`data_id`,`group_id`,`tenant_id`,`datum_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='增加租户字段';


/******************************************/
/*   表名称 = config_info_beta             */
/******************************************/
CREATE TABLE `config_info_beta`
(
        `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
        `data_id` varchar(255) NOT NULL COMMENT 'data_id',
        `group_id` varchar(128) NOT NULL COMMENT 'group_id',
        `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
        `content` longtext NOT NULL COMMENT 'content',
        `beta_ips` varchar(1024) DEFAULT NULL COMMENT 'betaIps',
        `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
        `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
        `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
        `src_user` text COMMENT 'source user',
        `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
        `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
        `encrypted_data_key` text NOT NULL COMMENT '密钥',
        PRIMARY KEY (`id`),
        UNIQUE KEY `uk_configinfobeta_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_beta';

/******************************************/
/*   表名称 = config_info_tag              */
/******************************************/
CREATE TABLE `config_info_tag`
(
           `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
           `data_id` varchar(255) NOT NULL COMMENT 'data_id',
           `group_id` varchar(128) NOT NULL COMMENT 'group_id',
           `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id',
           `tag_id` varchar(128) NOT NULL COMMENT 'tag_id',
           `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
           `content` longtext NOT NULL COMMENT 'content',
           `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
           `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
           `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
           `src_user` text COMMENT 'source user',
           `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
           PRIMARY KEY (`id`),
           UNIQUE KEY `uk_configinfotag_datagrouptenanttag` (`data_id`,`group_id`,`tenant_id`,`tag_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_tag';

/******************************************/
/*   表名称 = config_tags_relation         */
/******************************************/
CREATE TABLE `config_tags_relation`
(
            `id` bigint(20) NOT NULL COMMENT 'id',
            `tag_name` varchar(128) NOT NULL COMMENT 'tag_name',
            `tag_type` varchar(64) DEFAULT NULL COMMENT 'tag_type',
            `data_id` varchar(255) NOT NULL COMMENT 'data_id',
            `group_id` varchar(128) NOT NULL COMMENT 'group_id',
            `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id',
            `nid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'nid, 自增长标识',
            PRIMARY KEY (`nid`),
            UNIQUE KEY `uk_configtagrelation_configidtag` (`id`,`tag_name`,`tag_type`),
            KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_tag_relation';

/******************************************/
/*   表名称 = group_capacity               */
/******************************************/
CREATE TABLE `group_capacity`
(
          `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
          `group_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Group ID,空字符表示整个集群',
          `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值',
          `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量',
          `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值',
          `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数,,0表示使用默认值',
          `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',
          `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量',
          `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
          `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
          PRIMARY KEY (`id`),
          UNIQUE KEY `uk_group_id` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='集群、各Group容量信息表';

/******************************************/
/*   表名称 = his_config_info              */
/******************************************/
CREATE TABLE `his_config_info`
(
           `id` bigint(20) unsigned NOT NULL COMMENT 'id',
           `nid` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'nid, 自增标识',
           `data_id` varchar(255) NOT NULL COMMENT 'data_id',
           `group_id` varchar(128) NOT NULL COMMENT 'group_id',
           `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
           `content` longtext NOT NULL COMMENT 'content',
           `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
           `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
           `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
           `src_user` text COMMENT 'source user',
           `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
           `op_type` char(10) DEFAULT NULL COMMENT 'operation type',
           `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
           `encrypted_data_key` text NOT NULL COMMENT '密钥',
           PRIMARY KEY (`nid`),
           KEY `idx_gmt_create` (`gmt_create`),
           KEY `idx_gmt_modified` (`gmt_modified`),
           KEY `idx_did` (`data_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='多租户改造';


/******************************************/
/*   表名称 = tenant_capacity              */
/******************************************/
CREATE TABLE `tenant_capacity`
(
           `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
           `tenant_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Tenant ID',
           `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值',
           `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量',
           `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值',
           `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数',
           `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',
           `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量',
           `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
           `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
           PRIMARY KEY (`id`),
           UNIQUE KEY `uk_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='租户容量信息表';


CREATE TABLE `tenant_info`
(
           `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
           `kp` varchar(128) NOT NULL COMMENT 'kp',
           `tenant_id` varchar(128) default '' COMMENT 'tenant_id',
           `tenant_name` varchar(128) default '' COMMENT 'tenant_name',
           `tenant_desc` varchar(256) DEFAULT NULL COMMENT 'tenant_desc',
           `create_source` varchar(32) DEFAULT NULL COMMENT 'create_source',
           `gmt_create` bigint(20) NOT NULL COMMENT '创建时间',
           `gmt_modified` bigint(20) NOT NULL COMMENT '修改时间',
           PRIMARY KEY (`id`),
           UNIQUE KEY `uk_tenant_info_kptenantid` (`kp`,`tenant_id`),
           KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='tenant_info';

CREATE TABLE `users`
(
         `username` varchar(50) NOT NULL PRIMARY KEY COMMENT 'username',
         `password` varchar(500) NOT NULL COMMENT 'password',
         `enabled` boolean NOT NULL COMMENT 'enabled'
);

CREATE TABLE `roles`
(
         `username` varchar(50) NOT NULL COMMENT 'username',
         `role` varchar(50) NOT NULL COMMENT 'role',
         UNIQUE INDEX `idx_user_role` (`username` ASC, `role` ASC) USING BTREE
);

CREATE TABLE `permissions`
(
       `role` varchar(50) NOT NULL COMMENT 'role',
       `resource` varchar(128) NOT NULL COMMENT 'resource',
       `action` varchar(8) NOT NULL COMMENT 'action',
       UNIQUE INDEX `uk_role_permission` (`role`,`resource`,`action`) USING BTREE
);

INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE);

INSERT INTO roles (username, role) VALUES ('nacos', 'ROLE_ADMIN');





-- -------初始化seata建表sql----------
USE seata;
-- 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` (`xid`)
    ) 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);

1.3 编写docker-compose文件

version: "3.2"
services:
  mysql:
    container_name: mysql
    image: mysql:8.0.26
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_unicode_ci
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      TZ: Asia/Shanghai
    volumes:
    # /c/wh-test/seata-demo/resources/mysql/seata-init.sql需要替换成自己本地脚本路径
      - "/c/wh-test/seata-demo/resources/mysql/seata-init.sql:/docker-entrypoint-initdb.d/seata-init.sql"
    ports:
      - "3306:3306"
    restart: always
    networks:
      - net-seata
    healthcheck:
      test: [ "CMD", "mysqladmin","ping","-h","db","-P" ,"3306" ]
      interval: 30s
      timeout: 5s
      retries: 3

1.4 执行docker-compose文件,构建mysql容器

2. docker搭建nacos

2.1 编写docker-compose文件

version: "3.2"
services:
  nacos:
    container_name: nacos
    image: nacos/nacos-server:v2.2.0
    ports:
      - "8848:8848"
      - "9848:9848"
      - "9849:9849"
    environment:
      # standalone表示单机部署
      - MODE=standalone
      - SPRING_DATASOURCE_PLATFORM=mysql
      # mysql是mysql容器名
      - MYSQL_SERVICE_HOST=mysql
      # mysql 对应数据库名称
      - MYSQL_SERVICE_DB_NAME=nacos
      - MYSQL_SERVICE_PORT=3306
      - MYSQL_SERVICE_USER=root
      - MYSQL_SERVICE_PASSWORD=123456
      - MYSQL_SERVICE_DB_PARAM=useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&allowPublicKeyRetrieval=true
    restart: always
    networks:
      - net-seata

2.2 执行docker-compose文件,构建nacos容器

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

客户端登录nacos

2.3 nacos创建seata配置信息

2.3.1 创建命名空间

2.3.2 创建配置信息

2.3.3 seata.properties配置信息

ransport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.thread-factory.boss-thread-prefix=NettyBoss
transport.thread-factory.worker-thread-prefix=NettyServerNIOWorker
transport.thread-factory.server-executor-thread-prefix=NettyServerBizHandler
transport.thread-factory.share-boss-worker=false
transport.thread-factory.client-selector-thread-prefix=NettyClientSelector
transport.thread-factory.client-selector-thread-size=1
transport.thread-factory.client-worker-thread-prefix=NettyClientWorkerThread
transport.thread-factory.boss-thread-size=1
transport.thread-factory.worker-thread-size=8
transport.shutdown.wait=3

service.enableDegrade=false
service.disable=false
service.max.commit.retry.timeout=-1
service.max.rollback.retry.timeout=-1
client.async.commit.buffer.limit=10000
client.lock.retry.internal=10
client.lock.retry.times=30
store.mode=db
store.db.driver-class-name=com.mysql.cj.jdbc.Driver
store.db.datasource=druid
store.db.db_type=mysql
store.db.url=jdbc:mysql://mysql/seata?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
store.db.user=root
store.db.password=123456
store.db.min-conn=1
store.db.max-conn=3
store.db.global.table=global_table
store.db.branch.table=branch_table
store.db.query-limit=100
store.db.lock-table=lock_table
recovery.committing-retry-period=1000
recovery.asyn-committing-retry-period=1000
recovery.rollbacking-retry-period=1000
recovery.timeout-retry-period=1000
transaction.undo.data.validation=true
transaction.undo.log.serialization=jackson
transaction.undo.log.save.days=7
transaction.undo.log.delete.period=86400000
transaction.undo.log.table=undo_log
transport.serialization=seata
transport.compressor=none
metrics.enabled=false
metrics.registry-type=compact
metrics.exporter-list=prometheus
metrics.exporter-prometheus-port=9898
client.report.retry.count=5
service.disableGlobalTransaction=false

3. docker搭建seata

3.1 编写docker-compose文件

version: "3.2"
services:
  seataServer:
    container_name: seataServer
    image: seataio/seata-server:1.5.0
    ports:
      - "7091:7091"
      - "8091:8091"
    environment:
      - TZ=Asia/Shanghai
      - LANG=en_US.UTF-8
      - STORE_MODE=db
      - SEATA_IP=127.0.0.1
      - SEATA_PORT=8091
    volumes:
      - "/c/wh-test/seata-demo/resources/seata/application.yml:/seata-server/resources/application.yml"
    restart: always
    networks:
      - net-seata

networks:
  net-seata:
    external: true

3.2 执行docker-compose文件,创建seata容器

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

3.3 替换容器内mysql驱动包

seata默认支持mysql5版本,我们用的是mysql8,所以需要替mysql换驱动包

docker cp C:\wh-data\wh-mavenRepository\mysql\mysql-connector-java\8.0.27\mysql-connector-java-8.0.27.jar dd22743bc732:/seata-server/libs

进入容器内部并删除mysql5版本的驱动包

3.4 重启容器

docker restart 容器ID

以上就是docker实现本地seata分布式环境搭建详解的详细内容,更多关于docker本地seata分布式环境搭建的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文