From 188070b37f470c0300a693d91ae9f1d728e284ce Mon Sep 17 00:00:00 2001 From: K8sCat Date: Fri, 12 Jan 2024 19:31:02 +0800 Subject: [PATCH 1/3] optimize: deploy --- .gitignore | 4 +- api.Dockerfile | 17 +++++ doc/sql/campus_imaotai-1.0.5.sql | 3 + docker-compose.yml | 76 +++++++++++++++++++ .../application-prod.yml | 4 +- templates/web.conf | 27 +++++++ web.Dockerfile | 10 +++ 7 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 api.Dockerfile create mode 100644 docker-compose.yml rename {campus-modular/src/main/resources => templates}/application-prod.yml (89%) create mode 100644 templates/web.conf create mode 100644 web.Dockerfile diff --git a/.gitignore b/.gitignore index 1205c03e..e874c09c 100644 --- a/.gitignore +++ b/.gitignore @@ -40,4 +40,6 @@ build/ ### mac ### .DS_Store -.flattened-pom.xml \ No newline at end of file +.flattened-pom.xml + +volumes/ diff --git a/api.Dockerfile b/api.Dockerfile new file mode 100644 index 00000000..46e37673 --- /dev/null +++ b/api.Dockerfile @@ -0,0 +1,17 @@ +FROM maven:3.9.6-amazoncorretto-8-debian AS build +WORKDIR /app +COPY . . +RUN mvn clean package + +FROM openjdk:8 +LABEL MAINTAINER="K8sCat " +ENV TZ=Asia/Shanghai +WORKDIR /app +EXPOSE 8160 +COPY --from=build /app/campus-modular/target/campus-modular.jar ./app.jar +ENTRYPOINT ["java", \ + "-Djava.security.egd=file:/dev/./urandom", \ + "-Dspring.config.additional-location=/app/application-prod.yml", \ + "-Dserver.port=8160", \ + "-jar", "/app/app.jar", \ + "--spring.profiles.active=prod"] diff --git a/doc/sql/campus_imaotai-1.0.5.sql b/doc/sql/campus_imaotai-1.0.5.sql index df81ceb1..d20ffffe 100644 --- a/doc/sql/campus_imaotai-1.0.5.sql +++ b/doc/sql/campus_imaotai-1.0.5.sql @@ -2,6 +2,9 @@ Date: 02/08/2023 17:33:32 */ +CREATE DATABASE campus_imaotai; +USE campus_imaotai; + SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0; diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..73198aff --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,76 @@ +services: + imaotai-db: + image: mysql:8.0.35 + container_name: imaotai-db + environment: + MYSQL_ROOT_PASSWORD: 123456789 + volumes: + - ./volumes/db/data/:/var/lib/mysql/ + - ./volumes/db/logs:/logs + - ./doc/sql/campus_imaotai-1.0.5.sql:/docker-entrypoint-initdb.d/campus_imaotai-1.0.5.sql + command: [ + 'mysqld', + '--innodb-buffer-pool-size=80M', + '--character-set-server=utf8mb4', + '--collation-server=utf8mb4_unicode_ci', + '--default-time-zone=+8:00', + '--lower-case-table-names=1', + '--default-authentication-plugin=mysql_native_password' + ] + networks: + - imaotai_net + restart: always + + imaotai-redis: + image: redis:6.2.12 + container_name: imaotai-redis + volumes: + - ./volumes/redis/data/:/redis/data/ + networks: + - imaotai_net + restart: always + + imaotai-api: + image: imaotai-api:latest + build: + context: . + dockerfile: ./api.Dockerfile + container_name: imaotai-api + volumes: + - ./templates/application-prod.yml:/app/application-prod.yml + networks: + - imaotai_net + depends_on: + - imaotai-db + restart: always + healthcheck: + test: ["CMD", "curl", "-f", "http://127.0.0.1:8160/captchaImage"] + interval: 3s + timeout: 10s + retries: 20 + start_period: 40s + start_interval: 5s + + imaotai-web: + image: imaotai-web:latest + build: + context: . + dockerfile: ./web.Dockerfile + container_name: imaotai-web + volumes: + - ./templates/web.conf:/etc/nginx/conf.d/default.conf + - ./volumes/web/log:/var/log/nginx + privileged: true + networks: + - imaotai_net + ports: + - 80:80 + depends_on: + imaotai-api: + restart: false + condition: service_healthy + restart: always + +networks: + imaotai_net: + name: imaotai_net \ No newline at end of file diff --git a/campus-modular/src/main/resources/application-prod.yml b/templates/application-prod.yml similarity index 89% rename from campus-modular/src/main/resources/application-prod.yml rename to templates/application-prod.yml index 7d5cb153..7a92ea2d 100644 --- a/campus-modular/src/main/resources/application-prod.yml +++ b/templates/application-prod.yml @@ -18,7 +18,7 @@ spring: driverClassName: com.mysql.cj.jdbc.Driver # jdbc 所有参数配置参考 https://lionli.blog.csdn.net/article/details/122018562 # rewriteBatchedStatements=true 批处理优化 大幅提升批量插入更新删除性能(对数据库有性能损耗 使用批量操作应考虑性能问题) - url: jdbc:mysql://localhost:3306/campus_imaotai?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true + url: jdbc:mysql://imaotai-db:3306/campus_imaotai?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true username: root password: 123456789 # 从库数据源 @@ -52,7 +52,7 @@ spring: spring: redis: # 地址 - host: localhost + host: imaotai-redis # 端口,默认为6379 port: 6379 # 数据库索引 diff --git a/templates/web.conf b/templates/web.conf new file mode 100644 index 00000000..587a4a1b --- /dev/null +++ b/templates/web.conf @@ -0,0 +1,27 @@ +upstream api { + server imaotai-api:8160; +} + +server { + listen 80; + server_name localhost; + + location / { + root /app; + 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://api/; + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root html; + } +} diff --git a/web.Dockerfile b/web.Dockerfile new file mode 100644 index 00000000..3acb5532 --- /dev/null +++ b/web.Dockerfile @@ -0,0 +1,10 @@ +FROM node:16 AS build +WORKDIR /app +COPY ./vue_campus_admin . +RUN npm i && npm run build:prod + +FROM nginx:1.23.4 +LABEL MAINTAINER="K8sCat " +ENV TZ=Asia/Shanghai +RUN mkdir -p /app +COPY --from=build /app/dist/ /app/ From 4c01ccadc4b5355d79ecc25f8addcc8a23252f3a Mon Sep 17 00:00:00 2001 From: K8sCat Date: Fri, 2 Feb 2024 22:20:10 +0800 Subject: [PATCH 2/3] update: web.Dockerfile Signed-off-by: K8sCat --- web.Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web.Dockerfile b/web.Dockerfile index 3acb5532..18f173ad 100644 --- a/web.Dockerfile +++ b/web.Dockerfile @@ -1,7 +1,8 @@ FROM node:16 AS build WORKDIR /app COPY ./vue_campus_admin . -RUN npm i && npm run build:prod +RUN npm i --registry https://registry.npm.taobao.org \ + && npm run build:prod FROM nginx:1.23.4 LABEL MAINTAINER="K8sCat " From c9d0bd6a749d23fef637ce7f795ec512a6daaa4e Mon Sep 17 00:00:00 2001 From: K8sCat Date: Sat, 3 Feb 2024 00:02:42 +0800 Subject: [PATCH 3/3] update: web.Dockerfile Signed-off-by: K8sCat --- web.Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web.Dockerfile b/web.Dockerfile index 18f173ad..bbe05b6d 100644 --- a/web.Dockerfile +++ b/web.Dockerfile @@ -1,7 +1,7 @@ FROM node:16 AS build WORKDIR /app COPY ./vue_campus_admin . -RUN npm i --registry https://registry.npm.taobao.org \ +RUN npm i --registry=https://registry.npmmirror.com \ && npm run build:prod FROM nginx:1.23.4