DockerCompose一键部署nacos集群seata以及sentinel

1 Docker Compose 部署 nacos 集群,seata,sentinel

参考资料:Spring Cloud Alibaba

1.1 编写 compose 文件

包含三个 nacos 集群,一个 seata 服务,一个 sentinel 服务,自定义内部网络,seata 服务于 nacos 集群启动结束并通过健康检查后再启用,若遇到启动失败,则需要重新单独启动 seata 即可

注意端口设置!!!!!!!

version: "3.8"
services:
  nacos1:
    hostname: nacos1
    container_name: nacos1
    image: nacos/nacos-server:v2.5.0
    environment:
      - MODE=cluster
      - PREFER_HOST_MODE=hostname
      - NACOS_SERVERS=172.20.0.2:8848 172.20.0.3:8848 172.20.0.4:8848
      - NACOS_SERVER_IP=172.20.0.2
      - SPRING_DATASOURCE_PLATFORM=mysql
      - MYSQL_SERVICE_HOST=192.168.104.41
      - MYSQL_SERVICE_DB_NAME=nacos
      - MYSQL_SERVICE_PORT=3306
      - MYSQL_SERVICE_USER=root
      - MYSQL_SERVICE_PASSWORD=xxaq1122
      - MYSQL_SERVICE_DB_PARAM=utf8&connectTimeout=10000&socketTimeout=30000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
      - JAVA_OPTS=-Xms256m -Xmx256m -Xmn128m
    volumes:
      - /env/nacos/cluster.conf:/home/nacos/conf/cluster.conf
      - /env/nacos/application.properties:/home/nacos/conf/application.properties
      - /env/nacos/nacos-1/logs:/home/nacos/logs
    ports:
      - "8248:8848"
      - "9248:9848"
      - "7248:7848"
      - "9249:9849"
    restart: always
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8848/nacos"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    networks:
      nacos_cluster_nginx:
        ipv4_address: 172.20.0.2

  nacos2:
    hostname: nacos2
    image: nacos/nacos-server:v2.5.0
    container_name: nacos2
    environment:
      - MODE=cluster
      - PREFER_HOST_MODE=hostname
      - NACOS_SERVERS=172.20.0.2:8848 172.20.0.3:8848 172.20.0.4:8848
      - NACOS_SERVER_IP=172.20.0.3
      - SPRING_DATASOURCE_PLATFORM=mysql
      - MYSQL_SERVICE_HOST=192.168.104.41
      - MYSQL_SERVICE_DB_NAME=nacos
      - MYSQL_SERVICE_PORT=3306
      - MYSQL_SERVICE_USER=root
      - MYSQL_SERVICE_PASSWORD=xxaq1122
      - MYSQL_SERVICE_DB_PARAM=utf8&connectTimeout=10000&socketTimeout=30000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
      - JAVA_OPTS=-Xms256m -Xmx256m -Xmn128m
    volumes:
      - /env/nacos/application.properties:/home/nacos/conf/application.properties
      - /env/nacos/nacos-2/logs:/home/nacos/logs
      - /env/nacos/cluster.conf:/home/nacos/conf/cluster.conf
    ports:
      - "8348:8848"
      - "9348:9848"
      - "7348:7848"
      - "9349:9849"
    restart: always
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8848/nacos"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    networks:
      nacos_cluster_nginx:
        ipv4_address: 172.20.0.3
    
  nacos3:
    hostname: nacos3
    image: nacos/nacos-server:v2.5.0
    container_name: nacos3
    environment:
      - MODE=cluster
      - PREFER_HOST_MODE=hostname
      - NACOS_SERVERS=172.20.0.2:8848 172.20.0.3:8848 172.20.0.4:8848
      - NACOS_SERVER_IP=172.20.0.4
      - SPRING_DATASOURCE_PLATFORM=mysql
      - MYSQL_SERVICE_HOST=192.168.104.41
      - MYSQL_SERVICE_DB_NAME=nacos
      - MYSQL_SERVICE_PORT=3306
      - MYSQL_SERVICE_USER=root
      - MYSQL_SERVICE_PASSWORD=xxaq1122
      - MYSQL_SERVICE_DB_PARAM=utf8&connectTimeout=10000&socketTimeout=30000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
      - JAVA_OPTS=-Xms256m -Xmx256m -Xmn128m
    volumes:
      - /env/nacos/application.properties:/home/nacos/conf/application.properties
      - /env/nacos/nacos-3/logs:/home/nacos/logs
      - /env/nacos/cluster.conf:/home/nacos/conf/cluster.conf
    ports:
      - "8448:8848"
      - "9448:9848"
      - "7448:7848"
      - "9449:9849"
    restart: always
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8848/nacos"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    networks:
      nacos_cluster_nginx:
        ipv4_address: 172.20.0.4
        
        
  seata:
    hostname: seata
    image: seataio/seata-server:2.0.0
    container_name: seata
    ports:
      - "7591:7091"
      - "8591:8091"
    volumes:
      - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime        #设置系统时区
      - /usr/share/zoneinfo/Asia/Shanghai:/etc/timezone  #设置时区
      - /env/seata:/seata-server/resources
    networks:
      nacos_cluster_nginx:
        ipv4_address: 172.20.0.5
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:7091/"]
      interval: 5s
      timeout: 10s
      retries: 3
    depends_on:
      nacos1:
        condition: service_healthy
      nacos2:
        condition: service_healthy
      nacos3:
        condition: service_healthy
        
        
  sentinel-dashboard:
    hostname: sentinel
    image: bladex/sentinel-dashboard
    container_name: sentinel-dashboard
    restart: always
    environment:
      JAVA_OPTS: "-Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=xxaq1122 server.port=8858 -Dcsp.sentinel.dashboard.server=localhost:8858 -Dproject.name=sentinel-dashboard"
    ports: 
      - "8658:8858"
    volumes:
      - /env/sentinel/logs:/root/logs
    networks:
      nacos_cluster_nginx:
        ipv4_address: 172.20.0.6
    
networks:
  nacos_cluster_nginx:
    ipam:
       config:
         - subnet: 172.20.0.0/16

1.2 配置 nacos

1.2.1 创建 nacos 数据库

create table config_info
(
    id                 bigint auto_increment comment 'id'
        primary key,
    data_id            varchar(255)                            not null comment 'data_id',
    group_id           varchar(128)                            null comment 'group_id',
    content            longtext                                not null comment 'content',
    md5                varchar(32)                             null comment 'md5',
    gmt_create         datetime      default CURRENT_TIMESTAMP not null comment '创建时间',
    gmt_modified       datetime      default CURRENT_TIMESTAMP not null comment '修改时间',
    src_user           text                                    null comment 'source user',
    src_ip             varchar(50)                             null comment 'source ip',
    app_name           varchar(128)                            null comment 'app_name',
    tenant_id          varchar(128)  default ''                null comment '租户字段',
    c_desc             varchar(256)                            null comment 'configuration description',
    c_use              varchar(64)                             null comment 'configuration usage',
    effect             varchar(64)                             null comment '配置生效的描述',
    type               varchar(64)                             null comment '配置的类型',
    c_schema           text                                    null comment '配置的模式',
    encrypted_data_key varchar(1024) default ''                not null comment '密钥',
    constraint uk_configinfo_datagrouptenant
        unique (data_id, group_id, tenant_id)
)
    comment 'config_info' collate = utf8mb3_bin;

create table config_info_beta
(
    id                 bigint auto_increment comment 'id'
        primary key,
    data_id            varchar(255)                            not null comment 'data_id',
    group_id           varchar(128)                            not null comment 'group_id',
    app_name           varchar(128)                            null comment 'app_name',
    content            longtext                                not null comment 'content',
    beta_ips           varchar(1024)                           null comment 'betaIps',
    md5                varchar(32)                             null comment 'md5',
    gmt_create         datetime      default CURRENT_TIMESTAMP not null comment '创建时间',
    gmt_modified       datetime      default CURRENT_TIMESTAMP not null comment '修改时间',
    src_user           text                                    null comment 'source user',
    src_ip             varchar(50)                             null comment 'source ip',
    tenant_id          varchar(128)  default ''                null comment '租户字段',
    encrypted_data_key varchar(1024) default ''                not null comment '密钥',
    constraint uk_configinfobeta_datagrouptenant
        unique (data_id, group_id, tenant_id)
)
    comment 'config_info_beta' collate = utf8mb3_bin;

create table config_info_gray
(
    id                 bigint unsigned auto_increment comment 'id'
        primary key,
    data_id            varchar(255)                              not null comment 'data_id',
    group_id           varchar(128)                              not null comment 'group_id',
    content            longtext                                  not null comment 'content',
    md5                varchar(32)                               null comment 'md5',
    src_user           text                                      null comment 'src_user',
    src_ip             varchar(100)                              null comment 'src_ip',
    gmt_create         datetime(3)  default CURRENT_TIMESTAMP(3) not null comment 'gmt_create',
    gmt_modified       datetime(3)  default CURRENT_TIMESTAMP(3) not null comment 'gmt_modified',
    app_name           varchar(128)                              null comment 'app_name',
    tenant_id          varchar(128) default ''                   null comment 'tenant_id',
    gray_name          varchar(128)                              not null comment 'gray_name',
    gray_rule          text                                      not null comment 'gray_rule',
    encrypted_data_key varchar(256) default ''                   not null comment 'encrypted_data_key',
    constraint uk_configinfogray_datagrouptenantgray
        unique (data_id, group_id, tenant_id, gray_name)
)
    comment 'config_info_gray' charset = utf8mb3;

create index idx_dataid_gmt_modified
    on config_info_gray (data_id, gmt_modified);

create index idx_gmt_modified
    on config_info_gray (gmt_modified);

create table config_info_tag
(
    id           bigint auto_increment comment 'id'
        primary key,
    data_id      varchar(255)                           not null comment 'data_id',
    group_id     varchar(128)                           not null comment 'group_id',
    tenant_id    varchar(128) default ''                null comment 'tenant_id',
    tag_id       varchar(128)                           not null comment 'tag_id',
    app_name     varchar(128)                           null comment 'app_name',
    content      longtext                               not null comment 'content',
    md5          varchar(32)                            null comment 'md5',
    gmt_create   datetime     default CURRENT_TIMESTAMP not null comment '创建时间',
    gmt_modified datetime     default CURRENT_TIMESTAMP not null comment '修改时间',
    src_user     text                                   null comment 'source user',
    src_ip       varchar(50)                            null comment 'source ip',
    constraint uk_configinfotag_datagrouptenanttag
        unique (data_id, group_id, tenant_id, tag_id)
)
    comment 'config_info_tag' collate = utf8mb3_bin;

create table config_tags_relation
(
    id        bigint                  not null comment 'id',
    tag_name  varchar(128)            not null comment 'tag_name',
    tag_type  varchar(64)             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 '' null comment 'tenant_id',
    nid       bigint auto_increment comment 'nid, 自增长标识'
        primary key,
    constraint uk_configtagrelation_configidtag
        unique (id, tag_name, tag_type)
)
    comment 'config_tag_relation' collate = utf8mb3_bin;

create index idx_tenant_id
    on config_tags_relation (tenant_id);

create table group_capacity
(
    id                bigint unsigned auto_increment comment '主键ID'
        primary key,
    group_id          varchar(128) default ''                not null comment 'Group ID,空字符表示整个集群',
    quota             int unsigned default '0'               not null comment '配额,0表示使用默认值',
    `usage`           int unsigned default '0'               not null comment '使用量',
    max_size          int unsigned default '0'               not null comment '单个配置大小上限,单位为字节,0表示使用默认值',
    max_aggr_count    int unsigned default '0'               not null comment '聚合子配置最大个数,,0表示使用默认值',
    max_aggr_size     int unsigned default '0'               not null comment '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',
    max_history_count int unsigned default '0'               not null comment '最大变更历史数量',
    gmt_create        datetime     default CURRENT_TIMESTAMP not null comment '创建时间',
    gmt_modified      datetime     default CURRENT_TIMESTAMP not null comment '修改时间',
    constraint uk_group_id
        unique (group_id)
)
    comment '集群、各Group容量信息表' collate = utf8mb3_bin;

create table his_config_info
(
    id                 bigint unsigned                         not null comment 'id',
    nid                bigint unsigned auto_increment comment 'nid, 自增标识'
        primary key,
    data_id            varchar(255)                            not null comment 'data_id',
    group_id           varchar(128)                            not null comment 'group_id',
    app_name           varchar(128)                            null comment 'app_name',
    content            longtext                                not null comment 'content',
    md5                varchar(32)                             null comment 'md5',
    gmt_create         datetime      default CURRENT_TIMESTAMP not null comment '创建时间',
    gmt_modified       datetime      default CURRENT_TIMESTAMP not null comment '修改时间',
    src_user           text                                    null comment 'source user',
    src_ip             varchar(50)                             null comment 'source ip',
    op_type            char(10)                                null comment 'operation type',
    tenant_id          varchar(128)  default ''                null comment '租户字段',
    encrypted_data_key varchar(1024) default ''                not null comment '密钥',
    publish_type       varchar(50)   default 'formal'          null comment 'publish type gray or formal',
    gray_name          varchar(50)                             null comment 'gray name',
    ext_info           longtext                                null comment 'ext info'
)
    comment '多租户改造' collate = utf8mb3_bin;

create index idx_did
    on his_config_info (data_id);

create index idx_gmt_create
    on his_config_info (gmt_create);

create index idx_gmt_modified
    on his_config_info (gmt_modified);

create table permissions
(
    role     varchar(50)  not null comment 'role',
    resource varchar(128) not null comment 'resource',
    action   varchar(8)   not null comment 'action',
    constraint uk_role_permission
        unique (role, resource, action)
);

create table roles
(
    username varchar(50) not null comment 'username',
    role     varchar(50) not null comment 'role',
    constraint idx_user_role
        unique (username, role)
);

create table tenant_capacity
(
    id                bigint unsigned auto_increment comment '主键ID'
        primary key,
    tenant_id         varchar(128) default ''                not null comment 'Tenant ID',
    quota             int unsigned default '0'               not null comment '配额,0表示使用默认值',
    `usage`           int unsigned default '0'               not null comment '使用量',
    max_size          int unsigned default '0'               not null comment '单个配置大小上限,单位为字节,0表示使用默认值',
    max_aggr_count    int unsigned default '0'               not null comment '聚合子配置最大个数',
    max_aggr_size     int unsigned default '0'               not null comment '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',
    max_history_count int unsigned default '0'               not null comment '最大变更历史数量',
    gmt_create        datetime     default CURRENT_TIMESTAMP not null comment '创建时间',
    gmt_modified      datetime     default CURRENT_TIMESTAMP not null comment '修改时间',
    constraint uk_tenant_id
        unique (tenant_id)
)
    comment '租户容量信息表' collate = utf8mb3_bin;

create table tenant_info
(
    id            bigint auto_increment comment 'id'
        primary key,
    kp            varchar(128)            not null comment 'kp',
    tenant_id     varchar(128) default '' null comment 'tenant_id',
    tenant_name   varchar(128) default '' null comment 'tenant_name',
    tenant_desc   varchar(256)            null comment 'tenant_desc',
    create_source varchar(32)             null comment 'create_source',
    gmt_create    bigint                  not null comment '创建时间',
    gmt_modified  bigint                  not null comment '修改时间',
    constraint uk_tenant_info_kptenantid
        unique (kp, tenant_id)
)
    comment 'tenant_info' collate = utf8mb3_bin;

create index idx_tenant_id
    on tenant_info (tenant_id);

create table users
(
    username varchar(50)  not null comment 'username'
        primary key,
    password varchar(500) not null comment 'password',
    enabled  tinyint(1)   not null comment 'enabled'
);

1.3 编写 nacos 配置文件

首先配置 nacos 配置文件,包含 application.properties 和 cluster.conf

# spring
server.servlet.contextPath=${SERVER_SERVLET_CONTEXTPATH:/nacos}
server.contextPath=/nacos
# 主服务端口
server.port=8848
# gRpc端口,和以上都需要打开,保证端口号+1000
nacos.grpc.port=9848
server.tomcat.accesslog.max-days=30
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i %{Request-Source}i
server.tomcat.accesslog.enabled=${TOMCAT_ACCESSLOG_ENABLED:false}
server.error.include-message=ALWAYS
# default current work dir
server.tomcat.basedir=file:.
#*************** Config Module Related Configurations ***************#
### Deprecated configuration property, it is recommended to use `spring.sql.init.platform` replaced.
#spring.datasource.platform=${SPRING_DATASOURCE_PLATFORM:}
spring.sql.init.platform=${SPRING_DATASOURCE_PLATFORM:}
nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false
db.num=${MYSQL_DATABASE_NUM:1}
db.url.0=jdbc:mysql://${MYSQL_SERVICE_HOST}:${MYSQL_SERVICE_PORT:3306}/${MYSQL_SERVICE_DB_NAME}?${MYSQL_SERVICE_DB_PARAM:characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false}
db.user.0=${MYSQL_SERVICE_USER}
db.password.0=${MYSQL_SERVICE_PASSWORD}
## DB connection pool settings
db.pool.config.connectionTimeout=${DB_POOL_CONNECTION_TIMEOUT:30000}
db.pool.config.validationTimeout=10000
db.pool.config.maximumPoolSize=20
db.pool.config.minimumIdle=2
### The auth system to use, currently only 'nacos' and 'ldap' is supported:
### 打开认证
nacos.core.auth.system.type=nacos
nacos.core.auth.enabled=true
### worked when nacos.core.auth.system.type=nacos
### The token expiration in seconds:
nacos.core.auth.plugin.nacos.token.expire.seconds=${NACOS_AUTH_TOKEN_EXPIRE_SECONDS:18000}
### The default token:
nacos.core.auth.plugin.nacos.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
nacos.core.auth.caching.enabled=${NACOS_AUTH_CACHE_ENABLE:false}
nacos.core.auth.enable.userAgentAuthWhite=${NACOS_AUTH_USER_AGENT_AUTH_WHITE_ENABLE:false}
### 设置key和密码
nacos.core.auth.server.identity.key=nacos
nacos.core.auth.server.identity.value=nacos
## spring security config
### turn off security
nacos.security.ignore.urls=${NACOS_SECURITY_IGNORE_URLS:/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**}
# metrics for elastic search
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false
nacos.naming.distro.taskDispatchThreadCount=10
nacos.naming.distro.taskDispatchPeriod=200
nacos.naming.distro.batchSyncKeyCount=1000
nacos.naming.distro.initDataRatio=0.9
nacos.naming.distro.syncRetryDelay=5000
nacos.naming.data.warmup=true
nacos.console.ui.enabled=true
nacos.core.param.check.enabled=true
# 这个会自动生成,但是自定义最好,若遇到节点多出一个,检查docker compose文件内的网络和IP配置,修改cluster.conf文件后重启
172.20.0.2:8848
172.20.0.3:8848
172.20.0.4:8848

1.3 配置 seata

1.3.1 获取配置文件

seata 需要配置 application.yml 和在 nacos 中注册配置文件

首先运行一个 seata 容器,复制出配置文件

$ docker pull seataio/seata-server:2.0.0
$ docker run --name seata-server -p 8091:8091 -p 7091:7091 -e SEATA_IP=192.168.1.1  -e SEATA_PORT=8091 seataio/seata-server
# 查询seata容器
$ docker ps -a
# 容器命令行
$ docker exec -it seata-server sh
# 复制配置文件到本地(seata-server容器的ID)
$ docker cp 2d4764453cf7:/seata-server/resource /data/seata
$ docker stop 2d4764453cf7
$ docker rm 2d4764453cf7

1.3.2 创建 seata 数据库

create table branch_table
(
  branch_id         bigint        not null
  primary key,
  xid               varchar(128)  not null,
  transaction_id    bigint        null,
  resource_group_id varchar(32)   null,
  resource_id       varchar(256)  null,
  branch_type       varchar(8)    null,
  status            tinyint       null,
  client_id         varchar(64)   null,
  application_data  varchar(2000) null,
  gmt_create        datetime(6)   null,
  gmt_modified      datetime(6)   null
);

create index idx_xid
    on branch_table (xid);

create table distributed_lock
(
  lock_key   char(20)    not null
  primary key,
  lock_value varchar(20) not null,
  expire     bigint      null
);

create table global_table
(
  xid                       varchar(128)  not null
  primary key,
  transaction_id            bigint        null,
  status                    tinyint       not null,
  application_id            varchar(32)   null,
  transaction_service_group varchar(32)   null,
  transaction_name          varchar(128)  null,
  timeout                   int           null,
  begin_time                bigint        null,
  application_data          varchar(2000) null,
  gmt_create                datetime      null,
  gmt_modified              datetime      null
);

create index idx_status_gmt_modified
    on global_table (status, gmt_modified);

create index idx_transaction_id
    on global_table (transaction_id);

create table lock_table
(
  row_key        varchar(128)      not null
  primary key,
  xid            varchar(128)      null,
  transaction_id bigint            null,
  branch_id      bigint            not null,
  resource_id    varchar(256)      null,
  table_name     varchar(32)       null,
  pk             varchar(36)       null,
  status         tinyint default 0 not null comment '0:locked ,1:rollbacking',
  gmt_create     datetime          null,
  gmt_modified   datetime          null
);

create index idx_branch_id
    on lock_table (branch_id);

create index idx_status
    on lock_table (status);

create index idx_xid
    on lock_table (xid);

1.3.3 修改 applicatio.yml

修改 seata 注册到 nacos,配置中心为 nacos,注意地址和上述 nacos 地址一致,本地 IP

#  Copyright 1999-2019 Seata.io Group.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

server:
  port: 7091

spring:
  application:
    name: seata-server

logging:
  config: classpath:logback-spring.xml
  file:
    path: ${log.home:${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
console:
  user:
    username: seata
    password: seata

seata:
  config:
    # 修改配置中心为nacos,注意配置namespace和cluster
    type: nacos
    nacos:
      server-addr: 172.20.0.2:8848,172.20.0.3:8848,172.20.0.4:8848
      namespace: dev
      group: SEATA_GROUP
      username: nacos
      password: xxaq1122
      context-path:
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key:
      #secret-key:
      data-id: seataServer.properties
  registry:
    # 修改注册中心为nacos,注意配置namespace和cluster
    type: nacos
    nacos:
      application: seata-server
      server-addr: 172.20.0.2:8848,172.20.0.3:8848,172.20.0.4:8848
      group: SEATA_GROUP
      namespace: dev
      cluster: default
      username: nacos
      password: xxaq1122
      context-path:
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key:
      #secret-key:

  server:
    service-port: 8091 #If not configured, the default is '${server.port} + 1000'
    max-commit-retry-timeout: -1
    max-rollback-retry-timeout: -1
    rollback-failed-unlock-enable: false
    enable-check-auth: true
    enable-parallel-request-handle: true
    retry-dead-threshold: 130000
    xaer-nota-retry-timeout: 60000
    enableParallelRequestHandle: true
    recovery:
      committing-retry-period: 1000
      async-committing-retry-period: 1000
      rollbacking-retry-period: 1000
      timeout-retry-period: 1000
    undo:
      log-save-days: 7
      log-delete-period: 86400000
    session:
      branch-async-queue-size: 5000 #branch async remove queue size
      enable-branch-async-remove: false #enable to asynchronous remove branchSession
  metrics:
    enabled: false
    registry-type: compact
    exporter-list: prometheus
    exporter-prometheus-port: 9898
  transport:
    rpc-tc-request-timeout: 15000
    enable-tc-server-batch-send-response: false
    shutdown:
      wait: 3
    thread-factory:
      boss-thread-prefix: NettyBoss
      worker-thread-prefix: NettyServerNIOWorker
      boss-thread-size: 1


  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
  

1.3.4 在 nacos 中创建配置文件

这一步需要提前在 nacos 中注册配置文件,可将上述 dockercompose 文件删除 seata 服务项启动后,添加配置文件后,重新启动

创建 seataServer.properties,输入以下内容保存,注意 namespace 和 cluster 位置

store.mode=db
#-----db-----
store.db.datasource=druid
store.db.dbType=mysql
# 需要根据mysql的版本调整driverClassName
# mysql8及以上版本对应的driver:com.mysql.cj.jdbc.Driver
# mysql8以下版本的driver:com.mysql.jdbc.Driver
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://192.168.1.6:3306/seata?useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
store.db.user= root
store.db.password=xxaq1122
# 数据库初始连接数
store.db.minConn=1
# 数据库最大连接数
store.db.maxConn=20
# 获取连接时最大等待时间 默认5000,单位毫秒
store.db.maxWait=5000
# 全局事务表名 默认global_table
store.db.globalTable=global_table
# 分支事务表名 默认branch_table
store.db.branchTable=branch_table
# 全局锁表名 默认lock_table
store.db.lockTable=lock_table
# 查询全局事务一次的最大条数 默认100
store.db.queryLimit=100


# undo保留天数 默认7天,log_status=1(附录3)和未正常清理的undo
server.undo.logSaveDays=7
# undo清理线程间隔时间 默认86400000,单位毫秒
server.undo.logDeletePeriod=86400000
# 二阶段提交重试超时时长 单位ms,s,m,h,d,对应毫秒,秒,分,小时,天,默认毫秒。默认值-1表示无限重试
# 公式: timeout>=now-globalTransactionBeginTime,true表示超时则不再重试
# 注: 达到超时时间后将不会做任何重试,有数据不一致风险,除非业务自行可校准数据,否者慎用
server.maxCommitRetryTimeout=-1
# 二阶段回滚重试超时时长
server.maxRollbackRetryTimeout=-1
# 二阶段提交未完成状态全局事务重试提交线程间隔时间 默认1000,单位毫秒
server.recovery.committingRetryPeriod=1000
# 二阶段异步提交状态重试提交线程间隔时间 默认1000,单位毫秒
server.recovery.asynCommittingRetryPeriod=1000
# 二阶段回滚状态重试回滚线程间隔时间  默认1000,单位毫秒
server.recovery.rollbackingRetryPeriod=1000
# 超时状态检测重试线程间隔时间 默认1000,单位毫秒,检测出超时将全局事务置入回滚会话管理器
server.recovery.timeoutRetryPeriod=1000

1.4 配置 sentinel

无须配置,直接按照 docker compose 配置启动就行

### 使用 Docker Compose 部署 Seata 集群 为了实现高可用性和负载均衡,可以使用 Docker Compose部署多个 Seata Server 实例并配置它们形成一个集群环境。以下是关于如何通过 `docker-compose` 文件来完成这一目标的具体方法。 #### 1. 准备工作 在开始之前,确保已经具备以下条件: - 已经安装好 DockerDocker Compose。 - 下载了必要的依赖项以及配置文件(如 Nacos 或其他注册中心的相关配置)。这些通常可以通过脚本初始化或者手动创建[^3]。 #### 2. 编写 docker-compose.yaml 文件 下面是一个用于启动三个节点组成的 Seata 集群的示例 YAML 文件: ```yaml version: '3.1' services: seata-server-node1: image: seataio/seata-server:${latest-release-version} container_name: seata-server-node1 hostname: seata-server-node1 ports: - "8091:8091" environment: - SEATA_PORT=8091 - STORE_MODE=file volumes: - ./config:/data/conf seata-server-node2: image: seataio/seata-server:${latest-release-version} container_name: seata-server-node2 hostname: seata-server-node2 ports: - "8092:8091" environment: - SEATA_PORT=8091 - STORE_MODE=file volumes: - ./config:/data/conf seata-server-node3: image: seataio/seata-server:${latest-release-version} container_name: seata-server-node3 hostname: seata-server-node3 ports: - "8093:8091" environment: - SEATA_PORT=8091 - STORE_MODE=file volumes: - ./config:/data/conf ``` 此配置定义了三个服务实例 (`seata-server-node1`, `node2`, `node3`) ,每个都映射到不同的主机端口上运行相同的容器镜像版本 ${latest-release-version}[^2]。 #### 3. 修改配置文件 由于所有的服务器共享同一套配置数据,因此需要挂载本地目录至各个容器内部以便统一管理参数设置。上述例子中的 `- ./config:/data/conf` 行指定了将当前路径下的 `./config` 文件夹绑定到容器内的 `/data/conf` 路径下。 注意调整 `registry.conf` 中的服务发现方式以支持多台机器间的通信连接;如果采用的是 Nacos,则需确认其地址可达性并且正确填写相关信息字段。 #### 4. 启动集群 执行命令如下即可一键拉起整个分布式事务协调器组网架构: ```bash docker-compose up -d ``` 这会按照指定好的模板异步后台开启所有组件进程[^1]。 --- ### 注意事项 - **存储模式**: 默认情况下这里选择了基于文件系统的持久化方案(`STORE_MODE=file`)作为演示用途,在生产环境中推荐切换为数据库形式提升性能表现与可靠性。 - **网络规划**: 如果跨宿主机间存在防火墙限制等问题可能会影响正常交互操作,请提前做好相应策略安排处理。 - **健康检查机制**: 可考虑引入额外监控工具定期探测各成员状态从而及时发现问题所在位置加以修复优化措施落实到位。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值