Spring Native横空出世了

Spring官方推出了Spring Native,使用GraalVM将Spring应用编译成本地可执行的文件,这样就能提供一种新的方式来部署Spring 应用。这些原生的Spring应用可以作为一个独立的可执行文件进行部署,不再需要依赖JRE环境,并且还能提供一些有趣的特征,比如几乎瞬时的启动(一般会小于100毫秒)、瞬时的峰值性能以及更低的资源消耗,但代价是比JVM 更长的构建时间和更少的运行时优化。Spring Native支持Java和Kotlin。

有两种方式构建Spring Native应用
方式一:使用Spring Boot Buildpacks support生成包含本地可执行文件的轻量级容器。
方式二:使用GraalVM native image Maven plugin support生成本机可执行文件。

一、方式一:Buildpacks方式

1.先决条件

安装docker,并配置允许非root用户使用

2.Sample工程

代码来源:RESTful Web Service getting started guide
另外,需要注意的是版本问题:Spring Native 0.9.2仅支持Spring Boot 2.4.5
1.引入Springboot父坐标

<parent>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-parent</artifactId>  
    <version>2.4.5</version>  
    <relativePath/>  
</parent>   

2.添加Spring Native依赖

<dependencies>
    <!-- ... -->
    <dependency>
        <groupId>org.springframework.experimental</groupId>
        <artifactId>spring-native</artifactId>
        <version>0.9.2</version>
    </dependency>
</dependencies>

3.添加Spring AOT插件

<build>
    <plugins>
        <!-- ... -->
        <plugin>
            <groupId>org.springframework.experimental</groupId>
            <artifactId>spring-aot-maven-plugin</artifactId>
            <version>0.9.2</version>
            <executions>
                <execution>
                    <id>test-generate</id>
                    <goals>
                        <goal>test-generate</goal>
                    </goals>
                </execution>
                <execution>
                    <id>generate</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

然后,需要对IDE进行设置。在IDEA右侧maven栏中展开Plugins,在spring-aot插件下的spring-aot:generate上右键,选择Execute after Build。
4.启用native image支持

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <image>
            <builder>paketobuildpacks/builder:tiny</builder>
            <env>
                <BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
            </env>
        </image>
    </configuration>
</plugin>

5.仓库设置
需要添加spring-native依赖的仓库,和Spring AOT插件的仓库

<repositories>
    <!-- ... -->
    <repository>
        <id>spring-release</id>
        <name>Spring release</name>
        <url>https://repo.spring.io/release</url>
    </repository>
</repositories>

<pluginRepositories>
    <!-- ... -->
    <pluginRepository>
        <id>spring-release</id>
        <name>Spring release</name>
        <url>https://repo.spring.io/release</url>
    </pluginRepository>
</pluginRepositories>

3.build

mvn spring-boot:build-image

4.运行

docker run --rm -p 8080:8080 rest-service:0.0.1-SNAPSHOT

启动时间应该会小于100ms,而在jvm上的应用启动时间大约1500ms。

访问localhost:8080/greeting, 就会看到以下内容

{"id":1,"content":"Hello, World!"}

二、方式二:使用native-image-maven-plugin插件

1.先决条件

在安装GraalVM本机镜像编译器之前,需要满足许多先决条件。然后,需要在本机安装本地镜像编译器。

MacOS/Linux平台,安装本地镜像编译器,推荐使用SDKMAN.

  • 安装SDKMAN:
    curl -s "https://get.sdkman.io" | bash
    source "$HOME/.sdkman/bin/sdkman-init.sh"
    sdk version

  • 安装GraalVM:
    sdk install java 21.0.0.2.r8-grl (java8)
    sdk install java 21.0.0.2.r11-grl (java11)

  • 确保使用的是新安装的JDK:
    sdk use java 21.0.0.2.r8-grl (java8)
    sdk use java 21.0.0.2.r11-grl (java11)

  • 将本地镜像扩展引入到JDK
    gu install native-image

Windows平台,可以手动安装GraalVM builds

  • 下载GraalVM 21.0.0.2.
  • 设置JAVA_HOME和PATH环境变量
  • 将本地镜像扩展引入JDK
    gu install native-image

2.Sample工程

同样使用前面的例子。
1.引入Springboot父坐标
同方式一
2.添加Spring Native依赖
同方式一
3.添加Spring AOT插件
同方式一
4.启用native image支持

<profiles>
  <profile>
    <id>native-image</id>
    <build>
      <plugins>
        <plugin>
          <groupId>org.graalvm.nativeimage</groupId>
          <artifactId>native-image-maven-plugin</artifactId>
          <version>21.0.0.2</version>
          <configuration>
            <!-- The native image build needs to know the entry point to your application -->
            <mainClass>com.example.restservice.RestServiceApplication</mainClass>
          </configuration>
          <executions>
            <execution>
              <goals>
                <goal>native-image</goal>
              </goals>
              <phase>package</phase>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

5.仓库设置
同方式一

3.build

mvn -Pnative-image package

4.运行

target/com.example.restservice.restserviceapplication

启动时间应该会小于100ms,而在jvm上的应用启动时间大约1500ms。

访问localhost:8080/greeting, 就会看到以下内容

{"id":1,"content":"Hello, World!"}

参考:Spring Native documentation

不积跬步,无以至千里。不积小流,无以成江海!
原文地址:https://www.cnblogs.com/rouqinglangzi/p/14761901.html