springBoot打包的时候代码和jar包打包在同一个jar包里面,会导致jar包非常庞大,在不能连接内网的时候调试代码,每次只改动了java代码就需要把所有的jar包一起上传,导致传输文件浪费了很多时间,所以如果打包的时候只把写成的代码打包,已经上传服务器的jar包不用修改,这样每次上传文件将会大大节省时间,接下来描述一下单独打jar包的过程。
更改springBoot打jar包的插件即可改为一下格式:
org.springframework.boot spring-boot-maven-plugin ZIP non-exists non-exists repackage classes false
如图:
2、那么导入的jar包又在哪里呢?没关系,我们还有另一个插件。如下:
org.apache.maven.plugins maven-dependency-plugin copy-dependencies package copy-dependencies target/lib false false runtime
当你打包的时候自动去生成,如下:
jar包:
这样,就可以把项目中使用到的所有jar包提取出来。
项目在大jar包的时候也会把配置文件和页面一起打包,导致每次都要替换或者更改配置文件,导致非常繁琐。
我们可以在打包插件中指定不用打包的内容,如下:
org.apache.maven.plugins maven-jar-plugin **/*.properties **/*.xml **/*.yml static/** templates/** **/*.xlsx
Spring Boot 加载配置文件的方式以及步骤
springboot 默认加载配置文件有三个位置1、a 加载第二个是 通jar包同级 config目录 下的 一系列配置文件 xxx.yaml2、b 加载第一个是 同jar包同级的 一系列配置文件xxx.yaml3、c 加载第三个是 自身jar包的 配置文件。加载顺序 为:a b c
测试实例 :
a: server.port = 8082b: server.port = 8081c: server.port = 8080
结果如下:
web-discovery-test org.springframework.boot spring-boot-maven-plugin ZIP non-exists non-exists repackage classes false org.apache.maven.plugins maven-dependency-plugin copy-dependencies package copy-dependencies target/lib false false runtime org.apache.maven.plugins maven-jar-plugin **/*.properties **/*.xml **/*.yml static/** templates/** **/*.xlsx
java -jar -Dloader.path=lib -jar web-discovery-test.jar java -Dloader.path=lib -jar *.jar
制作镜像步骤
## java:8-alpine(145) java8:latest (500m)FROM java:8-alpine# 维者信息MAINTAINER fageRUN mkdir -p /work /work/config /work/libs /work/logs /work/fileEXPOSE 8080ADD empty.jar /work/web-discovery-test.jar WORKDIR /workCMD ["java","-Dloader.path=/work/libs","-jar","/work/web-discovery-test.jar"]
比如:empty.jar
运行命令
docker run -d -p 8080:8080 --name web-discovery-test -v /fage/web-discovery-test/web-discovery-test.jar://work/web-discovery-test.jar -v /fage/web-discovery-test/logs:/work/logs -v /fage/web-discovery-test/libs:/work/libs -v /fage/web-discovery-test/file:/work/file -v /fage/web-discovery-test/config:/work/config web-discovery-test:v2
docker build -t 镜像名:版本号标签 .## 实例:docker build -t web-discovery-test:v1 .
[root@localhost docker]#[root@localhost docker]# docker build -t web-discovery-test:v2 .Sending build context to Docker daemon 2.56kBStep 1/6 : FROM java:8-alpine8-alpine: Pulling from library/java709515475419: Pull complete38a1c0aaa6fd: Pull complete5b58c996e33e: Pull completeDigest: sha256:d49bf8c44670834d3dade17f8b84d709e7db47f1887f671a0e098bafa9bae49fStatus: Downloaded newer image for java:8-alpine ---> 3fd9dd82815cStep 2/6 : RUN mkdir -p /work /work/config /work/libs /work/logs /work/file ---> Running in 4833f98cc891Removing intermediate container 4833f98cc891 ---> 8398b01d5158Step 3/6 : EXPOSE 8080 ---> Running in 6b43e5e32ef7Removing intermediate container 6b43e5e32ef7 ---> 5bde587635f3Step 4/6 : ADD empty.jar /work/web-discovery-test.jar ---> beed469c2bfeStep 5/6 : WORKDIR /work ---> Running in d953acaefdc0Removing intermediate container d953acaefdc0 ---> 801c71d84e45Step 6/6 : CMD ["java","-Dloader.path=/work/libs","-jar","/work/web-discovery-test.jar"] ---> Running in af7a7d89f55bRemoving intermediate container af7a7d89f55b ---> 2f03a2dfcb94Successfully built 2f03a2dfcb94Successfully tagged web-discovery-test:v2[root@localhost docker]#[root@localhost docker]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEweb-discovery-test v2 2f03a2dfcb94 4 seconds ago 145MB
##进入容器
docker exec -it 容器名 /bin/bash
##查询 xx 相关的镜像
docker search xx
##下载镜像到本地
docker pull 镜像名 (可定义名称 xx:xx)
##查看本地镜像
docker images
##查看正在运行镜像
docker ps (-a 所有启动过的,包括不运行的)
##将容器制作成镜像(内部自定义了一些配置等)
docker commit -m '镜像描述' -a '制作者' 容器名 镜像名
##将制作好的镜像打成 tar 包
docker save -o tar包的名字 镜像名
##怎么使用 tar 包
docker load < tar 包所在路径
RUN:执行命令并创建新的镜像层;
CMD:设置容器启动后默认执行的命令即参数,但cmd能被docker run后面的命令行参数替换;
ENTRYPOINT:配置容器启动时运行的命令。
ENTRYPOINT的Exec格式用于设置要执行的命令及其参数,同时可以通过CMD提供额外的参数。ENTRYPOINT中的参数始终会被用到,而CMD的额外参数可以再容器启动时动态替换。
ENTRYPOINT指令可以让容器以应用程序或者服务的形式运行。和CMD不同的是,ENTRYPOINT不会被忽略,一定会被执行,即使运行docker run时指定了其他命令。
修改镜像名称
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
pujh/centos tomcat-centos 70ff7873d7cd About an hour ago 612 MB
docker.io/centos latest 9f38484d220f 11 days ago 202 MB
[root@localhost ~]# docker tag 70ff7873d7cd my_centos:tomcat-centos
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my_centos tomcat-centos 70ff7873d7cd About an hour ago 612 MB
pujh/centos tomcat-centos 70ff7873d7cd About an hour ago 612 MB
docker.io/centos latest 9f38484d220f 11 days ago 202 MB
[root@localhost ~]# docker rmi 70ff7873d7cd
Error response from daemon: conflict: unable to delete 70ff7873d7cd (cannot be forced) - image is being used by running container 70859e710147
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
70859e710147 70ff "/bin/sh -c '/root..." About an hour ago Up About an hour 0.0.0.0:8090->8080/tcp dazzling_hopper
[root@localhost ~]# docker stop 70859e710147
[root@localhost ~]# docker rm 70859e710147
[root@localhost ~]# docker rmi 70ff7873d7cd
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my_centos tomcat-centos 70ff7873d7cd About an hour ago 612 MB
docker.io/centos latest 9f38484d220f 11 days ago 202 MB
### IMAGE ID 一样,无法删除
[root@localhost /]# docker rmi 00bc163fa009
Error response from daemon: conflict: unable to delete 00bc163fa009 (must be forced) - image is referenced in multiple repositories
[root@localhost /]# docker rmi williamyeh/java8:latest
Untagged: williamyeh/java8:latest
Untagged: williamyeh/java8@sha256:174d528516a0eae5c4df69966eeb5e51d7c0dc1a532249af61013953eab1d9f3
Spring Boot 默认使用logback日志。
开发与生产配置隔离,分开配置
application.properties
## 开发环境 dev /生产环境 productspring.profiles.active=dev
application-dev.yaml
## 日志配置 配置绝对路径,不要使用相对路径logging: file: path: D:\logs\dev## 其他配置 server: port: 8084
application-product.yaml
## 日志配置 配置绝对路径,不要使用相对路径logging: file: path: D:\logs\product## 其他配置 server: port: 80
logback-spring.xml配置
一定要加 -spring ,表示在spring配置被加载之后才执行,当前的logback配置,
converter/> converter/> value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>System.outdebug${CONSOLE_LOG_PATTERN}UTF-8${FILE_LOG_PATTERN}UTF-8${logPath}/debug/log-debug-%d{yyyy-MM-dd}.%i.log100MB15debug${FILE_LOG_PATTERN}UTF-8${logPath}/info/log-info-%d{yyyy-MM-dd}.%i.log100MB15infoACCEPTDENY${FILE_LOG_PATTERN}UTF-8${logPath}/error/log-error-%d{yyyy-MM-dd}.%i.log100MB15ERROR
-r 就是向下递归,不管有多少级目录,一并删除-f 就是直接强行删除,不作任何提示的意思
删除文件夹实例:
rm -rf /var/log/httpd/access
将会删除/var/log/httpd/access目录以及其下所有文件、文件夹
删除文件使用实例:
rm -f /var/log/httpd/access.log
将会强制删除/var/log/httpd/access.log这个文件
docker build -t web-discovery-test:v1 .
可以发现,这个jar是非常的小
运行命令
docker run -d -p 8080:8080 --name web-discovery-test -v /fage/web-discovery-test/web-discovery-test.jar://work/web-discovery-test.jar -v /fage/web-discovery-test/logs:/work/logs -v /fage/web-discovery-test/libs:/work/libs -v /fage/web-discovery-test/file:/work/file -v /fage/web-discovery-test/config:/work/config web-discovery-test:v2
运行结果查看
日志打印
[root@localhost docker]# docker logs -f b2b3eaee4b81 . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.2.RELEASE)2020-08-06 14:14:17.080 INFO 1 --- [ main] com.fage.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT on b2b3eaee4b81 with PID 1 (/work/web-discovery-test.jar started by root in /work)2020-08-06 14:14:17.084 INFO 1 --- [ main] com.fage.demo.DemoApplication : The following profiles are active: product2020-08-06 14:14:18.863 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)2020-08-06 14:14:18.880 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2020-08-06 14:14:18.892 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]2020-08-06 14:14:19.026 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2020-08-06 14:14:19.026 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1835 ms2020-08-06 14:14:19.892 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'2020-08-06 14:14:20.374 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''2020-08-06 14:14:20.403 INFO 1 --- [ main] com.fage.demo.DemoApplication : Started DemoApplication in 4.073 seconds (JVM running for 4.615)2020-08-06 14:15:04.855 INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'2020-08-06 14:15:04.855 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'2020-08-06 14:15:04.871 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 16 ms2020-08-06 14:15:04.928 INFO 1 --- [nio-8080-exec-1] com.fage.demo.DemoApplication : file.isDirectory() = true2020-08-06 14:15:35.456 INFO 1 --- [nio-8080-exec-4] com.fage.demo.DemoApplication : hello world2020-08-06 14:15:35.457 ERROR 1 --- [nio-8080-exec-4] com.fage.demo.DemoApplication : error2020-08-06 14:16:12.006 INFO 1 --- [nio-8080-exec-6] com.fage.demo.DemoApplication : hello world2020-08-06 14:16:12.012 ERROR 1 --- [nio-8080-exec-6] com.fage.demo.DemoApplication : error2020-08-06 14:16:15.305 INFO 1 --- [nio-8080-exec-7] com.fage.demo.DemoApplication : file.isDirectory() = true2020-08-06 14:16:15.306 INFO 1 --- [nio-8080-exec-7] com.fage.demo.DemoApplication : 文件夹: /work/file/mk2020-08-06 14:16:15.306 INFO 1 --- [nio-8080-exec-7] com.fage.demo.DemoApplication : 文件: /work/file/aaa
验证 是否映射成功?调用接口:http://192.168.71.134:8080/files和http://192.168.71.134:8080/hello
日志打印:
2020-08-06 14:16:15.305 INFO 1 --- [nio-8080-exec-7] com.fage.demo.DemoApplication : file.isDirectory() = true2020-08-06 14:16:15.306 INFO 1 --- [nio-8080-exec-7] com.fage.demo.DemoApplication : 文件夹: /work/file/mk2020-08-06 14:16:15.306 INFO 1 --- [nio-8080-exec-7] com.fage.demo.DemoApplication : 文件: /work/file/aaa
代码分享
微信公众号 点击关于我,加入QQ群,即可获取到代码