docker实现本地seata分布式环境搭建详解-kb88凯时官网登录

来自:网络
时间:2024-06-08
阅读:

使用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 创建命名空间

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

2.3.2 创建配置信息

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

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

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分布式环境搭建的资料请关注其它相关文章!

返回顶部
顶部
网站地图