刘耀文

刘耀文

java开发者
github

Spring Cloud Environment Setup in Docker-Compose Mode

A quick setup service memo for learning Spring Cloud

YAML File#


version: "3"
services:

  # Nacos environment: standalone
  # 1. Prepare the database table, see below > Nacos database table 
  # 2. Prepare the startup environment, see below > nacos-standlone-mysql.env
  # 3. Official tutorial: https://nacos.io/docs/next/manual/admin/deployment/deployment-standalone/
  nacos:
    image: nacos/nacos-server:latest
    container_name: nacosnacos-standlone-mysql.env
    env_file:
      # Mount the environment here
      - /home/liu/nacos-docker/env/nacos-standlone-mysql.env
    volumes:
      - /home/liu/nacos-logs/standalone-logs/:/home/nacos/logs
      - /home/liu/wait-it.sh:/home/liu/wait-it.sh
    ports:
      - "8848:8848"
      - "9848:9848"
      - "9849:9849"
    restart: always
    # Wait for the MySQL service to start, this file needs to be prepared in advance, available on GitHub. If using an external SQL service, it is not needed, but the configuration in nacos-standalone-mysql.env needs to be changed.
    command: bash -c "chmod +x /home/liu/wait-it.sh && /home/liu/wait-it.sh mysql:3306 --timeout=30 --echo 'mysql is up'"
    depends_on:
      - mysql
  
    networks:
      - cloud-net
  # MySQL for Nacos service
  mysql: # Service name
    image: mysql:latest # Or other MySQL versions
    container_name: mysql8 # Container name
    environment:
      - MYSQL_ROOT_PASSWORD=mysql_liu 
#      - TZ=Asia/Shanghai # Set container timezone, I have synchronized the host machine's timezone and time through the mount method below, so this is ignored.
    volumes:
      - mysql:/var/lib/mysql
      - /home/docker/mysql8/log:/var/log/mysql 
      - /home/docker/mysql8/conf.d:/etc/mysql/conf.d 
      - /etc/localtime:/etc/localtime:ro 
    networks:
      - cloud-net
    ports:
      - 3306:3306 
    # Specify the mapping relationship between the host port and the container port, host:container
    restart: always # Container starts automatically with Docker
  # Distributed transactions 
  # 1. Copy the resource out docker cp container_name:/seata-server/resources your_linux_service_address, e.g., /home/liu/seata/resources
  # 2. Configure the application inside, template: https://github.com/apache/incubator-seata/blob/develop/server/src/main/resources/application.example.yml
  # 3. Add the configured Nacos in Seata configuration, see example: below > seataServer.properties
  # 4. Official tutorial: https://seata.apache.org/zh-cn/docs/ops/deploy-by-docker-compose
  seata-server:
    image: seataio/seata-server:1.5.2
    container_name: seata
    ports:
      - "7091:7091"
      - "8091:8091"
    environment:
      - STORE_MODE=db
      # Register Seata server with SEATA_IP as host
      - SEATA_IP=192.168.208.128
      - SEATA_PORT=8091
    volumes:
      - "/usr/share/zoneinfo/Asia/Shanghai:/etc/localtime"        # Set system timezone
      - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"  # Set timezone
      # Assume we copied the resource files to the relative path `./seata-server/resources` using the docker cp command
      # If there are issues, please read the above [Notes] and [Using Custom Configuration Files]
      - "/home/liu/seata/resources:/seata-server/resources"
    depends_on:
      - nacos
      
    restart: always  
    
    networks:
      - cloud-net

  # 1. After starting, configure the delayed queue plugin docker exec -it rabbitmq rabbitmq-plugins enable rabbitmq_delayed_message_exchange
  # 2. Official tutorial: https://hub.docker.com/_/rabbitmq/
  rabbitmq:
    image: rabbitmq:3.8-management
    container_name: rabbitmq
    hostname: my-rabbit
    ports:
      - "5672:5672"
      - "15672:15672"
    networks:
      - cloud-net
    environment:
      RABBITMQ_DEFAULT_USER: user
      RABBITMQ_DEFAULT_PASS: password
    volumes:
      - mq_plugins:/plugins

  # Official tutorial: https://www.elastic.co/guide/en/elasticsearch/reference/current/run-elasticsearch-locally.html
  kibana:
    image: docker.elastic.co/kibana/kibana:7.12.1
    container_name: kibana
    networks:
      - cloud-net
    ports:
      - "5601:5601"
    environment:
      ELASTICSEARCH_URL: http://es:9200
      ELASTICSEARCH_HOSTS: http://es:9200
      ELASTICSEARCH_USERNAME: user
      ELASTICSEARCH_PASSWORD: password
      xpack.security.enabled: "false"
      xpack.license.self_generated.type: trial
    depends_on:
      - es
      
      
  es:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.12.1
    container_name: es
    networks:
      - cloud-net
    ports:
      - "9200:9200"
    environment:
      ELASTIC_PASSWORD: password
      discovery.type: "single-node"
      xpack.security.http.ssl.enabled: "false"
      xpack.license.self_generated.type: trial
    volumes:
      - esdata:/usr/share/elasticsearch/data  # Persist Elasticsearch data

networks:
  cloud-net:

volumes:
  esdata:  # Define persistent storage volume
  mysql:
  mq_plugins:  # Define persistent storage volume for RabbitMQ plugins

Nacos Configuration#

SQL#


/******************************************/
/*   Table Name = 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 'Creation time',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time',
  `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 'tenant field',
  `c_desc` varchar(256) DEFAULT NULL COMMENT 'configuration description',
  `c_use` varchar(64) DEFAULT NULL COMMENT 'configuration usage',
  `effect` varchar(64) DEFAULT NULL COMMENT 'description of configuration effectiveness',
  `type` varchar(64) DEFAULT NULL COMMENT 'type of configuration',
  `c_schema` text COMMENT 'configuration schema',
  `encrypted_data_key` text NOT NULL COMMENT 'key',
  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';

/******************************************/
/*   Table Name = 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 'content',
  `gmt_modified` datetime NOT NULL COMMENT 'Modification time',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant field',
  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='Add tenant field';

/******************************************/
/*   Table Name = 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 'Creation time',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
  `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant field',
  `encrypted_data_key` text NOT NULL COMMENT 'key',
  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';

/******************************************/
/*   Table Name = 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 'Creation time',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time',
  `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';

/******************************************/
/*   Table Name = 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, auto-increment identifier',
  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';

/******************************************/
/*   Table Name = group_capacity           */
/******************************************/
CREATE TABLE `group_capacity` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Primary Key ID',
  `group_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Group ID, empty character indicates the entire cluster',
  `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Quota, 0 indicates using default value',
  `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Usage',
  `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Single configuration size limit, in bytes, 0 indicates using default value',
  `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum number of aggregated sub-configurations, 0 indicates using default value',
  `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Single aggregated data sub-configuration size limit, in bytes, 0 indicates using default value',
  `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum change history count',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_group_id` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Cluster and Group capacity information table';

/******************************************/
/*   Table Name = 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, auto-increment identifier',
  `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 'Creation time',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time',
  `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 'tenant field',
  `encrypted_data_key` text NOT NULL COMMENT 'key',
  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='Multi-tenant transformation';

/******************************************/
/*   Table Name = tenant_capacity          */
/******************************************/
CREATE TABLE `tenant_capacity` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Primary Key ID',
  `tenant_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Tenant ID',
  `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Quota, 0 indicates using default value',
  `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Usage',
  `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Single configuration size limit, in bytes, 0 indicates using default value',
  `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum number of aggregated sub-configurations',
  `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Single aggregated data sub-configuration size limit, in bytes, 0 indicates using default value',
  `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum change history count',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Tenant capacity information table';

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 'Creation time',
  `gmt_modified` bigint(20) NOT NULL COMMENT 'Modification time',
  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');

ENV Environment#

PREFER_HOST_MODE=hostname
MODE=standalone
SPRING_DATASOURCE_PLATFORM=mysql
MYSQL_SERVICE_HOST=192.168.208.128
MYSQL_SERVICE_DB_NAME=nacos
MYSQL_SERVICE_PORT=3306
MYSQL_SERVICE_USER=root	
MYSQL_SERVICE_PASSWORD=mysql_liu
MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true

Seata Configuration#

Application.yaml file for starting Seata TC [Nacos]#

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

console:
  user:
    username: seata
    password: seata

seata:
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos
    nacos:
      server-addr: nacos_ip:nacos_port
      namespace: seata-server
      group: SEATA_GROUP
      username: nacos
      password: nacos
      data-id: seataServer.properties

  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos
    nacos:
      application: seata-server
      server-addr: nacos_ip:nacos_port
      group: SEATA_GROUP
      namespace: seata-server
      # TC cluster name
      cluster: default
      username: nacos
      password: nacos
#  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 configuration file seataServer.properties#

store.mode=db
#-----db-----
store.db.datasource=druid
store.db.dbType=mysql
# The driverClassName needs to be adjusted according to the version of MySQL
# Driver for MySQL 8 and above: com.mysql.cj.jdbc.Driver
# Driver for MySQL below 8: com.mysql.jdbc.Driver
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata-server?useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
store.db.user= username
store.db.password=password
# Initial number of database connections
store.db.minConn=1
# Maximum number of database connections
store.db.maxConn=20
# Maximum wait time for obtaining connections, default 5000, in milliseconds
store.db.maxWait=5000
# Global transaction table name, default global_table
store.db.globalTable=global_table
# Branch transaction table name, default branch_table
store.db.branchTable=branch_table
# Global lock table name, default lock_table
store.db.lockTable=lock_table
# Maximum number of records to query for global transactions at once, default 100
store.db.queryLimit=100


# Undo retention days, default 7 days, log_status=1 (Appendix 3) and undo that has not been cleaned up normally
server.undo.logSaveDays=7
# Undo cleanup thread interval time, default 86400000, in milliseconds
server.undo.logDeletePeriod=86400000
# Timeout duration for retrying two-phase commit, unit ms, s, m, h, d, corresponding to milliseconds, seconds, minutes, hours, days, default milliseconds. Default value -1 means infinite retries
# Formula: timeout>=now-globalTransactionBeginTime, true means no more retries after timeout
# Note: After reaching the timeout, no retries will be made, there is a risk of data inconsistency, unless the business can calibrate the data itself, otherwise use with caution
server.maxCommitRetryTimeout=-1
# Timeout duration for retrying two-phase rollback
server.maxRollbackRetryTimeout=-1
# Interval time for retrying submission of unfinished global transactions in two-phase commit state, default 1000, in milliseconds
server.recovery.committingRetryPeriod=1000
# Interval time for retrying submission in two-phase asynchronous commit state, default 1000, in milliseconds
server.recovery.asynCommittingRetryPeriod=1000
# Interval time for retrying rollback in two-phase rollback state, default 1000, in milliseconds
server.recovery.rollbackingRetryPeriod=1000
# Interval time for retrying timeout state detection, default 1000, in milliseconds, detecting timeout will place the global transaction into the rollback session manager
server.recovery.timeoutRetryPeriod=1000

This article is synchronized and updated to xLog by Mix Space
The original link is https://me.liuyaowen.club/posts/default/20240816and3


Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.