這篇文章主要介紹“如何實(shí)現(xiàn)Spring Boot基于GraalVM構(gòu)建”,在日常操作中,相信很多人在如何實(shí)現(xiàn)Spring Boot基于GraalVM構(gòu)建問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何實(shí)現(xiàn)Spring Boot基于GraalVM構(gòu)建”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
龍子湖網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),龍子湖網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為龍子湖近千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的龍子湖做網(wǎng)站的公司定做!
容器化、函數(shù)式、低代碼、云原生各種概念和技術(shù)層出不窮,無奈,與時(shí)俱進(jìn),跟進(jìn)研究,發(fā)現(xiàn) Quarkus 最近比較火爆,抽空研究了一下,這就引出了本文的豬腳: GraalVM ,口號(hào)是:Run Programs Faster Anywhere
GraalVM Native Image是由Oracle Labs開發(fā)的一種AOT編譯器,支持基于JVM的高級(jí)語言,如Java, Scala, Clojure, Kotlin。Native Image以Java bytecode作為輸入,將所有應(yīng)用所需的class依賴項(xiàng)及runtime庫打包編譯生成一個(gè)單獨(dú)可執(zhí)行文件。具有高效的startup及較小的運(yùn)行時(shí)內(nèi)存開銷的優(yōu)勢(shì)。
與傳統(tǒng)的Java虛擬機(jī)不同,Native Image是封閉式的靜態(tài)分析和編譯,不支持class的動(dòng)態(tài)加載,程序運(yùn)行所需要的多有依賴項(xiàng)均在靜態(tài)分析階段完成。此外GraalVM Native Image運(yùn)行在一個(gè)名為SubstrateVM的輕量級(jí)的虛擬機(jī)之上。雖說是輕量級(jí)虛擬機(jī),SubstrateVM卻擁有運(yùn)行Java程序所必需的所有組件,包括deoptimizer、gc及thread scheduling等。 目前GraalVM Native Image并沒有完全支持多有的Java動(dòng)態(tài)特性,如不支持class動(dòng)態(tài)加載。 感興趣的童鞋可以點(diǎn)擊去看看,這里就不過多介紹了。
本文不涉及 GraalVM 的安裝和部署,只實(shí)踐 SpringBoot 基于 GraalVM 的構(gòu)建
SpringBoot 從 2.3 開始,已經(jīng)支持 GraalVM 打包。
構(gòu)建一個(gè)基本的 SpringBoot 項(xiàng)目
分別使用普通 jar 和 Native-image 方式打包,對(duì)比資源占用情況。
用你熟悉的工具搭建一個(gè)基本的 SpringBoot 項(xiàng)目
貓咪DTO:
@Data @Accessors(chain = true) public class CatDTO implements Serializable { private String id; private String name; private Integer age; }
實(shí)現(xiàn)基本增刪改查 API(模擬)
@RestController @Slf4j @RequestMapping("/cats") public class CatController { @PostMapping public CatDTO save(@RequestBody @Validated CatDTO param) { return param; } @DeleteMapping("/{id}") public ResponseEntitydelete(@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(); } }
其他代碼省略,感興趣的童鞋請(qǐng)參考 : 示例代碼
打包
$ ./mvnw package
運(yùn)行
$ cd target $ java -jar graalvm-demo-0.0.1-SNAPSHOT.jar
留意一下啟動(dòng)時(shí)間,我本機(jī)的是:
。。。。。。。 Started GraalvmDemoApplication in 1.774 seconds (JVM running for 2.212)
可以看到,啟動(dòng)用了將近 2 秒鐘。 如果引入數(shù)據(jù)庫、MQ等,肯定還會(huì)更多。
再看看內(nèi)存使用情況:
命令: $ 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 }' 結(jié)果: 51460 0.0 2.9 10508892 473.863MB /usr/bin/java-jargraalvm-demo-0.0.1-SNAPSHOT.jar
473.863MB ,就這么簡(jiǎn)單個(gè)功能的應(yīng)用,將近 500MB 內(nèi)存沒了。這個(gè)資源占用,不小! 相比之下,PHP、C#、甚至 Nodejs 實(shí)現(xiàn)同樣功能,哪個(gè)都沒 Java 吃的多~~妥妥的干飯專家....
GraalVM的安裝,請(qǐng)參考官方文檔,本文不做介紹。
安裝完畢后,記得安裝 native-image 本地映像打包插件。安裝命令:
gu install native-image
本例中具體環(huán)境如下:
// 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 項(xiàng)目需要進(jìn)行一些調(diào)整
//@SpringBootApplication //這里proxyBeanMethods方法代理關(guān)閉 @SpringBootApplication(proxyBeanMethods = false) public class GraalvmDemoApplication { public static void main(String[] args) { SpringApplication.run(GraalvmDemoApplication.class, args); } }
SringBoot 提供了 GraalVM Native 打包的 Maven 插件,可通過如下方式引入:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.2 com.luter graalvm-demo 0.0.1-SNAPSHOT graalvm-demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework spring-context-indexer true org.springframework.experimental spring-graalvm-native 0.8.3 spring-milestones Spring Milestones https://repo.spring.io/milestone spring-milestones Spring Milestones https://repo.spring.io/milestone org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok native org.graalvm.nativeimage native-image-maven-plugin 20.3.1 com.luter.graalvmdemo.GraalvmDemoApplication -Dspring.native.remove-yaml-support=false -Dspring.spel.ignore=true native-image package org.springframework.boot spring-boot-maven-plugin
打包命令:
$ ./mvnw -P native package
會(huì)顯示如下信息:
[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] ------------------------------------------------------------------------
這一步還是比較慢的,我本機(jī)用了將近 4 分鐘,請(qǐng)耐心等待.打包完畢后,target 目錄下會(huì)出現(xiàn)運(yùn)行文件: com.luter.graalvmdemo.graalvmdemoapplication,這個(gè)文件 57.6MB ,普通 Fat Jar 方式打包文件:graalvm-demo-0.0.1-SNAPSHOT.jar 17.7MB,大了三倍多
運(yùn)行 :
$ ./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)
這個(gè)啟動(dòng)太順滑了,回車后,幾乎是秒開,用時(shí)0.066秒。傳統(tǒng) Fatjar 方式啟動(dòng)用時(shí):1.774秒,20 多倍的提升。試想一下,一個(gè)動(dòng)不動(dòng)啟動(dòng)半分鐘的大型項(xiàng)目上云,是不是很激動(dòng)人心?
再看看內(nèi)存占用情況:
//命令 $ 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 }' //結(jié)果 51785 0.0 0.3 4526816 53.8711MB ./com.luter.graalvmdemo.graalvmdemoapplication
內(nèi)存占用:53.8711MB ,相比較傳統(tǒng) FatJar 的 473.863MB ,差不多 10 倍。 從啟動(dòng)速度到資源占用,都大幅度降低了,這對(duì)降低部署成本,還是很有幫助的。
到此,關(guān)于“如何實(shí)現(xiàn)Spring Boot基于GraalVM構(gòu)建”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!