一、项目介绍
系统模块
com.mingink
|--mingink-api // 接口模块
| └──mingink-api-system // 系统接口
|--mingink-common // 通用模块
| └──mingink-common-core // 系统接口
|--mingink-gateway // 网关模块【8081】
|--mingink-modules // 业务模块
| └──mingink-modules-system // 系统服务模块【8082】
| └──mingink-modules-article // 文章服务模块【8083】
|--pom.xml // 公共依赖
技术选型
开发框架:Spring Boot 3.2.2、Spring Cloud 2023.0.0、Spring Cloud Alibaba 2022.0.0.0
网关:Gateway
配置中心:Nacos 2.3.3
缓存:Redis 6.2.14
数据库:MySQL 8.0.11
目的
本次部署的是基于Spring Cloud的微服务项目,需要将现有的gateway网关模块、mingink-system系统服务模块、mingink-article文章服务模块分别打成Jar,上传到CentOS云服务器,再使用docker compose进行快速部署。
部署流程
- Docker 和 Docker Compose安装
- 环境部署
- 文件同步
- 打包
- 服务部署
- 测试访问
二、Docker 和 Docker Compose安装
Docker
参考:Centos安装docker
(1)安装 yum 工具
yum install yum-utils -y
(2)配置 yum 源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
(3)安装 docker
yum install -y docker-ce-19.03.9 docker-ce-cli-19.03.9 containerd.io
(4)启动 docker 并且设置开机启动
systemctl start docker
systemctl enable docker
(5)测试 docker
docker -v
Docker Compose
(1)安装 docker-compose-plugin
yum install docker-compose-plugin
(2)重启docker
systemctl restart docker
(3)测试 docker compose
docker compose version
三、环境部署
在部署项目服务之前,需要使用 docker compose 将项目所依赖的服务都配置好(这样将项目迁移到另一台服务器也十分方便)。在 Spring Cloud 项目中,以最常用到的 Mysql 8.0.11、Redis 6.2.14 和 Nacos 2.2.3 为例来准备相应 Docker Compose 文件。
3.1 MySQL
首先我们需要准备好需要的库表结构(包括项目业务数据库 mingink 和 Nacos 配置数据库 nacos_config),放在微服务项目根目录 mysql-init 文件夹中:
项目数据库建表语句就根据项目需求自行完成,这里给出 nacos_config 的建表语句(也可以在官网找):
-- 创建库
create database if not exists nacos_config;
-- 切换库
use nacos_config;
-- 创建表
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for config_info
-- ----------------------------
create table if not exists config_info (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
`data_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'data_id',
`group_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`content` longtext CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'content',
`md5` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NULL 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 CHARACTER SET utf8 COLLATE utf8_bin NULL COMMENT 'source user',
`src_ip` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'source ip',
`app_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`tenant_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT '' COMMENT '租户字段',
`c_desc` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`c_use` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`effect` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`type` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`c_schema` text CHARACTER SET utf8 COLLATE utf8_bin NULL,
`encrypted_data_key` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '秘钥',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `uk_configinfo_datagrouptenant`(`data_id` ASC, `group_id` ASC, `tenant_id` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 47 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = 'config_info' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for config_info_aggr
-- ----------------------------
create table if not exists config_info_aggr (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
`data_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'data_id',
`group_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'group_id',
`datum_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'datum_id',
`content` longtext CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '内容',
`gmt_modified` datetime NOT NULL COMMENT '修改时间',
`app_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`tenant_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT '' COMMENT '租户字段',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `uk_configinfoaggr_datagrouptenantdatum`(`data_id` ASC, `group_id` ASC, `tenant_id` ASC, `datum_id` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = '增加租户字段' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for config_info_beta
-- ----------------------------
create table if not exists config_info_beta (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
`data_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'data_id',
`group_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'group_id',
`app_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'app_name',
`content` longtext CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'content',
`beta_ips` varchar(1024) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'betaIps',
`md5` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NULL 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 CHARACTER SET utf8 COLLATE utf8_bin NULL COMMENT 'source user',
`src_ip` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'source ip',
`tenant_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT '' COMMENT '租户字段',
`encrypted_data_key` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '秘钥',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `uk_configinfobeta_datagrouptenant`(`data_id` ASC, `group_id` ASC, `tenant_id` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = 'config_info_beta' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for config_info_tag
-- ----------------------------
create table if not exists config_info_tag (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
`data_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'data_id',
`group_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'group_id',
`tenant_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT '' COMMENT 'tenant_id',
`tag_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'tag_id',
`app_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'app_name',
`content` longtext CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'content',
`md5` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NULL 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 CHARACTER SET utf8 COLLATE utf8_bin NULL COMMENT 'source user',
`src_ip` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'source ip',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `uk_configinfotag_datagrouptenanttag`(`data_id` ASC, `group_id` ASC, `tenant_id` ASC, `tag_id` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = 'config_info_tag' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for config_tags_relation
-- ----------------------------
create table if not exists config_tags_relation (
`id` bigint(20) NOT NULL COMMENT 'id',
`tag_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'tag_name',
`tag_type` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'tag_type',
`data_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'data_id',
`group_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'group_id',
`tenant_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT '' COMMENT 'tenant_id',
`nid` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`nid`) USING BTREE,
UNIQUE INDEX `uk_configtagrelation_configidtag`(`id` ASC, `tag_name` ASC, `tag_type` ASC) USING BTREE,
INDEX `idx_tenant_id`(`tenant_id` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = 'config_tag_relation' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for group_capacity
-- ----------------------------
create table if not exists group_capacity (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`group_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin 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`) USING BTREE,
UNIQUE INDEX `uk_group_id`(`group_id` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = '集群、各Group容量信息表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for his_config_info
-- ----------------------------
create table if not exists his_config_info (
`id` bigint(20) UNSIGNED NOT NULL,
`nid` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`data_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`group_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`app_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'app_name',
`content` longtext CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`md5` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`src_user` text CHARACTER SET utf8 COLLATE utf8_bin NULL,
`src_ip` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`op_type` char(10) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`tenant_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT '' COMMENT '租户字段',
`encrypted_data_key` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '秘钥',
PRIMARY KEY (`nid`) USING BTREE,
INDEX `idx_gmt_create`(`gmt_create` ASC) USING BTREE,
INDEX `idx_gmt_modified`(`gmt_modified` ASC) USING BTREE,
INDEX `idx_did`(`data_id` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 61 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = '多租户改造' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for permissions
-- ----------------------------
create table if not exists permissions (
`role` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`resource` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`action` varchar(8) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
UNIQUE INDEX `uk_role_permission`(`role` ASC, `resource` ASC, `action` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for roles
-- ----------------------------
create table if not exists roles (
`username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`role` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
UNIQUE INDEX `idx_user_role`(`username` ASC, `role` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for tenant_capacity
-- ----------------------------
create table if not exists tenant_capacity (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`tenant_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin 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`) USING BTREE,
UNIQUE INDEX `uk_tenant_id`(`tenant_id` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = '租户容量信息表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for tenant_info
-- ----------------------------
create table if not exists tenant_info (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
`kp` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'kp',
`tenant_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT '' COMMENT 'tenant_id',
`tenant_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT '' COMMENT 'tenant_name',
`tenant_desc` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'tenant_desc',
`create_source` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'create_source',
`gmt_create` bigint(20) NOT NULL COMMENT '创建时间',
`gmt_modified` bigint(20) NOT NULL COMMENT '修改时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `uk_tenant_info_kptenantid`(`kp` ASC, `tenant_id` ASC) USING BTREE,
INDEX `idx_tenant_id`(`tenant_id` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = 'tenant_info' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for users
-- ----------------------------
create table if not exists users (
`username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`password` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`enabled` tinyint(1) NOT NULL,
PRIMARY KEY (`username`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
然后在服务器找个位置创建 mysql 的数据存储目录/usr/soft/docker/mingink/mysql/data和配置目录/usr/soft/docker/mingink/mysql/conf ,之后就可以开始攥写 docker-compose.env.yml 文件了,示例代码如下:
version: '3'
services:
mysql:
image: mysql # 使用的镜像
container_name: mysql # 启动的示例名称
restart: always # 崩溃后自动重启
ports:
- "3306:3306" # 端口映射
environment:
TZ: Asia/Shanghai # 控制时区
MYSQL_ROOT_PASSWORD: root123456 # mysql root用户密码
volumes:
- /usr/soft/docker/mingink/mysql/data:/var/lib/mysql # 将数据目录挂载到本地目录以进行持久化
- /usr/soft/docker/mingink/mysql/conf:/etc/mysql/conf.d # 挂载配置文件目录
- ./env/mysql/mysql-init:/docker-entrypoint-initdb.d # 启动脚本(创建容器后会自动执行建表语句)
networks:
- mynetwork # 指定网络
# 网络,不定义的话就是默认网络
networks:
mynetwork: # 自定义网络,实现网络互通和隔离
3.2 Redis
Redis 服务和Mysql的类型,先创建配置文件 redis.conf ,再创建数据目录 /usr/soft/docker/mingink/redis/data 和日志目录 /usr/soft/docker/mingink/redis/logs ,示例代码如下:
redis.conf
# 绑定的 IP 地址,如果设置为 127.0.0.1,则只能本地访问;若设置为 0.0.0.0,则监听所有接口(默认:127.0.0.1)
bind 0.0.0.0
protected-mode yes
# Redis 服务器的端口号(默认:6379)
port 6379
tcp-backlog 511
# 设置密码,客户端连接时需要提供密码才能进行操作,如果不设置密码,可以注释掉此行(默认:无)
# requirepass redis123
# 设置在客户端闲置一段时间后关闭连接,单位为秒(默认:0,表示禁用)
# timeout 0
# 是否以守护进程(daemon)模式运行,默认为 "no",设置为 "yes" 后 Redis 会在后台运行
daemonize yes
# 设置日志级别(默认:notice)。可以是 debug、verbose、notice、warning
loglevel notice
# 设置日志文件的路径(默认:空字符串),如果不设置,日志会输出到标准输出
logfile ""
# 设置数据库数量(默认:16),Redis 使用数据库索引从 0 到 15
databases 16
# 是否启用 AOF 持久化,默认为 "no"。如果设置为 "yes",将在每个写操作执行时将其追加到文件中
appendonly yes
# 设置 AOF 持久化的文件路径(默认:appendonly.aof)
appendfilename "appendonly.aof"
# AOF 持久化模式,默认为 "always"。可以是 always、everysec 或 no
# always:每个写操作都立即同步到磁盘
# everysec:每秒钟同步一次到磁盘
# no:完全依赖操作系统的行为,可能会丢失数据,但性能最高
appendfsync always
# 设置是否在后台进行 AOF 文件重写,默认为 "no"
# auto-aof-rewrite-on-rewrite no
# 设置 AOF 文件重写触发时,原 AOF 文件大小与新 AOF 文件大小之间的比率(默认:100)
# auto-aof-rewrite-percentage 100
# 设置是否开启 RDB 持久化,默认为 "yes"。如果设置为 "no",禁用 RDB 持久化功能
save 900 1
save 300 10
save 60 10000
docker-compose.env.yml
redis:
image: redis:6.2.14
container_name: redis
restart: always
ports:
- "6379:6379"
environment:
TZ: Asia/Shanghai
volumes:
- /usr/soft/docker/mingink/redis/data:/data # 数据目录
- ./env/redis/redis.conf:/usr/local/etc/redis/redis.conf # 配置文件
- /usr/soft/docker/mingink/redis/logs:/logs # 日志, 未成功?
networks:
- mynetwork
3.3 Nacos
在上面的 mysql 中创建好 nacos_config 数据库后,需要在创建日志目录 /usr/soft/docker/mingink/nacos/logs。然后创建一个 nacos.env 环境配置文件:
MODE=standalone # 单节点启动
SPRING_DATASOURCE_PLATFORM=mysql # 单击模式下支持MYSQL数据库
MYSQL_SERVICE_HOST=mysql
MYSQL_SERVICE_PORT=3306
MYSQL_SERVICE_DB_NAME=nacos_config
MYSQL_SERVICE_USER=root
MYSQL_SERVICE_PASSWORD=root123456
MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=10000&socketTimeout=3000&autoReconnect=true&useSSL=false&serverTimezone=UTC
NACOS_AUTH_IDENTITY_KEY=mingink-key
NACOS_AUTH_IDENTITY_VALUE=mingink-value
NACOS_AUTH_TOKEN_SECRET_KEY=SecretKey012345678901234567890123456789012345678901234567890123456789
NACOS_AUTH_ENABLE=true # 打开授权系统
# NACOS_AUTH_TOKEN生成:参考https://juejin.cn/post/7213677398720757821
NACOS_AUTH_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjpbeyJtaW5naW5rIjoibG92ZWx1bWluZSJ9XSwiaWF0IjoxNzEwMDcxNzY4LCJleHAiOjIyMTQ5MjE1OTksImF1ZCI6IiIsImlzcyI6IiIsInN1YiI6IiJ9.eKkg8cr-kVBIMUajTzFZ0pyRxws3dvSBYsXjuYTp-yg
TZ=Asia/Shanghai # 控制时区
docker-compose.env.yml
nacos:
image: nacos/nacos-server:v2.2.3
container_name: nacos
restart: always
ports:
- "8848:8848"
- "9848:9848" # gRPC
environment:
TZ: Asia/Shanghai
env_file:
- env/nacos/nacos.env # nacos配置
volumes:
- /usr/soft/docker/mingink/nacos/logs:/home/nacos/logs # 日志
networks:
- mynetwork
总的 docker-compose.env.yml 如下:
version: '3'
services:
mysql:
image: mysql # 使用的镜像
container_name: mysql # 启动的示例名称
restart: always # 崩溃后自动重启
ports:
- "3306:3306" # 端口映射
environment:
TZ: Asia/Shanghai # 控制时区
MYSQL_ROOT_PASSWORD: root123456 # mysql root用户密码
volumes:
- /usr/soft/docker/mingink/mysql/data:/var/lib/mysql # 将数据目录挂载到本地目录以进行持久化
- /usr/soft/docker/mingink/mysql/conf:/etc/mysql/conf.d # 挂载配置文件目录
- ./env/mysql/mysql-init:/docker-entrypoint-initdb.d # 启动脚本(创建容器后会自动执行建表语句)
networks:
- mynetwork # 指定网络
redis:
image: redis:6.2.14
container_name: redis
restart: always
ports:
- "6379:6379"
environment:
TZ: Asia/Shanghai
volumes:
- /usr/soft/docker/mingink/redis/data:/data # 数据目录
- ./env/redis/redis.conf:/usr/local/etc/redis/redis.conf # 配置文件
- /usr/soft/docker/mingink/redis/logs:/logs # 日志, 未成功?
networks:
- mynetwork
nacos:
image: nacos/nacos-server:v2.2.3
container_name: nacos
restart: always
ports:
- "8848:8848"
- "9848:9848" # gRPC
environment:
TZ: Asia/Shanghai
env_file:
- env/nacos/nacos.env # nacos配置
volumes:
- /usr/soft/docker/mingink/nacos/logs:/home/nacos/logs # 日志
networks:
- mynetwork
# 网络,不定义的话就是默认网络
networks:
mynetwork: # 自定义网络,实现网络互通和隔离
四、服务部署
4.1 文件同步
下面,需要把微服务项目源码上传到服务器上(也可以使用FTP 等工具手动上传每次新打好的JAR包和docker-compose文件,但每次修改都需要重新上传,非常麻烦)。我们使用 IDEA 自带的远程部署功能,实现配置文件的自动上传,步骤如下:
1)在 工具 中选择 部署 的 配置 选项:
2)添加远程部署
3)指定连接的服务器配置:
4)配置本地文件和服务器文件路径映射:
5)开启自动上传:
这样你每次在项目中修改的文件(比如 docker-compose.env.yml)都会自动同步到远程服务器,不需要再手动通过 FTP 上传。
6) 首次需要手动上传文件。
上传完成后,就可以在刚才文件映射的目录( /usr/soft/mingink ) 下找到已上传的文件列表。
4.2 获取 jar 包
打包配置
打包的pom配置和方法可以参考上一篇文章Spring Cloud部署篇1——Jar包部署至CentOS云服务器。
在项目根目录下执行打包命令:
mvn clean package
然后把各模块(我这里有三个模块)的 jar 包上传到服务器(每次重新打包都需要):
4.3 编写业务服务配置
与环境部署一样,我们可以编写业务服务的 Docker Compose 文件 docker-compose.env.yml,示例代码如下:
version: '3'
services:
mingink-gateway:
container_name: mingink-gateway
build: # 服务的 Docker 构建文件位置
context: ./mingink-gateway
dockerfile: Dockerfile
ports:
- "8081:8081"
networks:
- mynetwork
volumes:
- ./logs/mingink-gateway:/app/log
mingink-modules-system:
container_name: mingink-modules-system
build:
context: ./mingink-modules/mingink-modules-system
dockerfile: Dockerfile
ports:
- "8082:8082"
networks:
- mynetwork
depends_on: # 本服务依赖的服务,控制启动先后顺序
- mingink-gateway
volumes:
- ./logs/mingink-modules-system:/app/log
mingink-modules-article:
container_name: mingink-modules-article
build:
context: ./mingink-modules/mingink-modules-article
dockerfile: Dockerfile
ports:
- "8083:8083"
networks:
- mynetwork
depends_on: # 本服务依赖的服务,控制启动先后顺序
- mingink-gateway
volumes:
- ./logs/mingink-modules-article:/app/log
# 网络,不定义的话就是默认网络
networks:
mynetwork:
然后在各模块下编写相应的 Dockerfile 文件,以 mingink-gateway 为例:
# 基础镜像
FROM openjdk:17-jdk-alpine
# 指定工作目录
WORKDIR /app
# 将 jar 包添加到工作目录
ADD target/mingink-gateway.jar .
# 创建日志目录
RUN chmod -R 777 /app && mkdir /app/log
# 设置时区
ENV TimeZone=Asia/Shanghai
# 使用软连接,并且将时区配置覆盖/etc/timezone (同步docker与宿主机的时区)
RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone
# 暴露端口
EXPOSE 8081
# 启动命令
# CMD允许运行时覆盖,而ENTRYPOINT不可以
CMD ["sh", "-c", "java -jar mingink-gateway.jar >./log/log.out 2>&1"]
其它两个模块 mingink-modules-systems 和 mingink-modules-article 模块也类似,把模块名称改改就行。
五、运行 Docker Compose 文件
在服务器的项目根目录下,先执行环境部署文件 docker-compose.env.yml:
docker compose -f docker-compose.env.yml up -d --build
再执行业务部署文件 docker-compose.service.yml
docker compose -f docker-compose.service.yml up -d --build
然后查看 docker 容器运行情况:
docker ps -a
如果遇到某一服务启动失败,可以单独启动该服务:
ocker compose -f docker-compose.service.yml up mingink-gateway
然后就可以开始测试你安装的服务是否正常运行了。