Spring Boot 3.0 本次带来最大的改动就是 GraalVM 原生镜像的支持,也是官方文档中强调的他们花费时间精力比较多的部分。 GraalVM 技术作为JRE的替代方案,其通过预先编译(Ahead Of Time,AOT)等技术对 Java 应用进行预先编译,让 Spring 在运行应用时掌握更多应用有关的信息,让整个应用启动速度更快。另外,通过编译工具在编译过程中通过消除一些不必要的内容可以让最终的应用更小,占用内存更低。对于一些对启动速度要求非常高的场景,比如 Serverless、FaaS 场景非常友好! 本次 Spring Boot 3.0 直接将其正式从 Spring Native 迁入到 Spring Boot 中来,也预示着该项技术开始逐渐走向成熟,Spring生态开始迈入 GraalVM 阶段!
Spring Boot 3.0本次带来最大的改动就是GraalVM原生镜像的支持,也是官方文档中强调的他们花费时间精力比较多的部分。 GraalVM技术作为JRE的替代方案,其通过预先编译(Ahead Of Time,AOT)等技术对Java应用进行预先编译,让Spring在运行应用时掌握更多应用有关的信息,让整个应用启动速度更快。另外,通过编译工具在编译过程中通过消除一些不必要的内容可以让最终的应用更小,占用内存更低。对于一些对启动速度要求非常高的场景,比如Serverless、FaaS场景非常友好! 提到GraalVM技术,其最早来自于 Spring 团队于2019年发起的 Spring Native 项目。它作为一个实验性质的项目,在过去几年通过与 Spring Boot 3.0 和 Spring Framework 6.0 之前的项目进行整合使用,据 Josh Long 在最近的相关采访透露,该实验项目已经验证了 Spring Boot 2.x 和 Spring Framework 5.x 的各项功能。本次 Spring Boot 3.0 直接将其正式从 Spring Native 迁入到 Spring Boot 中来,也预示着该项技术开始逐渐走向成熟,Spring生态开始迈入 GraalVM 阶段!
2022-12-22T16:28:51.008+08:00 INFO 75439 --- [ main] c.a.cloud.imports.examples.Application : Started Application in 0.653 seconds (process running for 0.662)
One of the biggest changes of Spring Boot 3.0 is the support for GraalVM native images, which the official takes a lot of time and effort. GraalVM technology, as an alternative to JRE, precompels Java applications through technologies such as Ahead Of Time (AOT), which enables Spring to grasp more information about applications when running applications and makes the whole application start faster. In addition, the final application is smaller and has a lower memory footprint by eliminating unnecessary content during the compilation process through the compilation tool. It is excellent for some scenarios that require very high startup speed, such as Serverless and FaaS scenarios. GraalVM technology comes from Spring Native project initiated by Spring team in 2019. It is an experimental project that has been used over the past few years through integration with projects prior to Spring Boot 3.0 and Spring Framework 6.0, according to Josh Long in a recent interview. This experimental project has verified the functions of Spring Boot 2.x and Spring Framework 5.x. This Spring Boot 3.0 directly moves it from Spring Native to Spring Boot, which also indicates that this technology begins to mature gradually and Spring ecology begins to enter the GraalVM stage!
One of the biggest changes of Spring Boot 3.0 is the support for GraalVM native images, which the official takes a lot of time and effort. GraalVM, as an alternative to JRE, precompels java applications through technologies such as Ahead Of Time (AOT), which enables Spring to grasp more information about applications when running applications and makes the whole application start faster. In addition, the final application is smaller and has a lower memory footprint by eliminating unnecessary content during the compilation process through the compilation tool. It is excellent for some scenarios that require very high startup speed, such as Serverless and FaaS scenarios. This Spring Boot 3.0 directly moves it from Spring Native to Spring Boot, which also indicates that this technology begins to mature gradually and Spring ecology begins to enter the GraalVM stage!
*GraalVM has the following features compared to the JVM compilation deployment:*
* During the application construction phase, static analysis of the application begins at the main entry point.
@ -12,7 +13,7 @@ One of the biggest changes of Spring Boot 3.0 is the support for GraalVM native
* Without lazy class loading, everything in the executable will be loaded into memory at startup.
* The supported Java applications have some limitations in some aspects. Therefore, it is not guaranteed that all the previous Java applications can directly use GraalVM technology for application construction, and there is a certain probability of incompatible exceptions.
Part of the middleware clients included in Spring Cloud Alibaba 2022.0.0.0 version of this release have completed the adaptation of constructing GraalVM native applications. The following is a sample application related to service registration, discovery and distributed transaction module supported by the community. After upgrading to Spring Boot 3.0, GraalVM is used to build a native application image to test the startup speed and runtime usage. (Sentinel and RocketMQ are still being adapted. The following test process is simulated for 5 times respectively under the environment of MacOS 11.4, 2.6GHz 6-Core Intel Core i7 processor and 16G memory) :
Part of the middleware clients included in Spring Cloud Alibaba 2022.0.0.0 version of this release have completed the adaptation of constructing GraalVM native applications. The following is a sample application related to service registration, discovery module supported by the community. After upgrading to Spring Boot 3.0, GraalVM is used to build a native application image to test the startup speed and runtime usage. (Sentinel, Seata and RocketMQ are still being adapted. The following test process is simulated for 5 times respectively under the environment of MacOS 11.4, 2.6GHz 6-Core Intel Core i7 processor and 16G memory) :
image::./pic/graalvm_performance.png[]
@ -22,11 +23,10 @@ From the above comparison, we can see that the latest Spring Cloud Alibaba appli
==== Build the project
Among all the components supported by Spring Cloud Alibaba for Spring Boot 3.0 this time, Nacos 2.1.1-RC already supports GraalVM technology to build applications. Next, we'll look at the components that use GraalVM technology to compile and run the application
Among all the components supported by Spring Cloud Alibaba for Spring Boot 3.0 this time, Nacos 2.1.1-RC already supports GraalVM technology to build applications. Next, we'll look at the components that use GraalVM to compile and run the application
The project construction can be carried out according to the general Spring Cloud Alibaba application construction method. The following are the relevant dependencies that must be added:
@ -64,11 +64,15 @@ The project construction can be carried out according to the general Spring Clou
</dependency>
</dependencies>
</dependencyManagement>
----
```
The above are necessary dependencies for registering Nacos through Spring Cloud Alibaba. Please adjust other dependencies according to your own needs. Please note that It is critical to use the GraalVM `spring-boot-starter-parent` parent module, which declares a native profile that contains the necessary information to create and run GraalVM native images. There are two ways to build a Spring Boot native image application:
1. Use Spring Boot's support for Cloud Native Buildpacks to generate lightweight containers containing native executables.
The above are necessary dependencies for registering Nacos through Spring Cloud Alibaba. Please adjust other dependencies according to your own needs. Please note that It is critical to use the GraalVM spring-boot-starter-parent parent module, which declares a native profile that contains the necessary information to create and run GraalVM native images. There are two ways to build a Spring Boot native image application: 1. Use Spring Boot's support for Cloud Native Buildpacks to generate lightweight containers containing native executables. 2. Use GraalVM Native Build Tools to generate a local executable file. As the first method has certain requirements on the local environment, If you need to use can refer to https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html#native-image.introducing-graalvm-native[Spring Boot 3.0] related user documents. The rest of this article demonstrates the second approach to GraalVM application building.
2. Use GraalVM Native Build Tools to generate a local executable file. As the first method has certain requirements on the local environment, If you need to use can refer to https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html#native-image.introducing-graalvm-native[Spring Boot 3.0] related user documents. The rest of this article demonstrates the second approach to GraalVM application building.
=== Preparation:
==== Preparation
To Build a Native image using Native Build Tools, you need to firstly install the GraalVM distribution on your machine. You can download it manually on the Liberica Native Image Kit page, or you can use tools like SDKMAN!. Run the following command to install the GraalVM environment:
[source,shell]
@ -85,7 +89,7 @@ openjdk version "17.0.5" 2022-10-18 LTS