PringBoot actual combat e-commerce project mall (40K + STAR) Address: github.com/macrozheng/…

Abstract: In order to facilitate the construction of Docker images for SpringBoot applications, we often use Maven plug-ins to package images. There are two types of Maven-plugin from Spotify and Fabric8. Recently SpringBoot 2.4.0 was released, the official plugin also added support for Docker, experience a discovery is also very easy to use, recommend to everyone! For details on how to deploy SpringBoot applications manually? Try this automation plugin! .

  • It is worth noting that when we use plug-ins, we need to define our own mirror build process, such as inpom.xmlTo use the following configuration,<images>The configuration under the TAB is for the image build process.

IO. Fabric8 docker maven – plugin 0.33.0http://192.168.3.101:2375 http://192.168.3.101:5000 192.168.3.101:5000 / mall – tiny /
p r o j e c t . n a m e : {project.name}:
{project.version} java:8 <JAR_FILE>
p r o j e c t . b u i l d . f i n a l N a m e . j a r < / J A R _ F I L E > < / a r g s > < ! Defines which files are copied to the container > < a s s e m b l y > < ! Define the directory to copy to the container > < t a r g e t D i r > / < / t a r g e t D i r > < ! Copy only generated ones j a r package > < d e s c r i p t o r R e f > a r t i f a c t < / d e s c r i p t o r R e f > < / a s s e m b l y > < ! Define container start commands > < e n t r y P o i n t > \ [ j a v a . j a r . / {project.build.finalName}.jar</JAR\_FILE> </args> <! Define which files are copied to the container –> <assembly> <! <targetDir>/</targetDir> <! — Copy generated jar packages only –> < <descriptorRef>artifact</descriptorRef> </assembly> <! <entryPoint>\[” Java “,” -jar”,”/
{project.build.finalName}.jar”] Macrozheng copies code

  • Or define the image build process in Dockerfile.

COPY maven/to the docker container/directory. Maven / Specify that docker container starts with jar package ENTRYPOINT [” Java “, “-jar”,”/mall-tiny-fabric-0.0.1- snapshot. jar”] # Specifies MAINTAINER name MAINTAINER Macrozheng

  • Then reference the Dockerfile file in the plug-in to build the image;

${project.basedir} copies the code

  • In fact, for SpringBoot applications, how to build Docker images from application Jar packages, the approach is basically the same, why have to define their own image construction process?

Official plug-in using SpringBoot official plug-in to solve the above problem, do not need to write their own Docker image build process, directly automatic build, is not very convenient! Let’s see how powerful it can be!

  • Since we need to push the image to the mirror repository, first we install the private mirror repositoryRegistryAnd visual image management toolsdocker-registry-ui, for specific referenceStill deploying the SpringBoot application manually? Try this automation plugin!;

[root@linux-local ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9ffec08467ac Joxit/docker – registry – UI: static “/ bin/sh – c entrypoi…” 2 hours ago Up 2 hours 0.0.0.0:8280->80/ TCP registry- UI a809535ee2A2 Registry :2 “/entrypoint.sh /etc…” 3 months ago Up 7 hours 0.0.0.0:5000->5000/ TCP registry2

  • Then we need to upgrade the version of the application to SpringBoot 2.4.0, the previous version of Docker support does not have this improvement;

Org.springframework. boot spring-boot-starter-parent 2.4.0 Copy code

  • Then changepom.xmlFile to configure the official Maven plug-in, mainly to configure docker-related functions;

org.springframework.boot spring-boot-maven-plugin 192.168.3.101:5000 / mall – tiny /
p r o j e c t . n a m e : {project.name}:
{project.version} true http://192.168.3.101:2375 false test test http://192.168.3.101:5000Copy the code

  • If you are using IDEA, double-click the SpringBoot pluginbuild-imageCommand can be packaged and pushed to the mirror warehouse;

  • You can also package the build image from the command line with the following Maven command.

MVN spring-boot:build-image Copy code

  • Github will download many dependencies from Github. If the network is not good, the download will fail. Try several times.

[INFO] > Pulling builder image ‘docker.io/paketobuild…’ 100% [INFO] > Pulled builder image ‘paketobuildpacks/builder@sha256:9d377230ba8ee74d8619178fd318b1b87a7da1a88bdb198afd14dd7de9e8ea6a’ [INFO] > Pulling run image ‘docker.io/paketobuild…’ 100% [INFO] > Pulled run image ‘paketobuildpacks/run@sha256:33d37fc9ba16e220f071805eaeed881a508ceee5c8909db5710aaed7e97e4fc2’ [INFO] > Executing lifecycle version v0.9.3 [INFO] > Using build cache volume ‘pack-cache-5641f846df6.build’ [INFO] [INFO] > Running creator [INFO] [creator] ===> DETECTING [INFO] [creator] 5 of 18 buildpacks participating [INFO] [creator] paketo-buildpacks/ca-certificates 1.0.1 [INFO] [creator] paketo-buildpacks/bellsoft-liberica 5.2.1 [INFO] [creator] paketo-buildpacks/executable-jar 3.1.3 [INFO] [creator] paketo-buildpacks/dist-zip 2.2.2 [INFO] [creator] paketo-buildpacks/spring-boot 3.5.0 [INFO] [creator] ===> ANALYZING [INFO] [creator] Restoring metadata for “paketo-buildpacks/ca-certificates:helper” from app image [INFO] [creator] Restoring metadata for “paketo-buildpacks/bellsoft-liberica:helper” from app image [INFO] [creator] Restoring metadata for “paketo-buildpacks/bellsoft-liberica:java-security-properties” from app image [INFO] [creator] Restoring metadata for “paketo-buildpacks/bellsoft-liberica:jre” from app image [INFO] [creator] Restoring metadata for “paketo-buildpacks/bellsoft-liberica:jvmkill” from app image [INFO] [creator] Restoring metadata for “paketo-buildpacks/executable-jar:class-path” from app image [INFO] [creator] Restoring metadata for “paketo-buildpacks/spring-boot:helper” from app image [INFO] [creator] Restoring metadata for “paketo-buildpacks/spring-boot:spring-cloud-bindings” from app image [INFO] [creator] Restoring metadata for “paketo-buildpacks/spring-boot:web-application-type” from app image [INFO] [creator] ===> RESTORING [INFO] [creator] ===> BUILDING [INFO] [creator] [INFO] [creator] Paketo CA Certificates Buildpack 1.0.1 [INFO] [creator] github.com/paketo-buil… [INFO] [creator] Launch Helper: Reusing cached layer [INFO] [creator] [INFO] [creator] Paketo BellSoft Liberica Buildpack 5.2.1 [INFO] [creator] github.com/paketo-buil… [INFO] [creator] Build Configuration: [INFO] [creator] BP\_JVM\_VERSION 8.\* the Java version \[INFO\] \[creator\] Launch Configuration: \[INFO\] \[creator\] BPL_JVM_HEAD_ROOM 0 the headroom in memory calculation [INFO] [creator]
B P L _ J V M _ L O A D E D _ C L A S S _ C O U N T 35 BPL\_JVM\_LOADED\_CLASS\_COUNT 35% of classes the number of loaded classes in memory calculation \[INFO\] \[creator\]
BPL_JVM_THREAD_COUNT 250 the number of threads in memory calculation [INFO] [creator] $JAVA_TOOL_OPTIONS the JVM launch flags [INFO] [creator] BellSoft Liberica JRE 8.0.275: Reusing cached layer [INFO] [creator] Launch Helper: Reusing cached layer [INFO] [creator] JVMKill Agent 1.16.0: Reusing cached layer [INFO] [creator] Java Security Properties: Reusing cached layer [INFO] [creator] [INFO] [creator] Paketo Executable JAR Buildpack 3.1.3 [INFO] [creator] github.com/paketo-buil… [INFO] [creator] Process types: [INFO] [creator] executable-jar: java org.springframework.boot.loader.JarLauncher [INFO] [creator] task: java org.springframework.boot.loader.JarLauncher [INFO] [creator] web: java org.springframework.boot.loader.JarLauncher [INFO] [creator] [INFO] [creator] Paketo Spring Boot Buildpack 3.5.0 [INFO] [creator] github.com/paketo-buil… [INFO] [creator] Creating slices from layers index [INFO] [creator] dependencies [INFO] [creator] spring-boot-loader [INFO] [creator] snapshot-dependencies [INFO] [creator] application [INFO] [creator] Launch Helper: Reusing cached layer [INFO] [creator] Web Application Type: Contributing to layer [INFO] [creator] Servlet web application detected [INFO] [creator] Writing env.launch/BPL_JVM_THREAD_COUNT.default [INFO] [creator] Spring Cloud Bindings 1.7.0: Reusing cached layer [INFO] [creator] 4 application slices [INFO] [creator] Image labels: [INFO] [creator] org.opencontainers.image.title [INFO] [creator] org.opencontainers.image.version [INFO] [creator] org.springframework.boot.spring-configuration-metadata.json [INFO] [creator] org.springframework.boot.version [INFO] [creator] ===> EXPORTING [INFO] [creator] Reusing layer ‘paketo-buildpacks/ca-certificates:helper’ [INFO] [creator] Reusing layer ‘paketo-buildpacks/bellsoft-liberica:helper’ [INFO] [creator] Reusing layer ‘paketo-buildpacks/bellsoft-liberica:java-security-properties’ [INFO] [creator] Reusing layer ‘paketo-buildpacks/bellsoft-liberica:jre’ [INFO] [creator] Reusing layer ‘paketo-buildpacks/bellsoft-liberica:jvmkill’ [INFO] [creator] Reusing layer ‘paketo-buildpacks/executable-jar:class-path’ [INFO] [creator] Reusing layer ‘paketo-buildpacks/spring-boot:helper’ [INFO] [creator] Reusing layer ‘paketo-buildpacks/spring-boot:spring-cloud-bindings’ [INFO] [creator] Reusing layer ‘paketo-buildpacks/spring-boot:web-application-type’ [INFO] [creator] Reusing 4/5 app layer(s) [INFO] [creator] Adding 1/5 app layer(s) [INFO] [creator] Reusing layer ‘launcher’ [INFO] [creator] Reusing layer ‘config’ [INFO] [creator] Reusing layer ‘process-types’ [INFO] [creator] Adding label ‘io.buildpacks.lifecycle.metadata’ [INFO] [creator] Adding label ‘io.buildpacks.build.metadata’ [INFO] [creator] Adding label ‘io.buildpacks.project.metadata’ [INFO] [creator] Adding label ‘org.opencontainers.image.title’ [INFO] [creator] Adding label ‘org.opencontainers.image.version’ [INFO] [creator] Adding label ‘org.springframework.boot.spring-configuration-metadata.json’ [INFO] [creator] Adding label ‘org.springframework.boot.version’ [INFO] [creator] Setting default process type ‘web’ [INFO] [creator] *** Images (d5e1771dac7b): [INFO] [creator] 192.168.3.101:5000/mall-tiny/mall-tiny-docker-plugin:0.0.1-SNAPSHOT [INFO] [INFO] Successfully built image ‘192.168.3.101:5000/mall-tiny/mall-tiny-docker-plugin:0.0.1-SNAPSHOT’ [INFO] [INFO] > Pushed image ‘192.168.3.101:5000/mall-tiny/mall-tiny-docker-plugin:0.0.1-SNAPSHOT’ [INFO] ———————————————————————— [INFO] BUILD SUCCESS [INFO] ———————————————————————— [INFO] Total time: 01:06 min [INFO] Finished at: 2020-11-27T15:07:46+08:00 [INFO] Final Memory: 37M/359M [INFO] ———————————————————————— 复制代码

  • After the image is successfully built, you can view our image from the mirror repository:

[root@linux-local ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE paketobuildpacks/run base-cnb a717358311fc 9 Days ago 87.2MB Java 8 d23bdf5b1b1b 3 years ago 634 MB 192.168.3.101:5000/mall-tiny/mall-tiny-docker-plugin 0.0.1 – the SNAPSHOT d5e1771dac7b 40 years line 244 MB pack. Local/builder/fewqajyqsc latest 40 years f15fad05a5ba line 558 MB pack.local/builder/kirivtcqtu latest f15fad05a5ba 40 years ago 558MB paketobuildpacks/builder base 511452064e06 40 years Ago 558MB Copies code

  • We can start withDocker Registry UITo view images in the mirror warehouse, visit:http://192.168.3.101:8280/

  • Then use the following command to start our SpringBoot application:

docker run -p 8080:8080 –name mall-tiny-docker-plugin \ –link mysql:db \ -v /etc/localtime:/etc/localtime \ -v /mydata/app/mall-tiny-docker-plugin/logs:/var/logs \ -d 192.168.3.101:5000/mall-tiny/mall-tiny-docker-plugin:0.0.1-SNAPSHOT replication code

  • Can start after the success, successful visit to Swagger SpringBoot applications page, access to the address: http://192.168.3.101:8080/swagger-ui.html

The official Maven plug-in of SpringBoot avoids the process of writing Docker image construction and makes full use of Jar layering technology after SpringBoot 2.3, but it causes some trouble for the scenes that need to customize the image construction. IO /spring-boot… Project source address github.com/macrozheng/… In this paper, making github.com/macrozheng/… Already included, welcome everyone Star!

MacroZheng Link: juejin.cn/post/690405… The copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.