這一節(jié)我們介紹 Spring Cloud 組件的容器化,主要包括 eureka-server 、 gateway-server 和 provider-server 。 1. Docker 化配置這里我們根據(jù) dockerfile-maven-plugin 這個 maven 插件來構(gòu)建,可以用來構(gòu)建 docker 鏡像的 maven 插件有很多,其中使用比較多的包括 docker-maven-plugin ,這兩個插件是同一個人在 Github 上開源的, docker-maven-plugin 被作者標記為不活躍的,并且建議大家使用 dockerfile-maven-plugin ,作者可能是覺得 docker-maven-plugin 在使用的方式上并不是那么的優(yōu)雅,才又新寫了 dockerfile-maven-plugin .(了解源碼可+求求: 1791743380) 本篇文章我們將介紹如何在 IDE 工具 idea 中使用 Docker 工具,并為我們的工程構(gòu)建鏡像。 2. 實戰(zhàn)2.1 創(chuàng)建父工程 chapter18父工程 pom.xml 依賴文件: 代碼清單:chapter18/pom.xml <build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>${dockerfile.maven.version}</version> <executions> <execution> <id>default</id> <goals> <goal>build</goal> <goal>push</goal> </goals> </execution> </executions> <configuration> <repository>${docker.image.prefix}/${project.artifactId}</repository> <tag>${project.version}</tag> <buildArgs> <JAR_FILE>${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration> </plugin> </plugins></build> 這里筆者僅截取了 dockerfile-maven-plugin 相關(guān)的代碼.
2.2 創(chuàng)建子工程 eureka-server2.2.1 子工程依賴 pom.xml 如下:代碼清單:chapter18/eureka_server/pom.xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> </plugin> </plugins></build> 由于在父工程中已經(jīng)詳細配置相關(guān)內(nèi)容,這里僅需要將 2.2.2 子工程Dockerfile 如下:代碼清單:chapter18/eureka_server/Dockerfile FROM registry.cn-shanghai.aliyuncs.com/springcloud-book/java:8u221-jreARG JAR_FILEENV PROFILE defaultADD target/${JAR_FILE} /opt/app.jarEXPOSE 8080ENTRYPOINT java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -Duser.timezone=Asia/Shanghai -Dfile.encoding=UTF-8 -Dspring.profiles.active=${PROFILE} -jar /opt/app.jar
2.2.3 子工程配置文件 application.yml 如下:代碼清單:chapter18/eureka_server/src/main/resources/application.yml server: port: 8080spring: application: name: spring-cloud-eureka-server cloud: inetutils: use-only-site-local-interfaces: trueeureka: instance: prefer-ip-address: true lease-expiration-duration-in-seconds: 90 lease-renewal-interval-in-seconds: 30 server: enable-self-preservation: false eviction-interval-timer-in-ms: 60000 client: register-with-eureka: false service-url: defaultZone: http://${EUREKA_SERVER_HOST}:${EUREKA_SERVER_PORT}/eureka/management: endpoints: web: exposure: include: '*'
2.2.4 子工程構(gòu)建 Docker 鏡像其余部分代碼筆者這里就不列出了,有需要的朋友請參考 Github 倉庫,接下來嘗試打包鏡像,這里我們直接使用 idea 的 package 命令,也可以在工程的跟目錄下執(zhí)行命令: mvn clean package 執(zhí)行以上命令需本地配置 maven 環(huán)境變量,否則會報錯 mvn 命令找不到。筆者使用 idea 提供的 package 命令,具體操作如圖: 直接雙擊,可以看到控制臺開始執(zhí)行打包命令,在打包完成后會將 [INFO] Image will be built as registry.cn-shanghai.aliyuncs.com/springcloud-book/eureka_server:0.0.1-SNAPSHOT[INFO] [INFO] Step 1/6 : FROM registry.cn-shanghai.aliyuncs.com/springcloud-book/java:8u221-jre[INFO] [INFO] Pulling from springcloud-book/java[INFO] Image 9d48c3bd43c5: Already exists[INFO] Image 828e49a80267: Already exists[INFO] Image 00b99dcc31ec: Already exists[INFO] Image e6e45bfebaec: Already exists[INFO] Digest: sha256:32d9a2557298d7b56997ded086baf043145ca178b71f6c8f47e76f837614bda0[INFO] Status: Downloaded newer image for registry.cn-shanghai.aliyuncs.com/springcloud-book/java:8u221-jre[INFO] ---> 5f59ebcc4b59[INFO] Step 2/6 : ARG JAR_FILE[INFO] [INFO] ---> Running in 8e3e51705681[INFO] Removing intermediate container 8e3e51705681[INFO] ---> 0bcb834dc28b[INFO] Step 3/6 : ENV PROFILE default[INFO] [INFO] ---> Running in 4a8e840e7a95[INFO] Removing intermediate container 4a8e840e7a95[INFO] ---> 11f97aef63ee[INFO] Step 4/6 : ADD target/${JAR_FILE} /opt/app.jar[INFO] [INFO] ---> 61c0ff0c5b6e[INFO] Step 5/6 : EXPOSE 8080[INFO] [INFO] ---> Running in 264bf0d042b6[INFO] Removing intermediate container 264bf0d042b6[INFO] ---> d951ab655bd5[INFO] Step 6/6 : ENTRYPOINT java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -Duser.timezone=Asia/Shanghai -Dfile.encoding=UTF-8 -Dspring.profiles.active=${PROFILE} -jar /opt/app.jar[INFO] [INFO] ---> Running in 4bd69be29edc[INFO] Removing intermediate container 4bd69be29edc[INFO] ---> 704768c9dd75[INFO] Successfully built 704768c9dd75[INFO] Successfully tagged registry.cn-shanghai.aliyuncs.com/springcloud-book/eureka_server:0.0.1-SNAPSHOT[INFO] [INFO] Detected build of image with id 704768c9dd75[INFO] Building jar: D:\Development\SpringCloudLearning\chapter18\eureka_server\target\eureka_server-0.0.1-SNAPSHOT-docker-info.jar[INFO] Successfully built registry.cn-shanghai.aliyuncs.com/springcloud-book/eureka_server:0.0.1-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 01:38 min[INFO] Finished at: 2019-09-21T20:05:50+08:00[INFO] ------------------------------------------------------------------------ 從以上命令,我們可以看到鏡像每一步的構(gòu)建過程,同樣,這個鏡像筆者這里 push 到阿里云的鏡像倉庫,有需要的朋友可以直接 pull 后使用。 docker pull registry.cn-shanghai.aliyuncs.com/springcloud-book/eureka_server:0.0.1-SNAPSHOT 2.2.5 子工程測試:測試這里筆者啟動兩個鏡像,構(gòu)建兩個 eureka_server 服務,對外映射分別使用不同的端口, 8761 和 8762 ,命令如下: docker run -p 8761:8080 --rm \-e JAVA_OPTS='-server -Xmx1g' \-e PROFILE='default' \-e EUREKA_SERVER_HOST=192.168.0.128 \-e EUREKA_SERVER_PORT=8762 \registry.cn-shanghai.aliyuncs.com/springcloud-book/eureka_server:0.0.1-SNAPSHOT docker run -p 8762:8080 --rm \-e JAVA_OPTS='-server -Xmx1g' \-e PROFILE='default' \-e EUREKA_SERVER_HOST=192.168.0.128 \-e EUREKA_SERVER_PORT=8761 \registry.cn-shanghai.aliyuncs.com/springcloud-book/eureka_server:0.0.1-SNAPSHOT 啟動成功后打開瀏覽器分別訪問路徑:http://localhost:8761/ 、 http://localhost:8762/ ,結(jié)果如圖: 3. 小結(jié)還有其余兩個工程 provider_server 和 gateway_server 筆者這里就不做演示了. |
|
來自: 新用戶32269360 > 《待分類》