您好,登录后才能下订单哦!
这篇文章主要介绍“如何实现Spring Boot基于GraalVM构建”,在日常操作中,相信很多人在如何实现Spring Boot基于GraalVM构建问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何实现Spring Boot基于GraalVM构建”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
容器化、函数式、低代码、云原生各种概念和技术层出不穷,无奈,与时俱进,跟进研究,发现 Quarkus 最近比较火爆,抽空研究了一下,这就引出了本文的猪脚: GraalVM ,口号是: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 项目
猫咪DTO:
@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<String> delete(@PathVariable String id) { return ResponseEntity.ok("Success" + id); } @PutMapping public CatDTO update(@RequestBody @Validated CatDTO param) { return param; } @GetMapping public ResponseEntity<String> 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 内存没了。这个资源占用,不小! 相比之下,PHP、C#、甚至 Nodejs 实现同样功能,哪个都没 Java 吃的多~~妥妥的干饭专家....
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); } }
SringBoot 提供了 GraalVM Native 打包的 Maven 插件,可通过如下方式引入:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <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>org.springframework.experimental</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>https://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://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>org.graalvm.nativeimage</groupId> <artifactId>native-image-maven-plugin</artifactId> <version>20.3.1</version> <configuration> <mainClass>com.luter.graalvmdemo.GraalvmDemoApplication</mainClass> <buildArgs>-Dspring.native.remove-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>
打包命令:
$ ./mvnw -P native package
会显示如下信息:
[com.luter.graalvmdemo.graalvmdemoapplication: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 [com.luter.graalvmdemo.graalvmdemoapplication: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 [com.luter.graalvmdemo.graalvmdemoapplication:49751] setup: 12,937.89 ms, 2.93 GB 。。。。。。。。 [com.luter.graalvmdemo.graalvmdemoapplication:49751] (clinit): 3,141.89 ms, 5.59 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] (typeflow): 73,234.16 ms, 5.59 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] (objects): 92,334.47 ms, 5.59 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] (features): 18,891.77 ms, 5.59 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] analysis: 196,029.89 ms, 5.59 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] universe: 5,281.74 ms, 5.62 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] (parse): 17,570.40 ms, 5.28 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] (inline): 23,751.47 ms, 7.13 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] (compile): 73,916.12 ms, 7.74 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] compile: 123,244.44 ms, 7.74 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] image: 15,852.33 ms, 7.83 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] write: 3,908.93 ms, 7.83 GB [com.luter.graalvmdemo.graalvmdemoapplication: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 目录下会出现运行文件: com.luter.graalvmdemo.graalvmdemoapplication,这个文件 57.6MB ,普通 Fat Jar 方式打包文件:graalvm-demo-0.0.1-SNAPSHOT.jar 17.7MB,大了三倍多
运行 :
$ ./com.luter.graalvmdemo.graalvmdemoapplication . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.2) 2021-01-27 13:31:04.357 INFO 51785 --- [ main] c.l.graalvmdemo.GraalvmDemoApplication : Starting GraalvmDemoApplication v0.0.1-SNAPSHOT using Java 1.8.0_282 on localhost with PID 51785 (/opt/luter/develop/temp/graalvm-demo/target/com.luter.graalvmdemo.graalvmdemoapplication started by clt in /opt/luter/develop/temp/graalvm-demo/target) 2021-01-27 13:31:04.357 INFO 51785 --- [ main] c.l.graalvmdemo.GraalvmDemoApplication : No active profile set, falling back to default profiles: default 2021-01-27 13:31:04.387 INFO 51785 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 10000 (http) Jan 27, 2021 1:31:04 PM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["http-nio-10000"] Jan 27, 2021 1:31:04 PM org.apache.catalina.core.StandardService startInternal INFO: Starting service [Tomcat] Jan 27, 2021 1:31:04 PM org.apache.catalina.core.StandardEngine startInternal INFO: Starting Servlet engine: [Apache Tomcat/9.0.41] Jan 27, 2021 1:31:04 PM org.apache.catalina.core.ApplicationContext log INFO: Initializing Spring embedded WebApplicationContext 2021-01-27 13:31:04.390 INFO 51785 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 33 ms 2021-01-27 13:31:04.401 INFO 51785 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' Jan 27, 2021 1:31:04 PM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["http-nio-10000"] 2021-01-27 13:31:04.408 INFO 51785 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 10000 (http) with context path '' 2021-01-27 13:31:04.408 INFO 51785 --- [ main] c.l.graalvmdemo.GraalvmDemoApplication : Started GraalvmDemoApplication in 0.066 seconds (JVM running for 0.068)
这个启动太顺滑了,回车后,几乎是秒开,用时0.066秒。传统 Fatjar 方式启动用时:1.774秒,20 多倍的提升。试想一下,一个动不动启动半分钟的大型项目上云,是不是很激动人心?
再看看内存占用情况:
//命令 $ ps aux | grep com.luter.graalvmdemo.graalvmdemoapplication | 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 ./com.luter.graalvmdemo.graalvmdemoapplication
内存占用:53.8711MB ,相比较传统 FatJar 的 473.863MB ,差不多 10 倍。 从启动速度到资源占用,都大幅度降低了,这对降低部署成本,还是很有帮助的。
到此,关于“如何实现Spring Boot基于GraalVM构建”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。