nacos Dockerfile(不需要特殊处理,使用docker conpose可以不写)
# 基础镜像
FROM nacos/nacos-server
# author
MAINTAINER jianglifeng<jlifeng@foxmail.com>
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo 'Asia/Shanghai' >/etc/timezone
# 复制conf文件到路径
COPY ./conf/application.properties /home/nacos/conf/application.properties
redis Dockerfile(不需要特殊处理,使用docker conpose可以不写)
# 基础镜像
FROM redis
# author
MAINTAINER jianglifeng<jlifeng@foxmail.com>
#设置时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo 'Asia/Shanghai' >/etc/timezone \
&& mkdir -p /home/nc/redis
# 挂载目录
VOLUME /home/nc/redis
# 指定路径
WORKDIR /home/nc/redis
# 复制conf文件到路径
COPY ./conf/redis.conf /home/nc/redis/redis.conf
nginx Dockerfile (不需要特殊处理,使用docker conpose可以不写)
# 基础镜像
FROM nginx
# author
MAINTAINER jianglifeng<jlifeng@foxmail.com>
#设置时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo 'Asia/Shanghai' >/etc/timezone \
&& mkdir -p /home/nc/projects/nc-ui
# 挂载目录
VOLUME /home/nc/projects/nc-ui
# 指定路径
WORKDIR /home/nc/projects/nc-ui
# 复制conf文件到路径
COPY ./conf/nginx.conf /etc/nginx/nginx.conf
# 复制html文件到路径
COPY ./html/dist /home/nc/projects/nc-ui
业务服务 Dockerfile(直接换上对应服务的jar)
# 基础镜像
FROM openjdk:8-jre
# author
MAINTAINER jianglifeng<jlifeng@foxmail.com>
#设置时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo 'Asia/Shanghai' >/etc/timezone \
&& mkdir -p /home/nc/projects/nc-ui
# 挂载目录
VOLUME /home/nc
# 指定路径
WORKDIR /home/nc
# 启动服务
ENTRYPOINT ["java","-jar","-Dspring.profiles.active=test","jar文件名.jar"]
docker-compose.yml
version : '3.7'
services:
# 生产环境使用单独的nacos
nc-nacos:
container_name: nc-nacos
image: nacos/nacos-server
build:
context: ./nacos
environment:
- MODE=standalone
- TZ=Asia/Shanghai
volumes:
- ./nacos/logs/:/home/nacos/logs
- ./nacos/conf/application.properties:/home/nacos/conf/application.properties
ports:
- "8848:8848"
- "9848:9848"
- "9849:9849"
nc-redis:
container_name: nc-redis
image: redis
build:
context: ./redis
ports:
- "6380:6379"
environment:
- TZ=Asia/Shanghai
volumes:
- ./redis/conf/redis.conf:/home/nc/redis/redis.conf
- ./redis/data:/data
command: redis-server /home/nc/redis/redis.conf
nc-nginx:
container_name: nc-nginx
image: nginx
build:
context: ./nginx
ports:
- "70:80"
environment:
- TZ=Asia/Shanghai
volumes:
- ./nginx/html/dist:/home/nc/projects/nc-ui
- ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/logs:/var/log/nginx
depends_on:
- nc-gateway
links:
- nc-gateway
nc-gateway:
container_name: nc-gateway
build:
context: ./nc/gateway
dockerfile: dockerfile
ports:
- "8080:8080"
volumes:
- ./logs/nc-gateway:/home/nc/logs/nc-gateway
- ./nc/gateway/jar/xx-gateway.jar:/home/nc/xx-gateway.jar
depends_on:
- nc-redis
- nc-nacos
links:
- nc-redis
- nc-nacos
nc-auth:
container_name: nc-auth
build:
context: ./nc/auth
dockerfile: dockerfile
ports:
- "9200:9200"
volumes:
- ./logs/nc-auth:/home/nc/logs/nc-auth
- ./nc/auth/jar/xx-auth.jar:/home/nc/xx-auth.jar
depends_on:
- nc-redis
- nc-nacos
links:
- nc-redis
- nc-nacos
nc-modules-system:
container_name: nc-modules-system
build:
context: ./nc/modules/system
dockerfile: dockerfile
ports:
- "9201:9201"
volumes:
- ./logs/nc-system:/home/nc/logs/nc-system
- ./nc/xx/system/jar/xx-modules-system.jar:/home/nc/xx-modules-system.jar
depends_on:
- nc-redis
- nc-nacos
links:
- nc-redis
- nc-nacos
nc-modules-file:
container_name: nc-modules-file
build:
context: ./nc/modules/file
dockerfile: dockerfile
ports:
- "9202:9202"
volumes:
- ./uploadPath:/home/nc/uploadPath
- ./logs/nc-file:/home/nc/logs/nc-file
- ./nc/modules/file/jar/xx-modules-file.jar:/home/nc/xx-modules-file.jar
nc-modules-marketing:
container_name: nc-modules-marketing
build:
context: ./nc/modules/marketing
dockerfile: dockerfile
ports:
- "9203:9203"
volumes:
- ./logs/nc-marketing:/home/nc/logs/nc-marketing
- ./jar/modules/marketing/xx-modules-marketing.jar:/home/nc/xx-modules-marketing.jar
depends_on:
- nc-redis
- nc-nacos
links:
- nc-redis
- nc-nacos
nginx配置文件(https自行编写)
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /home/nc/projects/nc-ui;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /prod-api/{
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://ruoyi-gateway:8080/;
}
if ($request_uri ~ "/actuator") {
return 403;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
脚本
部署脚本deploy.sh
#!/bin/sh
usage() {
echo "Usage: sh 执行脚本.sh [port|base|modules|stop|rm]"
exit 1
}
base(){
docker-compose up -d nc-redis nc-nacos
}
modules(){
docker-compose up -d 容器名称列表
}
stop(){
docker-compose stop
}
rm(){
docker-compose rm
}
rmi(){
docker rmi 镜像名称列表
}
case "$1" in
"port")
port
;;
"base")
base
;;
"modules")
modules
;;
"stop")
stop
;;
"rm")
rm
;;
*)
usage
;;
esac
批量复制文件到对应目录copy.sh
#!/bin/sh
usage() {
echo "Usage: sh copy.sh"
exit 1
}
echo "begin copy html "
cp -r ../jar-dist/dist/** ./nginx/html/dist
echo "begin copy xx-gateway "
cp ../jar-dist/xx-gateway.jar ./nc/gateway/jar
echo "begin copy xx-auth "
cp ../jar-dist/xx-auth.jar ./nc/auth/jar
echo "begin copy xx-modules-system "
cp ../jar-dist/xx-modules-system.jar ./nc/modules/system/jar
echo "begin copy xx-modules-file "
cp ../jar-dist/xx-modules-file.jar ./nc/modules/file/jar
echo "begin copy xx-modules-marketing "
cp ../jar-dist/xx-modules-marketing.jar ./nc/modules/marketing/jar
目录结构
docker/
│ copy.sh
│ deploy.sh
│ docker-compose.yml
│
├─nacos
│ │ dockerfile
│ │
│ └─conf
│ application.properties
│
├─nc
│ ├─auth
│ │ │ dockerfile
│ │ │
│ │ └─jar
│ │ readme.txt
│ │
│ ├─gateway
│ │ │ dockerfile
│ │ │
│ │ └─jar
│ │ readme.txt
│ │
│ └─modules
│ ├─file
│ │ │ dockerfile
│ │ │
│ │ └─jar
│ │ readme.txt
│ │
│ ├─marketing
│ │ │ dockerfile
│ │ │
│ │ └─jar
│ └─system
│ │ dockerfile
│ │
│ └─jar
│ readme.txt
│
├─nginx
│ │ dockerfile
│ │
│ ├─conf
│ │ nginx.conf
│ │
│ └─html
│ └─dist
│ readme.txt
│
└─redis
│ dockerfile
│
└─conf
redis.conf
日志输出模板logback.xml(放到resources目录下)
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 日志存放路径 -->
<property name="log.path" value="logs/nc-xx" />
<!-- 日志输出格式 -->
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
<!-- 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!-- 系统日志输出 -->
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/info.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>INFO</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/error.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>ERROR</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 系统模块日志级别控制 -->
<logger name="com.xx" level="info" />
<!-- Spring日志级别控制 -->
<logger name="org.springframework" level="warn" />
<root level="info">
<appender-ref ref="console" />
</root>
<!--系统操作日志-->
<root level="info">
<appender-ref ref="file_info" />
<appender-ref ref="file_error" />
</root>
</configuration>