关于什么是Spring Navtive 和 Spring Native的优缺点上一篇文章已经有介绍,不太了解的小伙伴可以参考之。

0x01 系统要求

我们以Windows平台为例

下载GraalVM 地址: https://www.graalvm.org/downloads/
设置JAVA_HOME 和 PATH 环境变量
以管理员方式执行 gu install native-image 将native-image 扩展集成到JDK中

这个过程可能失败,需要挂一下代理

set https_proxy=socks5://192.168.xx.xx:1080 

如果执行成功会显示如下信息

C:\Windows\system32>gu install native-image  
Downloading: Component catalog from www.graalvm.org
Processing Component: Native Image                                                                                      
Downloading: Component native-image: Native Image  from github.com                                                      
Installing new component: Native Image (org.graalvm.native-image, version 21.2.0)

0x02 示例项目

使用下面的工程项目

git clone https://github.com/spring-guides/gs-rest-service
cd gs-rest-service/complete

注意: Spring Native 0.10.2 只支持Spring Boot 2.5.3 ,如果有必要可自行修改。

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

添加Spring-native支持依赖

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

增加Spring AOT插件

<build>
    <plugins>
        <!-- ... -->
        <plugin>
            <groupId>org.springframework.experimental</groupId>
            <artifactId>spring-aot-maven-plugin</artifactId>
            <version>0.10.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>

添加Native 构建工具插件

<profiles>
        <profile>
            <id>native</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.graalvm.buildtools</groupId>
                        <artifactId>native-maven-plugin</artifactId>
                        <version>0.9.1</version>
                        <executions>
                            <execution>
                                <id>test-native</id>
                                <goals>
                                    <goal>test</goal>
                                </goals>
                                <phase>test</phase>
                            </execution>
                            <execution>
                                <id>build-native</id>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                                <phase>package</phase>
                            </execution>
                        </executions>
                        <configuration>
                            <!-- ... -->
                        </configuration>
                    </plugin>
                    <!-- Avoid a clash between Spring Boot repackaging and native-maven-plugin -->
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

增加Maven仓库和maven 插件仓库支持

<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>

构建应用

 mvn -Pnative -DskipTests package

window下要是用x64 native 工具命令行,构建完成之后会贼spring boot应用的target目录看到可执行文件。

运行应用

$ target/gs-rest-service

访问应用
访问: localhost:8080/greeting 可以对比一下和原生启动的速度

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

0x03 总结

使用工具构建可以直接生成可执行文件,特别是在Serverless架构下,对启动速度要求比较高的场景式优势明显。

0x04 错误记录

native 编译出现 org.graalvm.buildtools configuration file wasn't found.

Tags: spring, springboot, spring-native

Related Posts:

Leave a Comment