单体部署
提示
在部署之前,您需要准备相应的应用环境,您也可以通过参考快速体验章节中准备docker环境
准备工作
- docker准备完成后创建映射目录。shell
mkdir -p /opt/lite/mysql/conf /opt/lite/mysql/data /opt/lite/redis/data /opt/lite/redis/config /opt/lite/minio/data
- 任意目录创建docker-compose.yml文件,并写入以下内容。yaml
version: '3.8' services: mysql: image: mysql:8.3.0 ports: - 3306:3306 volumes: - /opt/lite/mysql/conf/mysql.cnf:/etc/mysql/conf.d/mysql.cnf - /opt/lite/mysql/data/:/var/lib/mysql/ privileged: true environment: - MYSQL_ROOT_PASSWORD=Abc123++ - MYSQL_DATABASE=iking_framework - TZ=Asia/Shanghai restart: always container_name: mysql command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci healthcheck: test: ["CMD", "mysqladmin", "ping", "-h", "localhost"] interval: 10s timeout: 5s start_period: 60s redis: image: redis:7.2.4 ports: - 6379:6379 volumes: - /opt/lite/redis/data:/data - /opt/lite/redis/config/redis.conf:/usr/local/etc/redis/redis.conf privileged: true restart: always container_name: redis command: redis-server --requirepass Abc123++ --appendonly yes --notify-keyspace-events Ex healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 30s timeout: 20s start_period: 60s minio: image: minio/minio:RELEASE.2024-01-31T20-20-33Z ports: - 9000:9000 - 9090:9090 volumes: - /opt/lite/minio/data:/data privileged: true environment: - MINIO_ROOT_USER=minio - MINIO_ROOT_PASSWORD=Abc123++ restart: always container_name: minio command: server /data --console-address ":9090" -address ":9000" healthcheck: test: ["CMD", "curl", "-f", "http://127.0.0.1:9000/minio/health/live"] interval: 30s timeout: 20s start_period: 60s
安装
- 进入docker-compose.yml文件所在目录,执行启动命令,并等待完成。shell
docker compose up -d
- 启动完成后,执行以下命令确认所有容器启动成功,至此,所有环境准备完成。shell
docker ps -a
💡 特别说明:
如果您的运行环境中已安装了数据库/缓存/对象存储服务等组件,您需要从docker-compose.yml文件中删除相应部分。例如,您的环境中已安装了MySQL数据库,那么从docker-compose.yml中移除以下部分的内容:
yamlmysql: image: mysql:8.3.0 ports: - 3306:3306 volumes: - /opt/lite/mysql/conf/mysql.cnf:/etc/mysql/conf.d/mysql.cnf - /opt/lite/mysql/data/:/var/lib/mysql/ privileged: true environment: - MYSQL_ROOT_PASSWORD=Abc123++ - MYSQL_DATABASE=iking_framework - TZ=Asia/Shanghai restart: always container_name: mysql command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci healthcheck: test: ["CMD", "mysqladmin", "ping", "-h", "localhost"] interval: 10s timeout: 5s start_period: 60s
相应的,您还需要将服务启动时的环境变量进行修改。仍以MySQL为例,您需要将环境变量
MYSQL_HOST
、MYSQL_PORT
、MYSQL_SCHEMA_NAME
、MYSQL_USERNAME
、MYSQL_PASSWORD
修改为您的数据库的信息。
所有环境变量的说明请参考以下列表:变量 说明 SRV_PORT 服务运行端口 MYSQL_HOST 数据库服务地址 MYSQL_PORT 数据库服务端口 MYSQL_SCHEMA_NAME 数据库名,默认iking_framework MYSQL_USERNAME 数据库服务用户名 MYSQL_PASSWORD 数据库服务密码 REDIS_HOST Redis服务地址 REDIS_PORT Redis服务端口 REDIS_DB Redis DB号 REDIS_PASSWORD Redis密码 OSS_ENABLED 是否启用文件服务 OSS_STORAGE_TYPE 对象存储服务类型,目前仅支持AWS_S3类型 OSS_HOST 对象存储服务地址,格式为http://ip:port OSS_ACCESS_KEY 对象存储服务AccessKey OSS_SECRET_KEY 对象存储服务SecretKey OSS_DEFAULT_BUCKET 默认桶名称 OSS_REGION 桶所在区域标识 服务默认使用
iking_framework
作为数据库名称,请确保您的数据库实例中没有同名的数据库。如果您希望自定义数据库名,请修改docker-compose.yml文件中的相应配置。
部署运行
打包
在项目根目录创建以下脚本并执行。
- windows环境shell
@echo off rem 本地Maven安装路径 set Path=%Path%;"D:\Program Files\apache-maven-4.0.0\bin" rem 本地JDK安装路径 set JAVA_HOME=D:\Program Files\Java\jdk-17.0.8 rem 本地仓库路径 set LOCAL_MAVEN_REPO="D:\MavenRepo" cd gateway call mvn -Dmaven.repo.local=%LOCAL_MAVEN_REPO% clean package cd ../auth call mvn -Dmaven.repo.local=%LOCAL_MAVEN_REPO% clean package cd ../server call mvn -Dmaven.repo.local=%LOCAL_MAVEN_REPO% clean package cd ../lite call mvn -Dmaven.repo.local=%LOCAL_MAVEN_REPO% clean package
- Linux环境shell
#!/bin/bash # 本地Maven安装路径 export PATH=$PATH;/root/apache-maven-4.0.0/bin # 本地JDK安装路径 export JAVA_HOME=/root/Java/jdk-17.0.8 # 本地仓库路径 export LOCAL_MAVEN_REPO=/root/MavenRepo cd gateway call mvn -Dmaven.repo.local=$LOCAL_MAVEN_REPO clean package cd ../auth call mvn -Dmaven.repo.local=$LOCAL_MAVEN_REPO% clean package cd ../server call mvn -Dmaven.repo.local=$LOCAL_MAVEN_REPO% clean package cd ../lite call mvn -Dmaven.repo.local=$LOCAL_MAVEN_REPO% clean package
部署、制作Docker镜像
- 在jar包所在目录下创建Dockerfile,填入以下内容。dockerfile
FROM openjdk:17 ADD lite.jar /opt/iking/lite.jar RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' > /etc/timezone ENV JAVA_OPS="-Ddruid.wall.multiStatementAllow=true --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/sun.net.util=ALL-UNNAMED" EXPOSE 8000 CMD cd /opt/iking && java -jar ${JAVA_OPS} lite.jar
- 在jar包所在目录执行
docker build
命令进行镜像制作。shelldocker build -f Dockerfile -t lite .
- 执行docker run命令启动服务。shell
docker run -d --name lite -p 8000:8000\ -e SRV_PORT=8000\ -e MYSQL_HOST=${数据库服务地址}\ -e MYSQL_PORT=${数据库服务端口}\ -e MYSQL_SCHEMA_NAME=${数据库名称}\ -e MYSQL_USERNAME=${数据库服务用户名}\ -e MYSQL_PASSWORD=${数据库服务密码}\ -e REDIS_HOST=${Redis服务地址}\ -e REDIS_PORT=${Redis服务端口}\ -e REDIS_DB=${Redis DB号}\ -e REDIS_PASSWORD=${Redis服务密码}\ -e OSS_ENABLED=true\ -e OSS_STORAGE_TYPE=AWS_S3\ -e OSS_HOST=${对象存储服务地址,格式为http(s)://ip:port}\ -e OSS_ACCESS_KEY=${对象存储服务AccessKey}\ -e OSS_SECRET_KEY=${对象存储服务SecretKey}\ -e OSS_DEFAULT_BUCKET=${默认桶名称}\ -e OSS_REGION=${桶所在区域标识}\ --restart=always\ lite
- 您也可以创建Dockerfile后,在jar包所在目录下创建以下脚本来启动服务。shell
#!/bin/bash docker stop lite || true docker rm lite || true docker rmi lite || true docker build -f Dockerfile -t lite . docker run -d --name lite -p 8000:8000\ -e SRV_PORT=8000\ -e MYSQL_HOST=${数据库服务地址}\ -e MYSQL_PORT=${数据库服务端口}\ -e MYSQL_SCHEMA_NAME=${数据库名称}\ -e MYSQL_USERNAME=${数据库服务用户名}\ -e MYSQL_PASSWORD=${数据库服务密码}\ -e REDIS_HOST=${Redis服务地址}\ -e REDIS_PORT=${Redis服务端口}\ -e REDIS_DB=${Redis DB号}\ -e REDIS_PASSWORD=${Redis服务密码}\ -e OSS_ENABLED=true\ -e OSS_STORAGE_TYPE=AWS_S3\ -e OSS_HOST=${对象存储服务地址,格式为http(s)://ip:port}\ -e OSS_ACCESS_KEY=${对象存储服务AccessKey}\ -e OSS_SECRET_KEY=${对象存储服务SecretKey}\ -e OSS_DEFAULT_BUCKET=${默认桶名称}\ -e OSS_REGION=${桶所在区域标识}\ --restart=always\ lite
- 前端服务打包请参考前端部署文档 点此查看
提示
推荐使用nginx作为web发布工具,部署方式微服务单体均适用。
以下是nginx推荐配置:
conf
load_module /usr/lib/nginx/modules/ngx_stream_module.so;
worker_processes auto;
events {
worker_connections 1024;
accept_mutex on;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 75s;
gzip on;
gzip_min_length 4k;
gzip_comp_level 4;
client_max_body_size 1024m;
client_header_buffer_size 32k;
client_body_buffer_size 8m;
server_names_hash_bucket_size 512;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
gzip_types application/javascript application/x-javascript text/javascript text/css application/json application/xml;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
location /api/ {
proxy_pass http://192.168.2.167:8000/;
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 X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location / {
root /home/nginxWebUI/www/web/;
index index.html;
}
location /share {
alias /home/nginxWebUI/www/web/;
index index.share.html;
}
location /server/jmreport {
proxy_pass http://192.168.2.167:6002/jmreport;
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 X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /mobile {
alias /home/nginxWebUI/www/web/mobile/;
index index.html;
}
}
}