java|码神之路博客项目部署

一、打包springboot程序 1.如果打包出现问题
在File–>Setting–> File Encoding 中 改为UTF-8,防止乱码
java|码神之路博客项目部署
文章图片

将原有的编译插件 更换为

org.apache.maven.plugins maven-resources-plugin 2.7 org.apache.maven.shared maven-filtering 1.3

2.将blog-admin添加到blog-parent里
java|码神之路博客项目部署
文章图片

修改blog-parent的pom文件
java|码神之路博客项目部署
文章图片

3.创建application-prod.properties文件
java|码神之路博客项目部署
文章图片

blog-admin的配置文件:
server.port=8889 spring.application.name=mszlu_admin_blog# datasource spring.datasource.url=jdbc:mysql://mysql的docker容器ip:3306/blog?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=UTC spring.datasource.username=root spring.datasource.password=密码 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver#mybatis-plus mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl mybatis-plus.global-config.db-config.table-prefix=ms_

blog-api的配置文件:
server.port=8888 spring.application.name=mszlu_blog spring.mvc.servlet.path=/api #server.servlet.contextPath=/api#数据库的配置 # datasource spring.datasource.url=jdbc:mysql://mysql的docker容器ip:3306/blog?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=UTC spring.datasource.username=root spring.datasource.password=密码 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver#mybatis-plus mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl mybatis-plus.global-config.db-config.table-prefix=ms_spring.redis.host=redis的docker容器ip spring.redis.port=6379spring.servlet.multipart.max-request-size=20MB spring.servlet.multipart.max-file-size=2MBqiniu.accessKey=111 qiniu.accessSecretKey=111

修改跨域配置
com/mszlu/blog/config/WebMVCConfig.java中:
java|码神之路博客项目部署
文章图片

5.打包
java|码神之路博客项目部署
文章图片

二、安装docker
# 1、yum 包更新到最新 yum update # 2、安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的 yum install -y yum-utils device-mapper-persistent-data lvm2 # 3、 设置yum源 yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo # 4、 安装docker,出现输入的界面都按 y yum install -y docker-ce # 5、 查看docker版本,验证是否验证成功 docker -v #启动docker /bin/systemctl start docker.service

三、安装Docker Compose 运行以下命令以下载 Docker Compose 的当前稳定版本:
$ sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

将可执行权限应用于二进制文件:
$ sudo chmod +x /usr/local/bin/docker-compose

创建软链:
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

测试是否安装成功:
$ docker-compose --version

四、拉取镜像 1.拉取镜像
下面四个镜像中,mysql和redis先创建容器运行起来,不通过compose编排运行
docker pull nginx docker pull redis:5.0.3 docker pull java:8 docker pull mysql:5.7

2.mysql
创建容器,设置端口映射、目录映射
mkdir /mnt/docker/mysql cd /mnt/docker/mysql

docker run -id \ -p 3306:3306 \# 前面的操作系统端口,后面的为容器端口 --name=c_mysql \ -v /mnt/docker/mysql/conf:/etc/mysql/conf.d \ -v /mnt/docker/mysql/logs:/logs \ -v /mnt/docker/mysql/data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=root \ mysql:5.7

在/mnt/docker/mysql/conf 创建my.cnf
[mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2Mdefault-time_zone = '+8:00'datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock character-set-server=utf8 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 lower_case_table_names=1 pid-file=/var/run/mysqld/mysqld.pid sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

去查看mysql对应的docker容器ip地址,项目配置文件中要用
docker inspect c_mysql

3.redis
启动容器
docker run -id --name=redis -p 6379:6379 redis:5.0.3

去查看mysql对应的docker容器ip地址,项目配置文件中要用
docker inspect redis

五、对用到的东西做一些简介 【java|码神之路博客项目部署】1.docker镜像:将其理解为一个镜像
2.docker容器:先要有镜像,然后通过镜像来创建启动容器,可将每个docker容器看做单独的系统
删除镜像或者容器,要先删容器再删镜像:
docker rmi 镜像id或者名称 docker rm 容器id或者名称

3.Dockerfile:本项目中将程序的jar包构建成镜像,使其能够作为容器启动
4.Docker Compose 服务编排:这个项目中用它启动nginx容器,以及自己程序jar包的容器。当对其执行down后编排的所有容器都会停止
六、目录结构 1.后面参照这个结构来上传或者创建文件
java|码神之路博客项目部署
文章图片

2.创建所有需要的文件
app目录下:
blog_dockerfile:
FROM java:8 EXPOSE 8888 ADD ./blog_api.jar /api.jar RUN bash -c 'touch /api.jar' ENTRYPOINT ["java","-jar","/api.jar","--spring.profiles.active=prod"]

这个文件是将blog_api.jar构建成镜像的,blog_admin.jar的修改如下:
FROM java:8 EXPOSE 8889 ADD ./blog_admin.jar /admin.jar RUN bash -c 'touch /admin.jar' ENTRYPOINT ["java","-jar","/admin.jar","--spring.profiles.active=prod"]

mysql目录下:
my.cnf,前面已经创建
docker-compose目录下:
docker-compose.yml
version: '3' services: nginx: image: nginx container_name: nginx ports: #ports:绑定容器的端口到主机的端口,前者为主机端口;expose:将当前容器的端口暴露给link到本容器的容器。 - 80:80 links: - app depends_on: - app volumes: #将本地目录挂载到容器的目录下,前者为linux的目录,后者为容器目录。(访问容器的目录即可访问到被挂载目录下的所有文件) - /mnt/docker/docker-compose/nginx/:/etc/nginx/ #privileged: true network_mode: "bridge" app: image: app container_name: app ports: - 8888:8888 #expose: #- "8888" network_mode: "bridge" admin: image: admin container_name: admin ports: - 8889:8889 #expose: #- "8889" network_mode: "bridge"

nginx目录下:
mime.types:
types { text/htmlhtml htm shtml; text/csscss; text/xmlxml; image/gifgif; image/jpegjpeg jpg; application/javascriptjs; application/atom+xmlatom; application/rss+xmlrss; text/mathmlmml; text/plaintxt; text/vnd.sun.j2me.app-descriptorjad; text/vnd.wap.wmlwml; text/x-componenthtc; image/pngpng; image/svg+xmlsvg svgz; image/tifftif tiff; image/vnd.wap.wbmpwbmp; image/webpwebp; image/x-iconico; image/x-jngjng; image/x-ms-bmpbmp; font/woffwoff; font/woff2woff2; application/java-archivejar war ear; application/jsonjson; application/mac-binhex40hqx; application/msworddoc; application/pdfpdf; application/postscriptps eps ai; application/rtfrtf; application/vnd.apple.mpegurlm3u8; application/vnd.google-earth.kml+xmlkml; application/vnd.google-earth.kmzkmz; application/vnd.ms-excelxls; application/vnd.ms-fontobjecteot; application/vnd.ms-powerpointppt; application/vnd.oasis.opendocument.graphicsodg; application/vnd.oasis.opendocument.presentationodp; application/vnd.oasis.opendocument.spreadsheetods; application/vnd.oasis.opendocument.textodt; application/vnd.openxmlformats-officedocument.presentationml.presentation pptx; application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx; application/vnd.openxmlformats-officedocument.wordprocessingml.document docx; application/vnd.wap.wmlcwmlc; application/x-7z-compressed7z; application/x-cocoacco; application/x-java-archive-diffjardiff; application/x-java-jnlp-filejnlp; application/x-makeselfrun; application/x-perlpl pm; application/x-pilotprc pdb; application/x-rar-compressedrar; application/x-redhat-package-managerrpm; application/x-seasea; application/x-shockwave-flashswf; application/x-stuffitsit; application/x-tcltcl tk; application/x-x509-ca-certder pem crt; application/x-xpinstallxpi; application/xhtml+xmlxhtml; application/xspf+xmlxspf; application/zipzip; application/octet-streambin exe dll; application/octet-streamdeb; application/octet-streamdmg; application/octet-streamiso img; application/octet-streammsi msp msm; audio/midimid midi kar; audio/mpegmp3; audio/oggogg; audio/x-m4am4a; audio/x-realaudiora; video/3gpp3gpp 3gp; video/mp2tts; video/mp4mp4; video/mpegmpeg mpg; video/quicktimemov; video/webmwebm; video/x-flvflv; video/x-m4vm4v; video/x-mngmng; video/x-ms-asfasx asf; video/x-ms-wmvwmv; video/x-msvideoavi; }

nginx.conf:
usernginx; worker_processes1; error_log/var/log/nginx/error.log warn; pid/var/run/nginx.pid; events { worker_connections1024; }http { include/etc/nginx/mime.types; default_typeapplication/octet-stream; log_formatmain'$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log/var/log/nginx/access.logmain; sendfileon; #tcp_nopushon; keepalive_timeout65; #gzipon; include /etc/nginx/conf.d/*.conf; }

conf.d目录下:
blog.conf:
gzip_min_length 1k; gzip_buffers 4 16k; gzip_comp_level 2; gzip_vary off; upstream appstream{server 云服务器的私网ip:8888; }server {listen80; server_name公网ip; indexindex.html; error_page500 502 503 504/50x.html; location = /50x.html { roothtml; } location /api { proxy_pass http://appstream; }location / { root /etc/nginx/html; index index.html; }}

七、发布springboot项目(将jar包构建成docker镜像) 1.将springboot项目打好的包上传到app目录下,分别改名为blog_admin.jar和blog_api.jar
2.使用cd命令进入到app目录下,执行构建命令
# 对应blog_api.jar docker build -f ./blog_dockerfile -t app .


# 对应blog_admin.jar docker build -f ./blog_dockerfile -t admin.

八、前端项目打包和上传 1.修改config\prod.env.js
'use strict' module.exports = { NODE_ENV: '"production"', BASE_API: '"http://公网ip/api/"' }

2.打包
npm run build

生成:
java|码神之路博客项目部署
文章图片

3.上传
将static文件夹和index.html文件上传到html目录里
九、编排启动 使用cd命令进入docker-compose目录
在里面执行启动命令,建议先不后台启动,观察是否有启动失败容器
docker-compose up#构建(重新构建)并启动容器 docker-compose up -d #-d 表示后台构建 docker-compose down#停止并删除容器docker-compose start #启动已有容器 docker-compose stop#停止运行的容器

然后访问
博客网址:http://公网ip/
后台管理系统网址:http://公网ip:8889/login.html

    推荐阅读