关于java:GraalVM和Spring-Native尝鲜一步步让Springboot启动飞起来66ms完成启动

1次阅读

共计 4286 个字符,预计需要花费 11 分钟才能阅读完成。

简介

GraalVM是高性能的 JDK,反对 Java/Python/JavaScript 等语言。它能够让 Java 变成二进制文件来执行,让程序在任何中央运行更快。这或者是 Java 与 Go 的一场和平?

下载安装 GraalVM

装置 GraalVM

首先到官网下载,我是间接到 GitHub Release Page 下载的,请下载对应的零碎包,我下载如下:

graalvm-ce-java11-darwin-amd64-22.3.0.tar.gz

下载后解压到某个目录,我的如下:

/Users/larry/Software/graalvm-ce-java11-22.3.0

接着测试对应的程序是否能够失常执行,如java --version。在 Mac 上会报错如下:

is damaged and can’t be opened.

所以须要执行上面语句:

$ sudo xattr -r -d com.apple.quarantine /Users/larry/Software/graalvm-ce-java11-22.3.0

留神批改对应的目录。

而后就能够执行了:

$ ./java --version
openjdk 11.0.17 2022-10-18
OpenJDK Runtime Environment GraalVM CE 22.3.0 (build 11.0.17+8-jvmci-22.3-b08)
OpenJDK 64-Bit Server VM GraalVM CE 22.3.0 (build 11.0.17+8-jvmci-22.3-b08, mixed mode, sharing)

装置 native-image

这个工具用来把 Java 程序转化为本地二进制包,装置如下:

$ ./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 22.3.0)

配置环境

配置环境变量

因为这个 GraalVM 还不够成熟,我不想始终应用,就通过一个命令来切换,配置如下:

export GraalVM_HOME=/Users/larry/Software/graalvm-ce-java11-22.3.0/Contents/Home
alias switchOnGraalVM='export PATH=$GraalVM_HOME:$PATH'
alias switchOnGraalVMJavaHome='export JAVA_HOME=/Users/larry/Software/graalvm-ce-java11-22.3.0/Contents/Home'

alias switchOffGraalVM='export PATH=`echo $PATH | tr":""\n" | grep -v "graalvm" | tr "\n" ":"`'alias switchOffGraalVMJavaHome='export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home'

配置 IDEA

能够在 IDEA 上配置对应的 JDK,这样开发的时候能够应用:

整合 Spring Native 与 Spring Boot

一般 Spring Boot 程序

新来创立一个一般的 Spring Boot Web 程序,次要 Java 代码如下:

@SpringBootApplication
@RestController
@RequestMapping("/")
public class SpringbootNativeGraalVMMain {public static void main(String[] args) {SpringApplication.run(SpringbootNativeGraalVMMain.class, args);
    }

    @GetMapping("/hi-graalvm")
    public String hi() {return "This message is from Spring Boot built/run by GraalVM/Spring Native";}
}

启动时长为 1.193 秒,还不错。我电脑还不错。

整合 Spring Native

增加依赖:

<dependency>
  <groupId>org.springframework.experimental</groupId>
  <artifactId>spring-native</artifactId>
  <version>${spring-native.version}</version>
</dependency>

增加插件,这个插件十分重要,不然会有各种谬误:

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

增加以下插件来打包生成可执行程序:

<profiles>
  <profile>
    <id>native</id>
    <properties>
      <repackage.classifier>exec</repackage.classifier>
      <native-buildtools.version>0.9.11</native-buildtools.version>
    </properties>
    <dependencies>
      <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <scope>test</scope>
      </dependency>
    </dependencies>
    <build>
      <plugins>
        <plugin>
          <groupId>org.graalvm.buildtools</groupId>
          <artifactId>native-maven-plugin</artifactId>
          <version>${native-buildtools.version}</version>
          <extensions>true</extensions>
          <executions>
            <execution>
              <id>test-native</id>
              <phase>test</phase>
              <goals>
                <goal>test</goal>
              </goals>
            </execution>
            <execution>
              <id>build-native</id>
              <phase>package</phase>
              <goals>
                <goal>build</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

而后通过以下命令来 build 包,工夫会长很多,因为要转化为二进制可执行文件:

$ mvn clean package -Pnative

两分多钟后实现,生成了一个可执行文件,执行如下:

$ target/spring-boot-native-graalvm

后果只花了 0.066 秒,即 66 毫秒就能够了,这也太快了吧。

拜访接口也是失常的:

用 Docker 启动

先启动本地的 Docker,而后增加依赖如下:

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

通过以下命令打出 Docker 镜像:

mvn spring-boot:build-image

可能会破费很长时间,须要下载一些工具与镜像。

打包胜利后,多了镜像:

$ docker images | grep graalvm
spring-boot-native-graalvm                               1.0-SNAPSHOT                                     d2c8d5c52a3c        42 years ago        85.8MB 

启动如下:

$ docker run --rm spring-boot-native-graalvm:1.0-SNAPSHOT -p 8080:8080

启动工夫为 59ms,更短了。

留神

  • 间接通过 native-image 命令来将 jar 包转化为可执行文件,如遇到各种问题,劝大家放弃尝试,这也是 Spring Native 存在的价值。别问我为什么晓得,哈哈~~
  • 要留神切换对应的 Java 程序和 Java Home,不然 build 包会报错。
  • 看 Spring Native 的包名是 experimental 的,离生产应该还有间隔,不要轻易在生产上用。

代码

代码请看 GitHub: https://github.com/LarryDpk/p…


References:

Quick Start Guide

GraalVM Quick Reference

GraalVM Native Image Quick Reference v1

向云原生凑近 | 体验 GraalVM 动态编译字节码,尝试“超声波 Java”– Quarkus

正文完
 0