Docker-compose构建Spring Cloud Alibaba之Mysql,Nacos,Nginx,Seata,Redis,ElasticSearch,SkyWalking,sentinel 超详细
Docker-compose构建Spring Cloud Alibaba之Mysql,Nacos,Nginx,Seata,Redis,ElasticSearch ,SkyWalking,Sentinel
Linux CentOS 部署目录结构:
/home/docker/
├── hostname
│ ├── config
│ │ ├── mysql
│ │ │ └── my.cnf
│ │ └── nginx
│ │ │ └── nginx.conf
│ │ └── seata
│ │ │ └── registry.conf
│ │ └── redis
│ │ └── redis.conf
│ ├── data
│ │ ├── mysql
│ │ ├── nacos
│ │ └── nginx
│ ├── docker-compose-all.yaml
│ ├── docker-compose-config.yaml
│ ├── docker-compose-discovery.yaml
│ ├── docker-compose.yaml
│ ├── env
│ │ ├── mysql
│ │ │ └── mysql.env
│ │ └── nacos
│ │ └── nacos.env
│ ├── .env
│ ├── init
│ │ └── mysql
│ │ └── init.sql
│ ├── log
│ │ ├── mysql
│ │ ├── nacos1
│ │ ├── nacos2
│ │ ├── nacos3
│ │ └── nginx
│ ├── start.sh
│ └── stop.sh
└── ip
├── config
│ ├── mysql
│ │ └── my.cnf
│ └── nginx
│ │ └── nginx.conf
│ └── seata
│ │ └── registry.conf
│ └── redis
│ └── redis.conf
├── data
│ ├── mysql
│ ├── nacos
│ └── nginx
├── docker-compose-all.yaml
├── docker-compose-config.yaml
├── docker-compose-discovery.yaml
├── docker-compose.yaml
├── env
│ ├── mysql
│ │ └── mysql.env
│ └── nacos
│ └── nacos.env
├── .env
├── init
│ └── mysql
│ └── init.sql
├── log
│ ├── mysql
│ ├── nacos1
│ ├── nacos2
│ ├── nacos3
│ └── nginx
├── start.sh
└── stop.sh
1、构建Mysql配置文件,不用创建挂载目录,会自动创建。
#创建mysql的my.cnf配置文件目录
mkdir -p /home/docker/config/mysql
cd /home/docker/config/mysql
touch my.cnf
vi my.cnf
[client]
default-character-set=utf8mb4
[mysqld]
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
# 禁用dns反查
skip-name-resolve
# 数据库唯一 ID,主从的标识号绝对不能重复。
server-id=1
# 开启 bin-log,并指定文件目录和文件名前缀
# 如果要使用挂载的路径,先设置log-bin=binlog,再入docker容器内设置权限
#docker exec -it 容器id /bin/bash
#chown -R mysql:mysql /var/log/mysql/
#log-bin=/var/log/mysql/binlog
log-bin=binlog
# bin-log 日志文件格式,设置为 MIXED 可以防止主键重复。
binlog_format=mixed
[mysql]
default-character-set=utf8mb4
2、构建Nginx配置文件,不用创建挂载目录,web目录、日志目录 会自动创建。
#创建配置目录,一定要先创建,不然执行docker cp找不到目录
mkdir -p /home/docker/config/nginx
#用docker命令启动对应的Nginx版本,
docker run -p 80 -d --name ng nginx:1.21.1
#进入docker容器内查看文件:docker exec -it 06ba008ac23a /bin/bash
#拷贝配置文件到/home/docker/config/nginx/ ,docker cp后跟容器ID或容器名称
docker cp ng:/etc/nginx/nginx.conf /home/docker/config/nginx/
#在nginx.conf配置NACOS负载均衡
cd /home/docker/config/nginx/
vi nginx.conf
2.1、hostname配置方式
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
# nacos2 TCP转发,不必添加网上所说的–with-stream,Docker-compose安装完Nginx之后默认支持stream
stream {
upstream nacos-server-grpc9848 {
hash $remote_addr consistent;
server nacos1:9848 weight=1 max_fails=3 fail_timeout=10s;
server nacos2:9848 weight=1 max_fails=3 fail_timeout=10s;
}
server {
listen 9848 so_keepalive=on;
proxy_connect_timeout 5s;
proxy_timeout 5s;
proxy_pass nacos-server-grpc9848;
}
upstream nacos-server-grpc9849 {
hash $remote_addr consistent;
server nacos1:9849 weight=1 max_fails=3 fail_timeout=10s;
server nacos2:9849 weight=1 max_fails=3 fail_timeout=10s;
}
server {
listen 9849 so_keepalive=on;
proxy_connect_timeout 20s;
proxy_timeout 5m;
proxy_pass nacos-server-grpc9849;
}
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
#与IP配置不同的地方,这里主要采用docker容器端口
upstream dispense {
server nacos1:8848 weight=1;
server nacos2:8848 weight=1;
}
server {
listen 8848;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://dispense;
index index.html index.htm;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
add_header Cache-Control no-cache;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
}
2.2、IP配置方式:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
# nacos2 TCP转发,不必添加网上所说的–with-stream,Docker-compose安装完Nginx之后默认支持stream
stream {
upstream nacos-server-grpc9848 {
hash $remote_addr consistent;
server nacos1:9000 max_fails=3 fail_timeout=10s;
server nacos2:9002 max_fails=3 fail_timeout=10s;
}
server {
listen 9848;
proxy_connect_timeout 20s;
proxy_timeout 5m;
proxy_pass nacos-server-grpc9848;
}
upstream nacos-server-grpc9849 {
hash $remote_addr consistent;
server nacos1:9001 max_fails=3 fail_timeout=10s;
server nacos2:9003 max_fails=3 fail_timeout=10s;
}
server {
listen 9849;
proxy_connect_timeout 20s;
proxy_timeout 5m;
proxy_pass nacos-server-grpc9849;
}
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
#与hostname配置不同的地方,这里主要采用外网端口
upstream dispense {
server nacos1:8000;
server nacos2:8002;
}
server {
listen 8848;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://dispense;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
}
3、创建seata配置文件 registry.conf
# 创建seata目录
mkdir -p /home/docker/config/seata
cd /home/docker/config/seata
touch registry.conf
vi registry.conf
# 如果Spring Boot提示java.util.concurrent.TimeoutException: Waited 3000 milliseconds
# 这里的Nacos有错误或先关闭Nacos的权限认证注册上之后打开权限认证(这个问题搞了好几天)
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
loadBalance = "RandomLoadBalance"
loadBalanceVirtualNodes = 10
nacos {
application = "seata-server"
serverAddr = "192.168.1.88:8848"
group = "SEATA_GROUP"
namespace = "seata"
cluster = "default"
username = "nacos"
password = "2004"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "192.168.1.88:8848"
namespace = "seata"
group = "SEATA_GROUP"
username = "nacos"
password = "2004"
}
}
4、创建Redis配置文件 redis.conf
#创建redis目录
mkdir -p /home/docker/config/redis
cd /home/docker/config/redis
touch redis.conf
vi redis.conf
#开启后台运行,docker“不能”设置这个,否则启动不了
#daemonize yes
#端口
port 6379
#数据库访问密码
requirepass 1234
#开启AOF
appendonly yes
5、创建Mysql重复配置文件mysql.env
#创建文件目录
mkdir -p /home/docker/env/mysql
cd /home/docker/env/mysql
touch mysql.env
vi mysql.env
TZ=Asia/Shanghai
MYSQL_ROOT_HOST=%
6、创建Naocs重复配置文件 nacos.env
#创建目录
mkdir -p /home/docker/env/nacos
cd /home/docker/env/nacos
touch nacos.env
vi nacos.env
6.1、hostname配置方式
# 根据服务器实际内存大小更改JVM,2.0.3至少要128M才能启动起来
TZ=Asia/Shanghai
JVM_XMS=128m
JVM_XMX=128m
JVM_XMN=32m
#MODE=cluster
MODE=standalone
# 与IP不同的地方
PREFER_HOST_MODE=hostname
NACOS_SERVERS=nacos1:8848 nacos2:8848
NACOS_AUTH_ENABLE=true
MYSQL_SERVICE_HOST=mysql
MYSQL_SERVICE_DB_NAME=nacos_config
# Nacos2.0版本一定要添加以下2行,连接参数必须一致
SPRING_DATASOURCE_PLATFORM=mysql
MYSQL_SERVICE_DB_PARAM=useSSL=false&autoReconnect=true&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&connectTimeout=10000&socketTimeout=30000&allowPublicKeyRetrieval=true
6.2、IP配置方式
# 根据服务器实际内存大小更改JVM,Nacos2.0.3必须要128M才能跑起来
TZ=Asia/Shanghai
JVM_XMS=128m
JVM_XMX=128m
JVM_XMN=32m
MODE=cluster
#MODE=standalone
# 与hostname不同的地方
PREFER_HOST_MODE=ip
NACOS_AUTH_ENABLE=true
MYSQL_SERVICE_HOST=mysql
MYSQL_SERVICE_DB_NAME=nacos_config
# Nacos2.0版本一定要添加以下2行,连接参数必须一致
SPRING_DATASOURCE_PLATFORM=mysql
MYSQL_SERVICE_DB_PARAM=useSSL=false&autoReconnect=true&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&connectTimeout=10000&socketTimeout=30000&allowPublicKeyRetrieval=true
7、创建Mysql初始化nacos-mysql.sql脚本文件,该文件位置:nacos\nacos1.4.1\conf\nacos-mysql.sql。
#创建初始化SQL目录及文件
mkdir /home/docker/init/mysql
cd /home/docker/init/mysql
toutch nacos-mysql.sql
vi nacos-mysql.sql
-- 创建数据库,数据库及表建好通过Navicat查看数据库编码(重装CentOs后不设置utf8mb3又是可以的)
-- 如果不是utf8mb3和utf8_general_ci一定要修改选utf8和utf8_general_ci,否则Nacos2连接提示No DataSource set
DROP database IF EXISTS `nacos_config`;
CREATE DATABASE `nacos_config` default character set utf8mb4 collate utf8mb4_unicode_ci;
-- 切换数据库
USE nacos_config;
/******************************************/
/* 数据库全名 = nacos_config */
/* 表名称 = 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(255) DEFAULT NULL,
`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,
`tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
`c_desc` varchar(256) DEFAULT NULL,
`c_use` varchar(64) DEFAULT NULL,
`effect` varchar(64) DEFAULT NULL,
`type` varchar(64) DEFAULT NULL,
`c_schema` text,
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';
/******************************************/
/* 数据库全名 = nacos_config */
/* 表名称 = 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(255) 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,
`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='增加租户字段';
/******************************************/
/* 数据库全名 = nacos_config */
/* 表名称 = 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 '租户字段',
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';
/******************************************/
/* 数据库全名 = nacos_config */
/* 表名称 = 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';
/******************************************/
/* 数据库全名 = nacos_config */
/* 表名称 = 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,
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';
/******************************************/
/* 数据库全名 = nacos_config */
/* 表名称 = 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容量信息表';
/******************************************/
/* 数据库全名 = nacos_config */
/* 表名称 = his_config_info */
/******************************************/
CREATE TABLE `his_config_info` (
`id` bigint(64) unsigned NOT NULL,
`nid` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`data_id` varchar(255) NOT NULL,
`group_id` varchar(128) NOT NULL,
`app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
`content` longtext NOT NULL,
`md5` varchar(32) DEFAULT NULL,
`gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`src_user` text,
`src_ip` varchar(50) DEFAULT NULL,
`op_type` char(10) DEFAULT NULL,
`tenant_id` varchar(128) DEFAULT '' 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='多租户改造';
/******************************************/
/* 数据库全名 = nacos_config */
/* 表名称 = 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,
`password` varchar(500) NOT NULL,
`enabled` boolean NOT NULL
);
CREATE TABLE `roles` (
`username` varchar(50) NOT NULL,
`role` varchar(50) NOT NULL,
UNIQUE INDEX `idx_user_role` (`username` ASC, `role` ASC) USING BTREE
);
CREATE TABLE `permissions` (
`role` varchar(50) NOT NULL,
`resource` varchar(255) NOT NULL,
`action` varchar(8) NOT NULL,
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');
8、创建seata数据库脚本 seata-mysql.sql
##创建seata-mysql.sql文件
cd /home/docker/init/mysql
touch seata-mysql.sql
vi seata-mysql.sql
-- 创建数据库
DROP database IF EXISTS `seata`;
CREATE DATABASE `seata` default character set utf8mb4 collate utf8mb4_unicode_ci;
-- 切换数据库
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_gmt_modified_status` (`gmt_modified`, `status`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
-- 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 = utf8;
-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
`row_key` VARCHAR(128) NOT NULL,
`xid` VARCHAR(96),
`transaction_id` BIGINT,
`branch_id` BIGINT NOT NULL,
`resource_id` VARCHAR(256),
`table_name` VARCHAR(32),
`pk` VARCHAR(36),
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`row_key`),
KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
9、创建 .env 文件
cd /home/docker
touch .env
##查看.env隐藏文件需要加参数-a
ls -a
vi .env
9.1、hostname配置方式
# version
MYSQL_VERSION=8.0.27
NACOS_VERSION=v2.0.4
NGINX_VERSION=1.21.1
# FIRST_VERSION=1.0.0
# SECOND_VERSION=1.0.0
# CONSUMER_VERSION=1.0.0
# ORDER_VERSION=1.0.0
# USER_VERSION=1.0.0
# pwd,mysql密码不能包含@#,否则seata连接不上
MYSQL_SERVICE_USER=root
MYSQL_ROOT_PASSWORD=2004
NACOS_USER=nacos
NACOS_PASSWORD=2004
# env
# ORDER_ENV=dev
# USER_ENV=dev
9.2、IP配置方式
# version
MYSQL_VERSION=8.0.27
NACOS_VERSION=v2.0.4
NGINX_VERSION=1.21.1
# FIRST_VERSION=1.0.0
# SECOND_VERSION=1.0.0
# CONSUMER_VERSION=1.0.0
# ORDER_VERSION=1.0.0
# USER_VERSION=1.0.0
# pwd
MYSQL_SERVICE_USER=root
MYSQL_ROOT_PASSWORD=2004
NACOS_USER=nacos
NACOS_PASSWORD=2004
# env
# ORDER_ENV=dev
# USER_ENV=dev
# 与host配置不同的地方,增加IP地址及端口
# nacos ip
# nacos ip配置方式才需要使用以下3个配置
#NACOS_SERVER_IP=192.168.1.88
#NACOS1_APPLICATION_PORT=8000
#NACOS2_APPLICATION_PORT=8002
10、安装ElasticSearch需修改sysctl.conf添加vm.max_map_count = 262144,否则启动报内存不足
# 查看当前限制
$ sysctl vm.max_map_count
vm.max_map_count = 65530
# 在sysctl.conf添加 vm.max_map_count = 262144
$ vi /etc/sysctl.conf
# ES选项
vm.max_map_count = 262144
# Redis
vm.overcommit_memory = 1
# 配置生效
$ sysctl vm.overcommit_memory=1
# 启用配置
$ sysctl -p
11、创建Sentine的Dockerfile文件
#上传sentinel-dashboard-1.8.3.jar到build目录
#创建Dockerfile文件
vi Dockerfile
FROM openjdk:8
LABEL author="Scode<stc66@qq.com>"
RUN echo "Asia/Shanghai" > /etc/timezone
#设置工目录了后面就可以不用再设置绝对路径
ENV MYPATH /apps
RUN mkdir ${MYPATH}
WORKDIR ${MYPATH}
ADD ./*.jar sentinel.jar
EXPOSE 8858 8719
ENTRYPOINT ["java","-Dserver.port=8858","-Dcsp.sentinel.api.port=8719","-Dsentinel.dashboard.auth.username=stc","-Dsentinel.dashboard.auth.password=200412aaAA","-Dcsp.sentinel.dashboard.server=localhost:8858","-Dproject.name=sentinel","-jar","sentinel.jar"]
11.1、构建Sentinel
docker build -t sentinel:1.8.3 .
12、创建docker-compose.yml文件
##创建文件
cd /home/docker
touch docker-compose.yml
vi docker-compose.yml
12.1、hostname配置方式:
version: '3.9'
services:
mysql: #服务名
container_name: mysql #容器名
image: mysql:${MYSQL_VERSION}
privileged: true
env_file:
- env/mysql/mysql.env
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} #加载.env文件里的配置变量,查看隐藏文件.env需要ls -a命令
ports:
- "3306:3306"
volumes:
- ./config/mysql/my.cnf:/etc/mysql/my.cnf
- ./init/mysql/:/docker-entrypoint-initdb.d/ #初始化sql脚本数据
- ./data/mysql/data/:/var/lib/mysql/
- ./data/mysql/mysql-files/:/var/lib/mysql-files/ #mysql8需要这个配置
- ./log/mysql/:/var/log/mysql/
command:
--character-set-server=utf8mb4
--collation-server=utf8mb4_unicode_ci
restart: always #开机启动
nacos1:
hostname: nacos1 #IP配置方式无此项
container_name: nacos1
image: nacos/nacos-server:${NACOS_VERSION}
privileged: true
env_file:
- env/nacos/nacos.env
environment:
MYSQL_SERVICE_USER: ${MYSQL_SERVICE_USER}
MYSQL_SERVICE_PASSWORD: ${MYSQL_ROOT_PASSWORD}
ports:
- "8000:8848" #与IP配置方式不同的地方
- "9000:9848"
- "9001:9849"
volumes:
- ./data/nacos1/:/home/nacos/data/
- ./log/nacos1/:/home/nacos/logs/
depends_on:
- mysql
restart: always
nacos2:
hostname: nacos2 #IP配置方式无此项
container_name: nacos2
image: nacos/nacos-server:${NACOS_VERSION}
privileged: true
env_file:
- env/nacos/nacos.env
environment:
MYSQL_SERVICE_USER: ${MYSQL_SERVICE_USER}
MYSQL_SERVICE_PASSWORD: ${MYSQL_ROOT_PASSWORD}
ports:
- "8002:8848" #与IP配置方式不同的地方
- "9002:9848"
- "9003:9849"
volumes:
- ./data/nacos2/:/home/nacos/data/
- ./log/nacos2/:/home/nacos/logs/
depends_on:
- mysql
restart: always
nginx:
container_name: nginx
image: nginx:${NGINX_VERSION}
privileged: true
environment:
TZ: Asia/Shanghai
ports:
- "8848:8848"
- "9848:9848"
- "9849:9849"
volumes:
- ./config/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./data/nginx/:/usr/share/nginx/html/
- ./log/nginx/:/var/log/nginx/
depends_on:
- nacos1
- nacos2
restart: always
seata-server:
hostname: seata-server
container_name: seata-server
image: seataio/seata-server:1.4.2
privileged: true
ports:
- "8091:8091"
environment:
SEATA_PORT: 8091
STORE_MODE: db
SEATA_IP: 192.168.1.88
TZ: Asia/Shanghai
volumes:
- ./config/seata/registry.conf:/seata-server/resources/registry.conf
- ./log/seata/:/root/logs/seata
depends_on:
- nacos1
restart: always
redis:
container_name: redis
image: redis:6.2.5
privileged: true
ports:
- 6379:6379
environment:
TZ: Asia/Shanghai
volumes:
- ./config/redis/redis.conf:/usr/local/etc/redis/redis.conf
- ./data/redis/:/data/
- ./log/redis/:/redis/log/
command: redis-server /usr/local/etc/redis/redis.conf
sysctls:
- net.core.somaxconn=1024
restart: always
elasticsearch:
hostname: elasticsearch
container_name: elasticsearch
image: elasticsearch:7.14.0
privileged: true
ports:
- 9200:9200
environment:
- TAKE_FILE_OWNERSHIP=true #设置volumes挂载权限
- discovery.type=single-node #单节点模式,集群模式去掉
- bootstrap.memory_lock=true #锁定物理内存地址
- TZ=Asia/Shanghai
- "ES_JAVA_OPTS=-Xms128m -Xmx256m" #堆内存大小
volumes:
#- ./config/elasticsearch/:/usr/share/elasticsearch/config/
- ./data/elasticsearch/data/:/usr/share/elasticsearch/data/
- ./data/elasticsearch/plugins/:/usr/share/elasticsearch/plugins/
- ./log/elasticsearch/:/usr/share/elasticsearch/logs/
ulimits:
memlock:
soft: -1
hard: -1
restart: always
oap:
hostname: oap #一定要设置hostname,否则SW_OAP_ADDRESS: oap:12800连不上
container_name: oap
image: apache/skywalking-oap-server:6.6.0-es7
privileged: true
ports:
- 11800:11800 #上报数据的端口,这是gRPC端口
- 12800:12800 #ui读取数据的端口,这是http端口
environment:
SW_STORAGE: elasticsearch #Es存储
SW_STORAGE_ES_CLUSTER_NODES: elasticsearch:9200 #Es的节点地址
SW_HEALTH_CHECKER: default
SW_TELEMETRY: prometheus
TZ: Asia/Shanghai
healthcheck:
test: [ "CMD", "./bin/swctl", "ch" ]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
depends_on:
- elasticsearch
links:
- elasticsearch
restart: always
ui:
hostname: ui
container_name: ui
image: apache/skywalking-ui:6.6.0
privileged: true
ports:
- 9090:8080
environment:
SW_OAP_ADDRESS: oap:12800
#security.user.admin.password: 1234
TZ: Asia/Shanghai
depends_on:
- oap
links:
- oap
restart: always
sentinel:
container_name: sentinel
image: sentinel:1.8.3
ports:
- 8858:8858
- 8719:8719
environment:
- TZ=Asia/Shanghai
volumes:
- ./log/sentinel/:/root/logs/
restart: always
gitlab:
container_name: gitlab
image: gitlab/gitlab-ce:14.7.0-ce.0
hostname: 192.168.3.15
privileged: true
environment:
TZ: Asia/Shanghai
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://192.168.3.15:2280'
registry_external_url 'http://192.168.3.15:2250' #开启docker registry镜像库功能,docker login $CI_REGISTRY -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD登录失败时需要在gitlab.rb开启才生效
gitlab_rails['gitlab_shell_ssh_port'] = 2222
nginx['listen_port'] = 2280
nginx['client_max_body_size'] = '1024m'
gitlab_rails['time_zone'] = 'Beijing'
gitlab_rails['lfs_enabled'] = true
registry_nginx['enable'] = true
registry_nginx['listen_port'] = 2250
ports:
- "2280:2280"
- "2443:443"
- "2222:22"
- "2250:2250"
volumes:
- ./config/gitlab:/etc/gitlab
- ./data/gitlab:/var/opt/gitlab
- ./log/gitlab/:/var/log/gitlab
- ./config/registry:/var/opt/gitlab/gitlab-rails/shared/registry
restart: always
gitlab-runner:
container_name: gitlab-runner
image: gitlab/gitlab-runner:v14.7.0
privileged: true
volumes:
- ./config/gitlab-runner:/etc/gitlab-runner
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
- gitlab
restart: always
12.2、IP配置方式:
version: '3.9'
services:
mysql: #服务名
container_name: mysql #容器名
image: mysql:${MYSQL_VERSION}
privileged: true
env_file:
- env/mysql/mysql.env
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} #加载.env文件里的配置变量,查看隐藏文件.env需要ls -a命令
ports:
- "3306:3306"
volumes:
- ./config/mysql/my.cnf:/etc/mysql/my.cnf
- ./init/mysql/:/docker-entrypoint-initdb.d/ #初始化sql脚本数据
- ./data/mysql/data/:/var/lib/mysql/
- ./data/mysql/mysql-files/:/var/lib/mysql-files/ #mysql8需要这个配置
- ./log/mysql/:/var/log/mysql/
command:
--character-set-server=utf8mb4
--collation-server=utf8mb4_unicode_ci
restart: always #开机启动
nacos1:
#hostname: nacos1 #IP配置方式无hostname
container_name: nacos1
image: nacos/nacos-server:${NACOS_VERSION}
env_file:
- env/nacos/nacos.env
environment:
MYSQL_SERVICE_USER: ${MYSQL_SERVICE_USER}
MYSQL_SERVICE_PASSWORD: ${MYSQL_ROOT_PASSWORD}
#IP配置方式多出以下设置
NACOS_SERVER_IP: ${NACOS_SERVER_IP}
NACOS_APPLICATION_PORT: ${NACOS1_APPLICATION_PORT}
NACOS_SERVERS: ${NACOS_SERVER_IP}:${NACOS1_APPLICATION_PORT} ${NACOS_SERVER_IP}:${NACOS2_APPLICATION_PORT}
ports:
- "${NACOS1_APPLICATION_PORT}:${NACOS1_APPLICATION_PORT}" #与hostname配置不同的地方,Nacos2还需增加9848、9849
volumes:
- ./data/nacos1/:/home/nacos/data/
- ./log/nacos1/:/home/nacos/logs/
depends_on:
- mysql
restart: always
nacos2:
#hostname: nacos2 #IP配置方式无hostname
container_name: nacos2
image: nacos/nacos-server:${NACOS_VERSION}
env_file:
- env/nacos/nacos.env
environment:
MYSQL_SERVICE_USER: ${MYSQL_SERVICE_USER}
MYSQL_SERVICE_PASSWORD: ${MYSQL_ROOT_PASSWORD}
#IP配置方式多出以下设置
NACOS_SERVER_IP: ${NACOS_SERVER_IP}
NACOS_APPLICATION_PORT: ${NACOS2_APPLICATION_PORT}
NACOS_SERVERS: ${NACOS_SERVER_IP}:${NACOS1_APPLICATION_PORT} ${NACOS_SERVER_IP}:${NACOS2_APPLICATION_PORT}
ports:
- "${NACOS2_APPLICATION_PORT}:${NACOS2_APPLICATION_PORT}" #与hostname配置不同的地方,Nacos2还需增加9848、9849
volumes:
- ./data/nacos2/:/home/nacos/data/
- ./log/nacos2/:/home/nacos/logs/
depends_on:
- mysql
restart: always
nginx:
container_name: nginx
image: nginx:${NGINX_VERSION}
privileged: true
environment:
TZ: Asia/Shanghai
ports:
- "8848:8848"
- "9848:9848"
- "9849:9849"
volumes:
- ./config/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./data/nginx/:/usr/share/nginx/html/
- ./log/nginx/:/var/log/nginx/
depends_on:
- nacos1
- nacos2
restart: always
seata-server:
hostname: seata-server
container_name: seata-server
image: seataio/seata-server:1.4.2
privileged: true
ports:
- "8091:8091"
environment:
SEATA_PORT: 8091
STORE_MODE: db
SEATA_IP: 192.168.1.88
TZ: Asia/Shanghai
volumes:
- ./config/seata/registry.conf:/seata-server/resources/registry.conf
- ./log/seata/:/root/logs/seata
depends_on:
- nacos1
restart: always
redis:
container_name: redis
image: redis:6.2.5
privileged: true
ports:
- 6379:6379
environment:
TZ: Asia/Shanghai
volumes:
- ./config/redis/redis.conf:/usr/local/etc/redis/redis.conf
- ./data/redis/:/data/
- ./log/redis/:/redis/log/
command: redis-server /usr/local/etc/redis/redis.conf
sysctls:
- net.core.somaxconn=1024
restart: always
elasticsearch:
hostname: elasticsearch
container_name: elasticsearch
image: elasticsearch:7.14.0
privileged: true
ports:
- 9200:9200
environment:
- TAKE_FILE_OWNERSHIP=true #设置volumes挂载权限
- discovery.type=single-node #单节点模式,集群模式去掉
- bootstrap.memory_lock=true #锁定物理内存地址
- TZ=Asia/Shanghai
- "ES_JAVA_OPTS=-Xms128m -Xmx256m" #堆内存大小
volumes:
#- ./config/elasticsearch/:/usr/share/elasticsearch/config/
- ./data/elasticsearch/data/:/usr/share/elasticsearch/data/
- ./data/elasticsearch/plugins/:/usr/share/elasticsearch/plugins/
- ./log/elasticsearch/:/usr/share/elasticsearch/logs/
ulimits:
memlock:
soft: -1
hard: -1
restart: always
oap:
hostname: oap #一定要设置hostname,否则SW_OAP_ADDRESS: oap:12800连不上
container_name: oap
image: apache/skywalking-oap-server:6.6.0-es7
privileged: true
ports:
- 11800:11800 #上报数据的端口,这是gRPC端口
- 12800:12800 #ui读取数据的端口,这是http端口
environment:
SW_STORAGE: elasticsearch #Es存储
SW_STORAGE_ES_CLUSTER_NODES: elasticsearch:9200 #Es的节点地址
SW_HEALTH_CHECKER: default
SW_TELEMETRY: prometheus
TZ: Asia/Shanghai
healthcheck:
test: [ "CMD", "./bin/swctl", "ch" ]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
depends_on:
- elasticsearch
links:
- elasticsearch
restart: always
ui:
hostname: ui
container_name: ui
image: apache/skywalking-ui:6.6.0
privileged: true
ports:
- 9090:8080
environment:
SW_OAP_ADDRESS: oap:12800
#security.user.admin.password: 1234
TZ: Asia/Shanghai
depends_on:
- oap
links:
- oap
restart: always
sentinel:
container_name: sentinel
image: sentinel:1.8.3
ports:
- 8858:8858
- 8719:8719
environment:
- TZ=Asia/Shanghai
volumes:
- ./log/sentinel/:/root/logs/
restart: always
gitlab:
container_name: gitlab
image: gitlab/gitlab-ce:14.7.0-ce.0
hostname: 192.168.3.15
privileged: true
environment:
TZ: Asia/Shanghai
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://192.168.3.15:2280'
registry_external_url 'http://192.168.3.15:2250' #开启docker registry镜像库功能,docker login $CI_REGISTRY -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD登录失败时需要在gitlab.rb开启才生效
gitlab_rails['gitlab_shell_ssh_port'] = 2222
nginx['listen_port'] = 2280
nginx['client_max_body_size'] = '1024m'
gitlab_rails['time_zone'] = 'Beijing'
gitlab_rails['lfs_enabled'] = true
registry_nginx['enable'] = true
registry_nginx['listen_port'] = 2250
ports:
- "2280:2280"
- "2443:443"
- "2222:22"
- "2250:2250"
volumes:
- ./config/gitlab:/etc/gitlab
- ./data/gitlab:/var/opt/gitlab
- ./log/gitlab/:/var/log/gitlab
- ./config/registry:/var/opt/gitlab/gitlab-rails/shared/registry
restart: always
gitlab-runner:
container_name: gitlab-runner
image: gitlab/gitlab-runner:v14.7.0
privileged: true
volumes:
- ./config/gitlab-runner:/etc/gitlab-runner
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
- gitlab
restart: always
13、在Windows本机下载seata源码、修改config.txt文件:路径script\config-center\config.txt
#tx_group:自定义名字,很重要,与项目配置文件的tx-service-group一一对应
service.vgroupMapping.my_test_tx_group=default
#seata的ip+端口
service.default.grouplist=192.168.148.88:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
#修改存储模式为数据库存储
store.mode=db
store.db.datasource=druid
#指定数据库为mysql,其他数据库类型根据自身情况修改
store.db.dbType=mysql
#指定引擎,mysql8以下为com.mysql.jdbc.Driver,mysql8为com.mysql.cj.jdbc.Driver
store.db.driverClassName=com.mysql.cj.jdbc.Driver
#一定要修改IP,不能设置127.0.0.1,否则连不上
store.db.url=jdbc:mysql://192.168.148.88:3306/seata?useUnicode=true
store.db.user=root
#Mysql密码不能包含@#,否则seata连接不上,提示not found service provider for : io.seata.core.store.db.DataSourceProvider
store.db.password=1234
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
14、将修改好的config.txt配置添加到nacos配置中心,nacos账号密码千万不能包含@#,它的bug
#将config.txt文件放到nacos-config.sh文件所在目录的上一级目录(nacos-config.sh不作任何修改)
#nacos的账号密码千万千万不能包含@#,否则连不上,因为这个问题搞了一整天
sh nacos-config.sh -h 192.168.148.88 -p 8848 -g SEATA_GROUP -t seata -u nacos -w 123456
15、设置elasticsearch权限
#查看日志:
docker logs -ft elasticsearch
#报错误1:Error opening log file 'logs/gc.log': Permission denied
#报错误2:java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes
#报以上2个错误需设置elasticsearch权限
#如果日志也报权限问题,同样设置/home/docker/log/elasticsearch权限
chmod 777 /home/docker/data/elasticsearch/data
16、查看端口是否启用:
netstat -anpt
17、功能验证
#Docker日志查看:docker logs -tf elasticsearch
#Nacos日志查看:tail -f log/nacos1/start.out
#1、验证ES是否正常
http://192.168.148.88:9200/
18、关闭Nacos2.0以上版本的双写
# accessToken参数为权限登录token
curl -X PUT 'localhost:8848/nacos/v1/ns/operator/switches?entry=doubleWriteEnabled&value=false&accessToken=eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJuYWNvcyIsImV4cCI6MTYzNjExNDcyMX0.siRrOwALQB4kYM-u-D8QhXEom8oE1nqwD7IbN6HAV-M'