服务器端提供了docker-compose文件,方便快速搭建后台服务。
部署服务器端 参考上一步,通过下载源码启动后端服务项目。
修改配置文件 打开servuce-oa/src/main/resources/application.yaml
,修改配置信息
spring: application: name: service-oa profiles: active: prod
打开application-prod.yaml
,修改password和prefix:
application-prod.yamlspring: datasource: url: jdbc:mysql://mysql:3306/guigu-oa?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8 username: root password: xxxxxxxx redis: host: redis password: xxxxxxxx wx: mp: app-id: xxxxxxxxxxxxxxxxxx secret: xxxxxxxxxxxxxxxxxxxxxxx wechat: prefix: http://xxxxxx.yyy.cn
注意!因为使用Docker所有涉及服务之间的网络通讯,这里mysql和redis的host不能是localhost
或者127.0.0.1
。可以使用真实的IP,或者为所有服务创建一个自定义网络,它们之间使用服务名 通讯。
app-id :微信公众号的appId
secret :微信公众号的secret
prefix :微信公众号的授权回调页面域名
👈获取方式:微信公众号测试申请
打包项目 使用maven.package打包项目,完成后可在service-oa/target
中找到。
将service-oa.jar
和Dockerfile
上传到Linux服务器的同一目录,方便构建镜像。
构建Docker镜像 Docker
需要安装Docker,安装方式可以在本站的Docker笔记中找到。
Dockerfile FROM openjdk:8 ADD service-oa.jar service-oa.jar RUN echo "Asia/Shanghai" > /etc/timezone EXPOSE 8800 ENTRYPOINT ["java" , "-jar" , "/service-oa.jar" ] MAINTAINER sw-code
确保service-oa.jar
和Dockerfile
在同一目录下,使用下面命令构建
docker build -t service-oa .
使用ps命令查看镜像:
使用docker-compose编排服务 创建docker-compose.yaml文件,写入
docker-compose.yaml version: "1" services: microService: image: service-oa container_name: oa ports: - "8800:8800" volumes: - /compose/microService:/data networks: - oa_net depends_on: - redis - mysql redis: image: redis:7.0.8 ports: - "6379:6379" volumes: - /compose/redis/redis.conf:/etc/redis/redis.conf - /compose/redis/data:/data networks: - oa_net command: /bin/sh -c "redis-server /etc/redis/redis.conf" privileged: true mysql: image: mysql:8 environment: MYSQL_ROOT_PASSWORD: 'xxxxxx' MYSQL_ALLOW_EMPTY_PASSWORD: 'no' MYSQL_DATABASE: 'guigu-oa' ports: - "3306:3306" volumes: - /compose/mysql/data:/var/lib/mysql - /compose/mysql/conf:/etc/mysql/conf.d - /compose/mysql/db:/docker-entrypoint-initdb.d - /compose/mysql/logs:/logs networks: - oa_net command: --default-authentication-plugin=mysql_native_password networks: oa_net:
配置Redis 打开docker-compose.yaml文件,需要提前准备好redis.conf配置文件:
redis: image: redis:7.0.8 ports: - "6379:6379" volumes: - /compose/redis/redis.conf:/etc/redis/redis.conf - /compose/redis/data:/data networks: - oa_net command: redis-server /etc/redis/redis.conf
使用下面命令创建:
mkdir -p /compose/redis/ vim /compose/redis/redis.conf
Redis配置文件,下载地址:https://redis.io/docs/management/config/
修改以下几项,密码和项目保持一致:
appendonly yes bind 0.0.0.0 requirepass xxxxxxxx
启动服务 DockerCompose
使用docker-compose同样需要配置,配置方式可以在本站的Docker笔记中找到。
进入docker-compose.yaml所在目录,使用下面命令启动:
查看服务情况
[root@swcode home] CONTAINER ID IMAGE PORTS NAMES 098ce7d9d8d1 service-oa 0.0.0.0:8800->8800/tcp oa 489f7552b8cc redis:7.0.8 0.0.0.0:6379->6379/tcp home-redis-1 6c6e9bd618cb mysql:8 0.0.0.0:3306->3306/tcp, 33060/tcp home-mysql-1
配置数据库
使用远程连接方式导入数据
使用下面方式进入容器:
docker exec -it home-mysql-1 /bin/sh
登陆mysql
修改权限
ALTER USER 'root' @'%' IDENTIFIED WITH mysql_native_password BY 'xxxxxx' ;
刷新权限
使用Navicat等软件,创建数据库guigu-oa
,使用SQL文件导入表结构和数据
部署前端 编译项目 修改.env.production
,注意网站前缀要和部署时保持一致
.env.production# 网站前缀 VUE_APP_BASE_URL = /cloud-oa-admin
打包项目
压缩,方便上传
tar -zcvf dist.tar.gz dist/
Dockerfile FROM nginxLABEL maintainer="swcode <2627311935@qq.com>" COPY dist /usr/share/nginx/html/cloud-oa-admin/ COPY nginx.conf /etc/nginx/nginx.conf ENTRYPOINT nginx -g "daemon off;"
Nginx配置 创建nginx.conf
配置文件,基于location实现二级访问目录,代理后端接口,修改配置信息如下:
nginx.confworker_processes 1 ;events { worker_connections 1024 ; }http { include mime.types; default_type application/json; sendfile on ; keepalive_timeout 65 ; server { listen 80 ; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } location ^~/cloud-oa-admin { alias /usr/share/nginx/html/cloud-oa-admin; index index.html index.htm; try_files $uri $uri / /cloud-oa-admin/index.html; } location /api { default_type application/json; keepalive_timeout 30s ; keepalive_requests 1000 ; proxy_http_version 1 .1 ; rewrite /api(/.*) $1 break ; proxy_pass_request_headers on ; proxy_next_upstream error timeout; proxy_pass http://microService:8800; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } include servers/*; }
注意!配置了root
之后,其他的二级目录需要使用alias
做代理时,使用同一个自定义网络下的服务名
上传文件 将dist.tar.gz上传到服务器,使用下面命令解压
确保下面三个文件在同一目录
Dockerfile nginx.conf dist/
build镜像 使用下面命令,build镜像
form-designer :表示镜像名,注意和docker-compose.yaml
保持一致
‘.’ :表示当前目录
ngnix没有指定版本会自动拉取最新版,等待镜像build。。。
更新Docker Compose 修改docker-compose.yaml
,添加前端镜像
docker-compose.yamlversion: "1" services: microService: image: service-oa container_name: oa ports: - "8800:8800" volumes: - /compose/microService:/data networks: - oa_net depends_on: - redis - mysql web: image: web-oa container_name: web ports: - "82:80" networks: - oa_net redis: image: redis:7.0.8 ports: - "6379:6379" volumes: - /compose/redis/redis.conf:/etc/redis/redis.conf - /compose/redis/data:/data networks: - oa_net command: /bin/sh -c "redis-server /etc/redis/redis.conf" privileged: true mysql: image: mysql:8 environment: MYSQL_ROOT_PASSWORD: 'xxxxxx' MYSQL_ALLOW_EMPTY_PASSWORD: 'no' MYSQL_DATABASE: 'guigu-oa' ports: - "3306:3306" volumes: - /compose/mysql/data:/var/lib/mysql - /compose/mysql/conf:/etc/mysql/conf.d - /compose/mysql/db:/docker-entrypoint-initdb.d - /compose/mysql/logs:/logs networks: - oa_net command: --default-authentication-plugin=mysql_native_password networks: oa_net:
启动项目 进入docker-compose.yaml
所在文件,执行下面命令
使用docker ps
命令查看容器
[root@swcode cloudOA] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 87586a93bda2 service-oa "java -jar /service-…" 4 seconds ago Up 3 seconds 0.0.0.0:8800->8800/tcp oa 393edce99db4 web-oa "/bin/sh -c 'nginx -…" 4 seconds ago Up 3 seconds 0.0.0.0:82->80/tcp web e31581190458 mysql:8 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp cloudoa-mysql-1 aa34fbcd0ecc redis:6.2.6 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:6379->6379/tcp cloudoa-redis-1
使用浏览器访问:http://ip:82/cloud-oa-admin