SpringBoot基于GraalVM构建
背景
容器化、函数式、低代码、云原⽣各种概念和技术层出不穷,⽆奈,与时俱进,跟进研究,发现 Quarkus 最近⽐较⽕爆,抽空研究了⼀下,这就引出了本⽂的猪脚: ,⼝号是:Run Programs Faster Anywhere
简介
GraalVM Native Image是由Oracle Labs开发的⼀种AOT编译器,⽀持基于JVM的⾼级语⾔,如Java, Scala, Clojure, Kotlin。Native Image以Java bytecode作为输⼊,将所有应⽤所需的class依赖项及runtime库打包编译⽣成⼀个单独可执⾏⽂件。具有⾼效的startup及较⼩的运⾏时内存开销的优势。
与传统的Java虚拟机不同,Native Image是封闭式的静态分析和编译,不⽀持class的动态加载,程序运⾏所需要的多有依赖项均在静态分析阶段完成。此外GraalVM Native Image运⾏在⼀个名为SubstrateVM的轻量级的虚拟机之上。虽说是轻量级虚拟机,SubstrateVM却拥有运⾏Java程序所必需的所有组件,包括deoptimizer、gc及thread scheduling等。⽬前GraalVM Native Image并没有完全⽀持多有的Java动态特性,如不⽀持class动态加载。感兴趣的童鞋可以点击去看看,这⾥就不过多介绍了。
本⽂不涉及 GraalVM 的安装和部署,只实践 SpringBoot 基于 GraalVM 的构建
SpringBoot 从 2.3 开始,已经⽀持 GraalVM 打包。
⽬标
构建⼀个基本的 SpringBoot 项⽬
分别使⽤普通 jar 和 Native-image ⽅式打包,对⽐资源占⽤情况。
搭建基础项⽬
⽤你熟悉的⼯具搭建⼀个基本的 SpringBoot 项⽬
@Data
@Accessors(chain = true)
public class CatDTO implements Serializable {
private String id;
private String name;
private Integer age;
}
实现基本增删改查 API(模拟)
@RestController
@Slf4j
@RequestMapping("/cats")
public class CatController {
@PostMapping
public CatDTO save(@RequestBody @Validated CatDTO param) {
return param;
}
@DeleteMapping("/{id}")
public ResponseEntity delete(@PathVariable String id) {
return ResponseEntity.ok("Success" + id);
}
@PutMapping
public CatDTO update(@RequestBody @Validated CatDTO param) {
return param;
}
@GetMapping
public ResponseEntity list(CatDTO param, Pageable page) {
return ResponseEntity.ok("列表查看");
}
@GetMapping("/{id}")
public CatDTO getById(@PathVariable String id) {
return new CatDTO();
}
}
其他代码省略,感兴趣的童鞋请参考 :
打包运⾏
打包
$ ./mvnw package
运⾏
$ cd target
$ java -jar graalvm-demo-0.0.1-SNAPSHOT.jar
留意⼀下启动时间,我本机的是:
。。。。。。。
Started GraalvmDemoApplication in 1.774 seconds (JVM running for 2.212)
可以看到,启动⽤了将近 2 秒钟。如果引⼊数据库、MQ等,肯定还会更多。
再看看内存使⽤情况:
命令:
$ ps aux | grep graalvm-demo | awk '{print $1 "t" $2 "t" $3 "t" $4 "t" $5 "t" $6/1024"MB" "t" $11$12$13$14$15$16$17$18 }'
结果:
51460 0.0 2.9 10508892 473.863MB /usr/bin/java-jargraalvm-demo-0.0.1-SNAPSHOT.jar
473.863MB ,就这么简单个功能的应⽤,将近 500MB 内存没了。这个资源占⽤,不⼩!相⽐之下,、C#、甚⾄ Nodejs 实现同样功能,哪个都没 Java 吃的多~~妥妥的⼲饭专家....
使⽤ GraalVM 打包
GraalVM的安装,请参考官⽅⽂档,本⽂不做介绍。
安装完毕后,记得安装 native-image 本地映像打包插件。安装命令:
gu install native-image
本例中具体环境如下:
// GraalVM 版本
$ java -version
openjdk version "1.8.0_282"
OpenJDK Runtime Environment (build 1.8.0_282-b07)
OpenJDK 64-Bit Server VM GraalVM CE 20.3.1 (build 25.282-b07-jvmci-20.3-b09, mixed mode)
调整⽰例项⽬
为了使⽤ GraalVM 打包,SpringBoot 项⽬需要进⾏⼀些调整
修改启动类
//@SpringBootApplication
//这⾥proxyBeanMethods⽅法代理关闭
@SpringBootApplication(proxyBeanMethods = false)
public class GraalvmDemoApplication {
public static void main(String[] args) {
SpringApplication.run(GraalvmDemoApplication.class, args);
}
}
修改 l
SringBoot 提供了 GraalVM Native 打包的 Maven 插件,可通过如下⽅式引⼊:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
spring怎么读取yaml<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.luter</groupId>
<artifactId>graalvm-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>graalvm-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- graalvm 打包插件-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>perimental</groupId>
<artifactId>spring-graalvm-native</artifactId>
<version>0.8.3</version>
</dependency>
</dependencies>
<!-- graalvm 插件不在中央库,所以还需要指定 repository-->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<!-- 加⼀个 native 打包的配置-->
<profile>
<id>native</id>
<build>
<plugins>
<plugin>
<groupId&aalvm.nativeimage</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>20.3.1</version>
<configuration>
<mainClass>aalvmdemo.GraalvmDemoApplication</mainClass>
<buildArgs>-ve-yaml-support=false
-Dspring.spel.ignore=true
</buildArgs>
</configuration>
<executions>
<execution>
<goals>
<goal>native-image</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Native 打包
打包命令:
$ ./mvnw -P native package
会显⽰如下信息:
[aalvmdemoapplication:49751] classlist: 6,378.68 ms, 2.20 GB
_____ _ _ __ __ _
/ ___/ ____ _____ (_) ____ ____ _ / | / / ____ _ / /_ (_) _ __ ___
__ / __ / ___/ / / / __ / __ `/ / |/ / / __ `/ / __/ / / | | / / / _
___/ / / /_/ / / / / / / / / / / /_/ / / /| / / /_/ / / /_ / / | |/ / / __/
/
____/ / .___/ /_/ /_/ /_/ /_/ __, / /_/ |_/ __,_/ __/ /_/ |___/ ___/
/_/ /____/
Removing unused configurations
Verification turned on
Removing XML support
Removing SpEL support
Removing JMX support
Use -Dspring.native.verbose=true on native-image call to see more detailed information from the feature
[aalvmdemoapplication:49751] (cap): 3,263.36 ms, 2.51 GB
feature operating mode: reflection (spring init active? false)
Found #15 types in static reflection list to register
Skipping #12 types not on the classpath
Attempting proxy registration of #12 proxies
Skipped registration of #4 proxies - relevant types not on classpath
[aalvmdemoapplication:49751] setup: 12,937.89 ms, 2.93 GB
。。。。。。。。
[aalvmdemoapplication:49751] (clinit): 3,141.89 ms, 5.59 GB
[aalvmdemoapplication:49751] (typeflow): 73,234.16 ms, 5.59 GB
[aalvmdemoapplication:49751] (objects): 92,334.47 ms, 5.59 GB
[aalvmdemoapplication:49751] (features): 18,891.77 ms, 5.59 GB
[aalvmdemoapplication:49751] analysis: 196,029.89 ms, 5.59 GB
[aalvmdemoapplication:49751] universe: 5,281.74 ms, 5.62 GB
[aalvmdemoapplication:49751] (parse): 17,570.40 ms, 5.28 GB
[aalvmdemoapplication:49751] (inline): 23,751.47 ms, 7.13 GB
[aalvmdemoapplication:49751] (compile): 73,916.12 ms, 7.74 GB
[aalvmdemoapplication:49751] compile: 123,244.44 ms, 7.74 GB
[aalvmdemoapplication:49751] image: 15,852.33 ms, 7.83 GB
[aalvmdemoapplication:49751] write: 3,908.93 ms, 7.83 GB
[aalvmdemoapplication:49751] [total]: 371,061.00 ms, 7.83 GB
[INFO]
[INFO] --- spring-boot-maven-plugin:2.4.2:repackage (repackage) @ graalvm-demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 06:21 min
[INFO] Finished at: 2021-01-27T12:52:16+08:00
[INFO] ------------------------------------------------------------------------
这⼀步还是⽐较慢的,我本机⽤了将近 4 分钟,请耐⼼等待.打包完毕后,target ⽬录下会出现运⾏⽂件: aalvmdemoapplication,这个⽂件 57.6MB ,普
通 Fat Jar ⽅式打包⽂件:graalvm-demo-0.0.1-SNAPSHOT.jar 17.7MB,⼤了三倍多
运⾏ :
$ ./aalvmdemoapplication
. ____ _ __ _ _
/ / ___'_ __ _ _(_)_ __ __ _
( ( )___ | '_ | '_| | '_ / _` |
/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.2)
2021-01-27 13:31:04.357 INFO 51785 --- [ main] aalvmdemo.GraalvmDemoApplication : Starting GraalvmDemoApplication v0.0.1-SNAPSHOT using Java 1.8.0_282 on localhost with PID 51785 (/opt/luter/develop/temp/graalvm-dem 2021-01-27 13:31:04.357 INFO 51785 --- [ main] aalvmdemo.GraalvmDemoApplication : No active profile set, falling back to default profiles: default
2021-01-27 13:31:04.387 INFO 51785 --- [ main] o.s.at.TomcatWebServer : Tomcat initialized with port(s): 10000 (http)
Jan 27, 2021 1:31:04 PM AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-10000"]
Jan 27, 2021 1:31:04 PM org.StandardService startInternal
INFO: Starting service [Tomcat]
Jan 27, 2021 1:31:04 PM org.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.41]
Jan 27, 2021 1:31:04 PM org.ApplicationContext log
INFO: Initializing Spring embedded WebApplicationContext
2021-01-27 13:31:04.390 INFO 51785 --- [ main] ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 33 ms
2021-01-27 13:31:04.401 INFO 51785 --- [ main] o.urrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
Jan 27, 2021 1:31:04 PM AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-10000"]
2021-01-27 13:31:04.408 INFO 51785 --- [ main] o.s.at.TomcatWebServer : Tomcat started on port(s): 10000 (http) with context path ''
2021-01-27 13:31:04.408 INFO 51785 --- [ main] aalvmdemo.GraalvmDemoApplication : Started GraalvmDemoApplication in 0.066 seconds (JVM running for 0.068)
这个启动太顺滑了,回车后,⼏乎是秒开,⽤时0.066秒。传统 Fatjar ⽅式启动⽤时:1.774秒,20 多倍的提升。试想⼀下,⼀个动不动启动半分钟的⼤型项⽬上云,是不是很激动⼈⼼?
再看看内存占⽤情况:
//命令
$ ps aux | grep aalvmdemoapplication | grep S+ | awk '{print $1 "t" $2 "t" $3 "t" $4 "t" $5 "t" $6/1024"MB" "t" $11 }'
//结果
51785 0.0 0.3 4526816 53.8711MB ./aalvmdemoapplication
内存占⽤:53.8711MB ,相⽐较传统 FatJar 的 473.863MB ,差不多 10 倍。从启动速度到资源占⽤,都⼤幅度降低了,这对降低部署成本,还是很有帮助的。
结语
本⽂
整个过程,还是⽐较折腾的,尤其是 Graalvm 的安装,⽹络的原因,⽐较卡⽐较慢。还有就是,由于不⽀持 CGLIB 的动态代理了,很多 SpringBoot的⾃动配置功能⽆法使⽤,
如果你的项⽬对 SpringBoot 的各项特性依赖很强,谨慎⼊坑。否则,你也可以考虑⼀下:Quarkus。
官⽹有专门⽂档介绍⽀持情况,有兴趣的童鞋可以⾃⾏去查看。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论