Graalvm (3) Spring Boot – Native

說明

前一篇Graalvm (2) Spring Native介紹如何將SpringBoot 程式編成使用graalvm的docker image,是專為了MicroServices架構所用的,但也可以使用Spring Native把SpringBoot從可執行的jar轉為可執行檔。

修改

  1. pom.xml 中 properties

指定SpringBoot中的Main Class

<properties>
  ...
  <start-class>tw.elliot.native02.Native02Application</start-class>
</properties>
  1. spring-boot-maven-plugin

要加入classifierBP_NATIVE_IMAGE兩個設定

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <classifier>exec</classifier>
        <image>
            <env>
                <BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
            </env>
        </image>
    </configuration>
</plugin>
  1. spring-aot-maven-plugin

加入removeYamlSupport

<plugin>
    <groupId>org.springframework.experimental</groupId>
    <artifactId>spring-aot-maven-plugin</artifactId>
    <version>${spring-native.version}</version>
    <configuration>
        <removeYamlSupport>true</removeYamlSupport>
    </configuration>
    <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>
  1. native-image-maven-plugin

指定imageName, 不然會用main class的全名含package來命名,太長了…

<plugin>
    <groupId>org.graalvm.nativeimage</groupId>
    <artifactId>native-image-maven-plugin</artifactId>
    <version>${graalvm.version}</version>
    <configuration>
        <imageName>${project.artifactId}</imageName>
        <!--
                    <mainClass>${start-class}</mainClass><buildArgs>${native.build.args}</buildArgs>-->
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>native-image</goal>
            </goals>
            <phase>package</phase>
        </execution>
    </executions>
</plugin>

執行

很容易地

> mvn clean package

看到下面類似結果

[native03:41258]   (features):   2,875.74 ms,  5.01 GB
[native03:41258]     analysis:  52,579.25 ms,  5.01 GB
[native03:41258]     universe:   2,020.95 ms,  5.01 GB
[native03:41258]      (parse):   7,067.89 ms,  6.23 GB
[native03:41258]     (inline):   6,049.03 ms,  7.11 GB
[native03:41258]    (compile):  26,909.03 ms,  7.18 GB
[native03:41258]      compile:  43,108.02 ms,  7.18 GB
[native03:41258]        image:   7,157.03 ms,  7.21 GB
[native03:41258]        write:   1,309.45 ms,  7.21 GB
# Printing build artifacts to: native03.build_artifacts.txt
[native03:41258]      [total]: 112,830.18 ms,  7.21 GB
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

看看目錄下結果

> ls -al ./target                                                                                   
total 186952
-rwxr-xr-x   1 elliot  staff  76575000 May 21 09:33 native03
-rw-r--r--   1 elliot  staff  19102454 May 21 09:31 native03-0.0.1-SNAPSHOT-exec.jar

除了原來的jar檔,還多了一個不小的執行檔native03

然後依先前所訂的imageName做為執行檔,你就能看到一個飛快的熟悉畫面。

> ./native03

2021-05-21 09:35:13.724  INFO 42108 --- [           main] o.s.nativex.NativeListener               : This application is bootstrapped with code generated with Spring AOT

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.5)

2021-05-21 09:35:13.726  INFO 42108 --- [           main] tw.elliot.native02.Native02Application   : Starting Native02Application v0.0.1-SNAPSHOT using Java 11.0.11 on ElliotChende-MacBook-Pro.local with PID 42108 (/Users/elliot/IdeaProjects/spring-native-demo/native03/target/native03 started by elliot in /Users/elliot/IdeaProjects/spring-native-demo/native03)
....
2021-05-21 09:35:13.779  INFO 42108 --- [           main] tw.elliot.native02.Native02Application   : Started Native02Application in 0.226 seconds (JVM running for 0.227)

這樣即使主機上沒有jdk也能執行,而且超快!

不過先提醒,這是理想值,實際上真的案子會遇到一堆問題,但至少是個開始。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料