歡迎訪問我的GitHub

https://github.com/zq2599/blog_demos

內容:所有原創文章分類彙總及配套原始碼,涉及Java、Docker、Kubernetes、DevOPS等;

關於Spring Native

  • Spring官方部落格於2021年03月11日宣佈Spring Native的beta版本釋出,藉助Spring Native可以將spring應用與GraalVM整合到native image中;

  • native image是GraalVM的一項技術,會將java應用的位元組碼編譯成可執行檔案,還會與JDK的本地庫做靜態連結,執行應用時無需Java虛擬機器,自身已集成了記憶體管理,執行緒排程等能力,更多資訊請參考:https://www.graalvm.org/reference-manual/native-image/

  • 本文以實戰為主,因此不會用太多篇幅介紹Spring Native的理論和優勢,這裡簡單小結幾個重要特性:

  1. 應用啟動速度不超過100毫秒;
  2. 啟動即達到效能峰值(C1、C2等手段已經用不上了)
  3. 執行時更低的記憶體消耗;
  4. docker映象不含JDK(所需檔案已經抽取出來放入映象),官方展示的含有Spring Boot, Spring MVC, Jackson, Tomcat的映象大小是50M;
  5. 為了達到前面的效果,代價是構建時間更長;

Spring Native到底是什麼

個人的理解:Spring Native是Spring提供的、製作native image的技術方案,涉及到以下關鍵技術:

  1. Spring ahead-of-time (AOT) 外掛,對spring應用做AOT處理,使得傳統虛擬機器的class lazy loading在不復存在;
  2. spring-boot-maven-plugin外掛在構建docker映象的時候,使用了名為dmikusa/graalvm-tiny的映象作為構建工具,這個工具負責將當前工程的構建結果和GraalVM整合在一起,最終制作成native image;

本篇概覽

作為實戰風格的文章,本篇主要內容是開發springboot應用再構建為native image,然後驗證其功能和效果,本文由以下內容構成:

  1. 環境資訊
  2. 新建名為spring-native-tutorials的maven父工程,對實戰用到的依賴庫、外掛等做統一配置;
  3. 新建名為webmvc的maven子工程,這是個springboot應用;
  4. 將webmvc構建為native image,這是個docker映象;
  5. 在docker中啟動映象,驗證是否可用,並檢查相關相關指標;

環境資訊

本次實戰相關的環境資訊如下:

  1. 電腦:MacBook pro 13寸 2018
  2. 作業系統:macOS Big Sur 11.2.3
  3. IDE:IntelliJ IDEA 2018.3.5 (Ultimate Edition)
  4. docker:20.10.5
  5. JDK:1.8.0_211
  6. maven:3.6.0
  7. springboot:2.5.0-SNAPSHOT
  8. spring-aot-maven-plugin:0.10.0-SNAPSHOT

原始碼下載

名稱 連結 備註
專案主頁 https://github.com/zq2599/blog_demos 該專案在GitHub上的主頁
git倉庫地址(https) https://github.com/zq2599/blog_demos.git 該專案原始碼的倉庫地址,https協議
git倉庫地址(ssh) git@github.com:zq2599/blog_demos.git 該專案原始碼的倉庫地址,ssh協議
  • 這個git專案中有多個資料夾,本次實戰的原始碼在spring-native-tutorials資料夾下,如下圖紅框所示:

新建名為spring-native-tutorials的maven父工程

  • 對Spring Native的學習不是寫出helloworld就完事,因此這裡先建立一個父工程,為今後所有的應用提供統一的依賴庫、外掛管理;

  • 新建名為spring-native-tutorials的maven父工程,pom.xml內容如下,有幾處要注意的地方稍後提到:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <modules>
  7. <module>webmvc</module>
  8. </modules>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>2.5.0-SNAPSHOT</version>
  13. <relativePath/>
  14. </parent>
  15. <groupId>com.bolingcavalry</groupId>
  16. <artifactId>spring-native-tutorials</artifactId>
  17. <version>1.0-SNAPSHOT</version>
  18. <packaging>pom</packaging>
  19. <properties>
  20. <java.version>1.8</java.version>
  21. <!-- springboot生成jar檔案的檔名字尾,用來避免Spring Boot repackaging和native-image-maven-plugin外掛之間可能存在的衝突 -->
  22. <classifier/>
  23. <!-- 構建映象時的定製引數 -->
  24. <native.build.args/>
  25. <!-- 指定使用dmikusa/graalvm-tiny這個映象作為構建工具,來構建映象 -->
  26. <builder>dmikusa/graalvm-tiny</builder>
  27. <!-- spring cloud版本 -->
  28. <spring-cloud.version>2020.0.2</spring-cloud.version>
  29. </properties>
  30. <!-- 外掛管理 -->
  31. <pluginRepositories>
  32. <pluginRepository>
  33. <id>spring-release</id>
  34. <name>Spring release</name>
  35. <url>https://repo.spring.io/release</url>
  36. <snapshots>
  37. <enabled>false</enabled>
  38. </snapshots>
  39. </pluginRepository>
  40. <pluginRepository>
  41. <id>spring-milestone</id>
  42. <name>Spring milestone</name>
  43. <url>https://repo.spring.io/milestone</url>
  44. <snapshots>
  45. <enabled>false</enabled>
  46. </snapshots>
  47. </pluginRepository>
  48. <pluginRepository>
  49. <id>spring-snapshot</id>
  50. <name>Spring Snapshots</name>
  51. <url>https://repo.spring.io/snapshot</url>
  52. <releases>
  53. <enabled>false</enabled>
  54. </releases>
  55. </pluginRepository>
  56. </pluginRepositories>
  57. <!--倉庫管理-->
  58. <repositories>
  59. <repository>
  60. <id>spring-release</id>
  61. <name>Spring release</name>
  62. <url>https://repo.spring.io/release</url>
  63. <snapshots>
  64. <enabled>false</enabled>
  65. </snapshots>
  66. </repository>
  67. <repository>
  68. <id>spring-milestone</id>
  69. <name>Spring milestone</name>
  70. <url>https://repo.spring.io/milestone</url>
  71. <snapshots>
  72. <enabled>false</enabled>
  73. </snapshots>
  74. </repository>
  75. <repository>
  76. <id>spring-snapshot</id>
  77. <name>Spring Snapshots</name>
  78. <url>https://repo.spring.io/snapshot</url>
  79. <releases>
  80. <enabled>false</enabled>
  81. </releases>
  82. </repository>
  83. </repositories>
  84. <!--依賴包版本管理-->
  85. <dependencyManagement>
  86. <dependencies>
  87. <dependency>
  88. <groupId>org.springframework.experimental</groupId>
  89. <artifactId>spring-native</artifactId>
  90. <version>0.10.0-SNAPSHOT</version>
  91. </dependency>
  92. <dependency>
  93. <groupId>org.springframework.cloud</groupId>
  94. <artifactId>spring-cloud-dependencies</artifactId>
  95. <version>${spring-cloud.version}</version>
  96. <type>pom</type>
  97. <scope>import</scope>
  98. </dependency>
  99. </dependencies>
  100. </dependencyManagement>
  101. <!--外掛配置-->
  102. <build>
  103. <pluginManagement>
  104. <plugins>
  105. <plugin>
  106. <groupId>org.springframework.boot</groupId>
  107. <artifactId>spring-boot-maven-plugin</artifactId>
  108. <configuration>
  109. <classifier>${classifier}</classifier>
  110. <image>
  111. <builder>${builder}</builder>
  112. <env>
  113. <BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
  114. <BP_NATIVE_IMAGE_BUILD_ARGUMENTS>${native.build.args}</BP_NATIVE_IMAGE_BUILD_ARGUMENTS>
  115. </env>
  116. <!--執行構建任務的映象,如果在當前環境不存在才會遠端下載-->
  117. <pullPolicy>IF_NOT_PRESENT</pullPolicy>
  118. </image>
  119. </configuration>
  120. </plugin>
  121. <!-- aot外掛,ahead-of-time transformations -->
  122. <plugin>
  123. <groupId>org.springframework.experimental</groupId>
  124. <artifactId>spring-aot-maven-plugin</artifactId>
  125. <version>0.10.0-SNAPSHOT</version>
  126. <executions>
  127. <execution>
  128. <id>test-generate</id>
  129. <goals>
  130. <goal>test-generate</goal>
  131. </goals>
  132. </execution>
  133. <execution>
  134. <id>generate</id>
  135. <goals>
  136. <goal>generate</goal>
  137. </goals>
  138. </execution>
  139. </executions>
  140. </plugin>
  141. </plugins>
  142. </pluginManagement>
  143. </build>
  144. </project>
  • 上述pom.xml有以下幾處需要注意:
  1. 外掛倉庫、依賴庫倉庫、依賴庫版本的配置都集中在這裡;
  2. 配置好spring-aot-maven-plugin和spring-boot-maven-plugin這兩個外掛,子工程會用到;
  3. spring-boot-maven-plugin外掛製作docker映象的時候,又會用到dmikusa/graalvm-tiny映象,這才是真正構建native image的工具;

新建springboot型別的maven子工程

  • 新建名為webmvc的子工程,pom.xml內容如下,可見內容很簡單,就是常規依賴庫和父工程配置的兩個外掛,一個負責執行AOT,一個負責構建映象:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring-native-tutorials</artifactId>
  7. <groupId>com.bolingcavalry</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>webmvc</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.experimental</groupId>
  15. <artifactId>spring-native</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. <exclusions>
  21. <exclusion>
  22. <groupId>org.apache.tomcat.embed</groupId>
  23. <artifactId>tomcat-embed-core</artifactId>
  24. </exclusion>
  25. <exclusion>
  26. <groupId>org.apache.tomcat.embed</groupId>
  27. <artifactId>tomcat-embed-websocket</artifactId>
  28. </exclusion>
  29. </exclusions>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.apache.tomcat.experimental</groupId>
  33. <artifactId>tomcat-embed-programmatic</artifactId>
  34. <version>${tomcat.version}</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-test</artifactId>
  39. <scope>test</scope>
  40. </dependency>
  41. </dependencies>
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.experimental</groupId>
  46. <artifactId>spring-aot-maven-plugin</artifactId>
  47. <configuration>
  48. <removeSpelSupport>true</removeSpelSupport>
  49. </configuration>
  50. </plugin>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. </plugin>
  55. </plugins>
  56. </build>
  57. </project>
  • 程式碼很簡單,一個普通的springboot應用,帶http介面:
  1. package com.bolingcavalry.webmvc;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.http.HttpStatus;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.ResponseStatus;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import java.time.LocalDateTime;
  9. @SpringBootApplication
  10. @RestController
  11. public class WebmvcApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(WebmvcApplication.class, args);
  14. }
  15. @ResponseStatus(HttpStatus.ACCEPTED)
  16. @GetMapping("/status")
  17. public String status() {
  18. return "status";
  19. }
  20. @GetMapping("/")
  21. public String hello() {
  22. return "1. Hello from Spring MVC and Tomcat, " + LocalDateTime.now();
  23. }
  24. }
  • 現在編碼已完成,來構建docker映象吧,進入父工程的pom.xml所在目錄,執行以下命令:
  1. mvn clean -U -DskipTests spring-boot:build-image
  • 構建成功後輸出資訊如下(篇幅所限僅擷取最後一小段),耗時4分25秒,期間筆記本風扇狂轉:
  1. ...
  2. [INFO] Successfully built image 'docker.io/library/webmvc:1.0-SNAPSHOT'
  3. [INFO]
  4. [INFO] ------------------------------------------------------------------------
  5. [INFO] Reactor Summary for spring-native-tutorials 1.0-SNAPSHOT:
  6. [INFO]
  7. [INFO] spring-native-tutorials ............................ SUCCESS [ 1.786 s]
  8. [INFO] webmvc ............................................. SUCCESS [04:19 min]
  9. [INFO] ------------------------------------------------------------------------
  10. [INFO] BUILD SUCCESS
  11. [INFO] ------------------------------------------------------------------------
  12. [INFO] Total time: 04:25 min
  13. [INFO] Finished at: 2021-05-22T16:36:44+08:00
  14. [INFO] ------------------------------------------------------------------------
  15. [WARNING] The requested profile "nexus" could not be activated because it does not exist.
  • 執行docker images命令,如下圖,可見映象已經生成:

  • 檢視映象構成,可見每個layer都不大,共計七十多M:
  1. (base) zhaoqindeMBP:~ zhaoqin$ docker history webmvc:1.0-SNAPSHOT
  2. IMAGE CREATED CREATED BY SIZE COMMENT
  3. b8ff54813ae0 41 years ago 69B
  4. <missing> 41 years ago 452kB
  5. <missing> 41 years ago 2.51MB
  6. <missing> 41 years ago 57.2MB
  7. <missing> 41 years ago 1.4MB
  8. <missing> 41 years ago 268B
  9. <missing> 41 years ago 17.3MB
  • 映象構建成功,可以驗證基本功能了;

驗證

  • 執行以下命令,建立一個臨時容器(控制檯結束後容器會被清理掉):
  1. docker run --rm -p 8080:8080 webmvc:1.0-SNAPSHOT
  • 控制檯輸出如下,79毫秒啟動完成,真是一眨間的功夫:
  1. (base) zhaoqindeMBP:~ zhaoqin$ docker run --rm -p 8080:8080 webmvc:1.0-SNAPSHOT
  2. 2021-05-22 09:34:57.578 INFO 1 --- [ main] o.s.nativex.NativeListener : This application is bootstrapped with code generated with Spring AOT
  3. . ____ _ __ _ _
  4. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  5. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  6. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  7. ' |____| .__|_| |_|_| |_\__, | / / / /
  8. =========|_|==============|___/=/_/_/_/
  9. :: Spring Boot :: (v2.5.0-SNAPSHOT)
  10. 2021-05-22 09:34:57.586 INFO 1 --- [ main] c.b.webmvc.WebmvcApplication : Starting WebmvcApplication using Java 1.8.0_292 on 3529ec458896 with PID 1 (/workspace/com.bolingcavalry.webmvc.WebmvcApplication started by cnb in /workspace)
  11. 2021-05-22 09:34:57.586 INFO 1 --- [ main] c.b.webmvc.WebmvcApplication : No active profile set, falling back to default profiles: default
  12. 2021-05-22 09:34:57.661 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
  13. May 22, 2021 9:34:57 AM org.apache.coyote.AbstractProtocol init
  14. INFO: Initializing ProtocolHandler ["http-nio-8080"]
  15. May 22, 2021 9:34:57 AM org.apache.catalina.core.StandardService startInternal
  16. INFO: Starting service [Tomcat]
  17. May 22, 2021 9:34:57 AM org.apache.catalina.core.StandardEngine startInternal
  18. INFO: Starting Servlet engine: [Apache Tomcat/9.0.46]
  19. May 22, 2021 9:34:57 AM org.apache.catalina.core.ApplicationContext log
  20. INFO: Initializing Spring embedded WebApplicationContext
  21. 2021-05-22 09:34:57.669 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 79 ms
  22. May 22, 2021 9:34:57 AM org.apache.coyote.AbstractProtocol start
  23. INFO: Starting ProtocolHandler ["http-nio-8080"]
  24. 2021-05-22 09:34:57.713 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
  25. 2021-05-22 09:34:57.713 INFO 1 --- [ main] c.b.webmvc.WebmvcApplication : Started WebmvcApplication in 0.178 seconds (JVM running for 0.19)
  26. 2021-05-22 09:34:57.713 INFO 1 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state LivenessState changed to CORRECT
  27. 2021-05-22 09:34:57.714 INFO 1 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
  • 瀏覽器訪問本機8080埠,如下圖,應用基本功能正常:

  • 再看看資源使用情況,命令是docker stats,如下可見,記憶體僅用了30M:
  1. CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
  2. 6ce6c66fb4de jovial_hertz 0.11% 30.69MiB / 3.844GiB 0.78% 1.49kB / 158B 4.31MB / 0B 18
  • 我曾經在hub.docker.com上放了一個傳統springboot應用製作的映象bolingcavalry/hellojib:0.0.1-SNAPSHOT,現在拿來和Spring Native映象對比一下,啟動資訊如下,耗時2036毫秒:
  1. (base) zhaoqindeMacBook-Pro:~ zhaoqin$ docker run --rm -P docker.io/bolingcavalry/hellojib:0.0.1-SNAPSHOT
  2. . ____ _ __ _ _
  3. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  4. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  5. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  6. ' |____| .__|_| |_|_| |_\__, | / / / /
  7. =========|_|==============|___/=/_/_/_/
  8. :: Spring Boot :: (v2.1.6.RELEASE)
  9. 2021-05-22 11:13:28.121 INFO 1 --- [ main] c.b.hellojib.HellojibApplication : Starting HellojibApplication on ffb32e5b68b9 with PID 1 (/app/classes started by root in /)
  10. 2021-05-22 11:13:28.128 INFO 1 --- [ main] c.b.hellojib.HellojibApplication : No active profile set, falling back to default profiles: default
  11. 2021-05-22 11:13:30.000 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
  12. 2021-05-22 11:13:30.054 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  13. 2021-05-22 11:13:30.054 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21]
  14. 2021-05-22 11:13:30.241 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  15. 2021-05-22 11:13:30.241 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2036 ms
  16. 2021-05-22 11:13:30.715 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
  17. 2021-05-22 11:13:31.103 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
  18. 2021-05-22 11:13:31.110 INFO 1 --- [ main] c.b.hellojib.HellojibApplication : Started HellojibApplication in 3.618 seconds (JVM running for 4.297)
  19. 2021-05-22 11:13:48.866 INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
  20. 2021-05-22 11:13:48.866 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
  21. 2021-05-22 11:13:48.880 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 14 ms
  • 再用docker stats對比記憶體,傳統springboot應用的容器消耗了三百多兆記憶體:
  1. CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
  2. ffb32e5b68b9 eager_williamson 0.64% 356.3MiB / 3.844GiB 9.05% 3.46kB / 2.29kB 0B / 0B 31
  3. 6ce6c66fb4de jovial_hertz 0.11% 30.69MiB / 3.844GiB 0.78% 1.49kB / 158B 4.31MB / 0B 18
  • 綜上所述,Spring Native帶來的優勢是很明顯的,不過請注意:2021年03月11日官方宣佈的Spring Native只是beta版本,請不要用於生產環境!!!

下載外掛失敗

在實際操作過程中,經常會遇到maven外掛或者docker映象下載失敗的情況,除了多試幾次,您還可以考慮將專案放到github上去,藉助github action在雲端完成映象構建,具體操作請參考《用GitHub Actions製作Docker映象》

不用開發,直接體驗

  • 我已將映象上傳到hub.docker.com,完整名稱是bolingcavalry/webmvc:1.0-SNAPSHOT,如果您只想體驗一下native image的效果可以直接下載該映象使用;

你不孤單,欣宸原創一路相伴

  1. Java系列
  2. Spring系列
  3. Docker系列
  4. kubernetes系列
  5. 資料庫+中介軟體系列
  6. DevOps系列

歡迎關注公眾號:程式設計師欣宸

微信搜尋「程式設計師欣宸」,我是欣宸,期待與您一同暢遊Java世界...

https://github.com/zq2599/blog_demos