Graalvm (2) Spring Native

說明

Spring 為了Graalvm開了一個Project - Spring Native
在開始Beta之後,Spring Initializr 也開始支援了

只要將上列加入project裡就完成了一半

修改

要改用Graalvm建構SpringBoot的Docker Image只要做下面幾件事

Example

  1. 加入spring-native
<dependency>
  <groupId>org.springframework.experimental</groupId>
  <artifactId>spring-native</artifactId>
  <version>0.9.2</version>
</dependency>

plugin

  1. 修改spring-boot-maven-plugin
<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>

加入builder與BP_NATIVE_IMAGE設定

  1. 再加入 spring-aot-maven-plugin
<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>
  1. Build Image

與其他專案一樣,執行spring-boot:build-image

> mvn clean spring-boot:build-image

[INFO]     [creator]     ===> EXPORTING
[INFO]     [creator]     Adding 1/1 app layer(s)
....
[INFO]     [creator]     Setting default process type 'web'
[INFO]     [creator]     Saving docker.io/library/native02:0.0.1-SNAPSHOT...
[INFO]     [creator]     *** Images (a5b90094d80c):
[INFO]     [creator]           docker.io/library/native02:0.0.1-SNAPSHOT
[INFO]     [creator]     Reusing cache layer 'paketo-buildpacks/graalvm:jdk'
[INFO]     [creator]     Adding cache layer 'paketo-buildpacks/native-image:native-image'

這裡可以看到加入了下列的image

paketo-buildpacks/graalvm:jdk
paketo-buildpacks/native-image:native-image

做為執行的條件

比較

用相同的程式碼分別使用Spring Native與一般的方式建成Docker Image來比較

Image 大小

> docker images
native      0.0.1-SNAPSHOT  b0373b8ca666  41 years ago  80.7MB
traditional 0.0.1-SNAPSHOT  2ee0e5ee2241  41 years ago  224MB

啟動時間

> docker run --rm -p 8080:8080 native:0.0.1-SNAPSHOT
Started NativeApplication in 0.065 seconds (JVM running for 0.067)

> docker run --rm -p 8080:8080 traditional:0.0.1-SNAPSHOT
Started TraditionalApplication in 2.861 seconds (JVM running for 3.531)

Memory Usage

> Spring Native
CONTAINER ID   NAME                  CPU %     MEM USAGE / LIMIT     MEM %     NET I/O     BLOCK I/O   PIDS
c19a7348668f   relaxed_nightingale   0.09%     29.73MiB / 3.844GiB   0.76%     806B / 0B   0B / 0B     18

> Traditional
CONTAINER ID   NAME             CPU %     MEM USAGE / LIMIT     MEM %     NET I/O     BLOCK I/O    PIDS
58fe5f5ea463   elegant_mendel   0.58%     219.3MiB / 3.844GiB   5.57%     876B / 0B   0B / 319kB   33

在Image大小,啟動時間,初始記憶體使用量,用了Graalvm的都比較好。

雖說這就是個小測試,結果甚至不能做為參考,但看看這樣差距,也難怪Twitter會將Graalvm推上Production環境了。

如果夠穩定我也很心動,想把一些舊案子改用看看,

One thought to “Graalvm (2) Spring Native”

發表迴響

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