Spring Native項(xiàng)目實(shí)戰(zhàn)(體驗(yàn)79毫秒啟動(dòng)springboot應(yīng)用)
歡迎訪問我的GitHub
這里分類和匯總了欣宸的全部原創(chuàng)(含配套源碼):https://github.com/zq2599/blog_demos
關(guān)于Spring Native Spring官方博客于2021年03月11日宣布Spring Native的beta版本發(fā)布,借助Spring Native可以將spring應(yīng)用與GraalVM集成到native image中; native image是GraalVM的一項(xiàng)技術(shù),會(huì)將java應(yīng)用的字節(jié)碼編譯成可執(zhí)行文件,還會(huì)與JDK的本地庫做靜態(tài)鏈接,運(yùn)行應(yīng)用時(shí)無需Java虛擬機(jī),自身已集成了內(nèi)存管理,線程調(diào)度等能力,更多信息請參考:https://www.graalvm.org/reference-manual/native-image/ 本文以實(shí)戰(zhàn)為主,因此不會(huì)用太多篇幅介紹Spring Native的理論和優(yōu)勢,這里簡單小結(jié)幾個(gè)重要特性: 應(yīng)用啟動(dòng)速度不超過100毫秒; 啟動(dòng)即達(dá)到性能峰值(C1、C2等手段已經(jīng)用不上了) 運(yùn)行時(shí)更低的內(nèi)存消耗; docker鏡像不含JDK(所需文件已經(jīng)抽取出來放入鏡像),官方展示的含有Spring Boot, Spring MVC, Jackson, Tomcat的鏡像大小是50M; 為了達(dá)到前面的效果,代價(jià)是構(gòu)建時(shí)間更長; Spring Native到底是什么個(gè)人的理解:Spring Native是Spring提供的、制作native image的技術(shù)方案,涉及到以下關(guān)鍵技術(shù):
Spring ahead-of-time (AOT) 插件,對spring應(yīng)用做AOT處理,使得傳統(tǒng)虛擬機(jī)的class lazy loading在不復(fù)存在; spring-boot-maven-plugin插件在構(gòu)建docker鏡像的時(shí)候,使用了名為dmikusa/graalvm-tiny的鏡像作為構(gòu)建工具,這個(gè)工具負(fù)責(zé)將當(dāng)前工程的構(gòu)建結(jié)果和GraalVM集成在一起,最終制作成native image; 本篇概覽作為實(shí)戰(zhàn)風(fēng)格的文章,本篇主要內(nèi)容是開發(fā)springboot應(yīng)用再構(gòu)建為native image,然后驗(yàn)證其功能和效果,本文由以下內(nèi)容構(gòu)成:
環(huán)境信息 新建名為spring-native-tutorials的maven父工程,對實(shí)戰(zhàn)用到的依賴庫、插件等做統(tǒng)一配置; 新建名為webmvc的maven子工程,這是個(gè)springboot應(yīng)用; 將webmvc構(gòu)建為native image,這是個(gè)docker鏡像; 在docker中啟動(dòng)鏡像,驗(yàn)證是否可用,并檢查相關(guān)相關(guān)指標(biāo);環(huán)境信息本次實(shí)戰(zhàn)相關(guān)的環(huán)境信息如下:
電腦:MacBook pro 13寸 2018 操作系統(tǒng):macOS Big Sur 11.2.3 IDE:IntelliJ IDEA 2018.3.5 (Ultimate Edition) docker:20.10.5 JDK:1.8.0_211 maven:3.6.0 springboot:2.5.0-SNAPSHOT spring-aot-maven-plugin:0.10.0-SNAPSHOT源碼下載 本篇實(shí)戰(zhàn)中的完整源碼可在GitHub下載到,地址和鏈接信息如下表所示(https://github.com/zq2599/blog_demos):
名稱 鏈接 備注 項(xiàng)目主頁 https://github.com/zq2599/blog_demos 該項(xiàng)目在GitHub上的主頁 git倉庫地址(https) https://github.com/zq2599/blog_demos.git 該項(xiàng)目源碼的倉庫地址,https協(xié)議 git倉庫地址(ssh) git@github.com:zq2599/blog_demos.git 該項(xiàng)目源碼的倉庫地址,ssh協(xié)議這個(gè)git項(xiàng)目中有多個(gè)文件夾,本次實(shí)戰(zhàn)的源碼在spring-native-tutorials文件夾下,如下圖紅框所示:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <modules><module>webmvc</module> </modules> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.0-SNAPSHOT</version><relativePath/> </parent> <groupId>com.bolingcavalry</groupId> <artifactId>spring-native-tutorials</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <properties><java.version>1.8</java.version><!-- springboot生成jar文件的文件名后綴,用來避免Spring Boot repackaging和native-image-maven-plugin插件之間可能存在的沖突 --><classifier/><!-- 構(gòu)建鏡像時(shí)的定制參數(shù) --><native.build.args/><!-- 指定使用dmikusa/graalvm-tiny這個(gè)鏡像作為構(gòu)建工具,來構(gòu)建鏡像 --><builder>dmikusa/graalvm-tiny</builder><!-- spring cloud版本 --><spring-cloud.version>2020.0.2</spring-cloud.version> </properties> <!-- 插件管理 --> <pluginRepositories><pluginRepository> <id>spring-release</id> <name>Spring release</name> <url>https://repo.spring.io/release</url> <snapshots><enabled>false</enabled> </snapshots></pluginRepository><pluginRepository> <id>spring-milestone</id> <name>Spring milestone</name> <url>https://repo.spring.io/milestone</url> <snapshots><enabled>false</enabled> </snapshots></pluginRepository><pluginRepository> <id>spring-snapshot</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases><enabled>false</enabled> </releases></pluginRepository> </pluginRepositories> <!--倉庫管理--> <repositories><repository> <id>spring-release</id> <name>Spring release</name> <url>https://repo.spring.io/release</url> <snapshots><enabled>false</enabled> </snapshots></repository><repository> <id>spring-milestone</id> <name>Spring milestone</name> <url>https://repo.spring.io/milestone</url> <snapshots><enabled>false</enabled> </snapshots></repository><repository> <id>spring-snapshot</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases><enabled>false</enabled> </releases></repository> </repositories> <!--依賴包版本管理--> <dependencyManagement><dependencies> <dependency><groupId>org.springframework.experimental</groupId><artifactId>spring-native</artifactId><version>0.10.0-SNAPSHOT</version> </dependency> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope> </dependency></dependencies> </dependencyManagement> <!--插件配置--> <build><pluginManagement> <plugins><plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration><classifier>${classifier}</classifier><image> <builder>${builder}</builder> <env><BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE><BP_NATIVE_IMAGE_BUILD_ARGUMENTS>${native.build.args}</BP_NATIVE_IMAGE_BUILD_ARGUMENTS> </env> <!--執(zhí)行構(gòu)建任務(wù)的鏡像,如果在當(dāng)前環(huán)境不存在才會(huì)遠(yuǎn)程下載--> <pullPolicy>IF_NOT_PRESENT</pullPolicy></image> </configuration></plugin><!-- aot插件,ahead-of-time transformations --><plugin> <groupId>org.springframework.experimental</groupId> <artifactId>spring-aot-maven-plugin</artifactId> <version>0.10.0-SNAPSHOT</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></pluginManagement> </build></project>
上述pom.xml有以下幾處需要注意:
插件倉庫、依賴庫倉庫、依賴庫版本的配置都集中在這里; 配置好spring-aot-maven-plugin和spring-boot-maven-plugin這兩個(gè)插件,子工程會(huì)用到; spring-boot-maven-plugin插件制作docker鏡像的時(shí)候,又會(huì)用到dmikusa/graalvm-tiny鏡像,這才是真正構(gòu)建native image的工具;新建springboot類型的maven子工程新建名為webmvc的子工程,pom.xml內(nèi)容如下,可見內(nèi)容很簡單,就是常規(guī)依賴庫和父工程配置的兩個(gè)插件,一個(gè)負(fù)責(zé)執(zhí)行AOT,一個(gè)負(fù)責(zé)構(gòu)建鏡像:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd'> <parent><artifactId>spring-native-tutorials</artifactId><groupId>com.bolingcavalry</groupId><version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>webmvc</artifactId> <dependencies><dependency> <groupId>org.springframework.experimental</groupId> <artifactId>spring-native</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions><exclusion> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId></exclusion><exclusion> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-websocket</artifactId></exclusion> </exclusions></dependency><dependency> <groupId>org.apache.tomcat.experimental</groupId> <artifactId>tomcat-embed-programmatic</artifactId> <version>${tomcat.version}</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency> </dependencies> <build><plugins> <plugin><groupId>org.springframework.experimental</groupId><artifactId>spring-aot-maven-plugin</artifactId><configuration> <removeSpelSupport>true</removeSpelSupport></configuration> </plugin> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins> </build></project>
代碼很簡單,一個(gè)普通的springboot應(yīng)用,帶http接口:
package com.bolingcavalry.webmvc;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.http.HttpStatus;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.ResponseStatus;import org.springframework.web.bind.annotation.RestController;import java.time.LocalDateTime;@SpringBootApplication@RestControllerpublic class WebmvcApplication {public static void main(String[] args) {SpringApplication.run(WebmvcApplication.class, args);}@ResponseStatus(HttpStatus.ACCEPTED)@GetMapping('/status')public String status() {return 'status';}@GetMapping('/')public String hello() {return '1. Hello from Spring MVC and Tomcat, ' + LocalDateTime.now();}}
現(xiàn)在編碼已完成,來構(gòu)建docker鏡像吧,進(jìn)入父工程的pom.xml所在目錄,執(zhí)行以下命令:
mvn clean -U -DskipTests spring-boot:build-image
構(gòu)建成功后輸出信息如下(篇幅所限僅截取最后一小段),耗時(shí)4分25秒,期間筆記本風(fēng)扇狂轉(zhuǎn):
..[INFO] Successfully built image ’docker.io/library/webmvc:1.0-SNAPSHOT’[INFO] [INFO] ------------------------------------------------------------------------[INFO] Reactor Summary for spring-native-tutorials 1.0-SNAPSHOT:[INFO] [INFO] spring-native-tutorials ............................ SUCCESS [ 1.786 s][INFO] webmvc ............................................. SUCCESS [04:19 min][INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 04:25 min[INFO] Finished at: 2021-05-22T16:36:44+08:00[INFO] ------------------------------------------------------------------------[WARNING] The requested profile 'nexus' could not be activated because it does not exist.
執(zhí)行docker images命令,如下圖,可見鏡像已經(jīng)生成:
查看鏡像構(gòu)成,可見每個(gè)layer都不大,共計(jì)七十多M:
(base) zhaoqindeMBP:~ zhaoqin$ docker history webmvc:1.0-SNAPSHOTIMAGE CREATEDCREATED BY SIZE COMMENTb8ff54813ae0 41 years ago69B<missing> 41 years ago452kB<missing> 41 years ago2.51MB<missing> 41 years ago57.2MB<missing> 41 years ago1.4MB<missing> 41 years ago268B<missing> 41 years ago17.3MB
鏡像構(gòu)建成功,可以驗(yàn)證基本功能了;
驗(yàn)證執(zhí)行以下命令,創(chuàng)建一個(gè)臨時(shí)容器(控制臺(tái)結(jié)束后容器會(huì)被清理掉):
docker run --rm -p 8080:8080 webmvc:1.0-SNAPSHOT
控制臺(tái)輸出如下,79毫秒啟動(dòng)完成,真是一眨間的功夫:
(base) zhaoqindeMBP:~ zhaoqin$ docker run --rm -p 8080:8080 webmvc:1.0-SNAPSHOT2021-05-22 09:34:57.578 INFO 1 --- [ main] o.s.nativex.NativeListener : This application is bootstrapped with code generated with Spring AOT . ____ _ __ _ _ / / ___’_ __ _ _(_)_ __ __ _ ( ( )___ | ’_ | ’_| | ’_ / _` | / ___)| |_)| | | | | || (_| | ) ) ) ) ’ |____| .__|_| |_|_| |___, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.0-SNAPSHOT)2021-05-22 09:34:57.586 INFO 1 --- [ main] c.b.webmvc.WebmvcApplication : Starting WebmvcApplication using Java 1.8.0_292 on 3529ec458896 with PID 1 (/workspace/com.bolingcavalry.webmvc.WebmvcApplication started by cnb in /workspace)2021-05-22 09:34:57.586 INFO 1 --- [ main] c.b.webmvc.WebmvcApplication : No active profile set, falling back to default profiles: default2021-05-22 09:34:57.661 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)May 22, 2021 9:34:57 AM org.apache.coyote.AbstractProtocol initINFO: Initializing ProtocolHandler ['http-nio-8080']May 22, 2021 9:34:57 AM org.apache.catalina.core.StandardService startInternalINFO: Starting service [Tomcat]May 22, 2021 9:34:57 AM org.apache.catalina.core.StandardEngine startInternalINFO: Starting Servlet engine: [Apache Tomcat/9.0.46]May 22, 2021 9:34:57 AM org.apache.catalina.core.ApplicationContext logINFO: Initializing Spring embedded WebApplicationContext2021-05-22 09:34:57.669 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 79 msMay 22, 2021 9:34:57 AM org.apache.coyote.AbstractProtocol startINFO: Starting ProtocolHandler ['http-nio-8080']2021-05-22 09:34:57.713 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ’’2021-05-22 09:34:57.713 INFO 1 --- [ main] c.b.webmvc.WebmvcApplication : Started WebmvcApplication in 0.178 seconds (JVM running for 0.19)2021-05-22 09:34:57.713 INFO 1 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state LivenessState changed to CORRECT2021-05-22 09:34:57.714 INFO 1 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
瀏覽器訪問本機(jī)8080端口,如下圖,應(yīng)用基本功能正常:
再看看資源使用情況,命令是docker stats,如下可見,內(nèi)存僅用了30M:
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS6ce6c66fb4de jovial_hertz 0.11% 30.69MiB / 3.844GiB 0.78% 1.49kB / 158B 4.31MB / 0B 18
我曾經(jīng)在hub.docker.com上放了一個(gè)傳統(tǒng)springboot應(yīng)用制作的鏡像bolingcavalry/hellojib:0.0.1-SNAPSHOT,現(xiàn)在拿來和Spring Native鏡像對比一下,啟動(dòng)信息如下,耗時(shí)2036毫秒:
(base) zhaoqindeMacBook-Pro:~ zhaoqin$ docker run --rm -P docker.io/bolingcavalry/hellojib:0.0.1-SNAPSHOT . ____ _ __ _ _ / / ___’_ __ _ _(_)_ __ __ _ ( ( )___ | ’_ | ’_| | ’_ / _` | / ___)| |_)| | | | | || (_| | ) ) ) ) ’ |____| .__|_| |_|_| |___, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::(v2.1.6.RELEASE)2021-05-22 11:13:28.121 INFO 1 --- [ main] c.b.hellojib.HellojibApplication : Starting HellojibApplication on ffb32e5b68b9 with PID 1 (/app/classes started by root in /)2021-05-22 11:13:28.128 INFO 1 --- [ main] c.b.hellojib.HellojibApplication : No active profile set, falling back to default profiles: default2021-05-22 11:13:30.000 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)2021-05-22 11:13:30.054 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2021-05-22 11:13:30.054 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21]2021-05-22 11:13:30.241 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2021-05-22 11:13:30.241 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2036 ms2021-05-22 11:13:30.715 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService ’applicationTaskExecutor’2021-05-22 11:13:31.103 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ’’2021-05-22 11:13:31.110 INFO 1 --- [ main] c.b.hellojib.HellojibApplication : Started HellojibApplication in 3.618 seconds (JVM running for 4.297)2021-05-22 11:13:48.866 INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet ’dispatcherServlet’2021-05-22 11:13:48.866 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet: Initializing Servlet ’dispatcherServlet’2021-05-22 11:13:48.880 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet: Completed initialization in 14 ms
再用docker stats對比內(nèi)存,傳統(tǒng)springboot應(yīng)用的容器消耗了三百多兆內(nèi)存:
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDSffb32e5b68b9 eager_williamson 0.64% 356.3MiB / 3.844GiB 9.05% 3.46kB / 2.29kB 0B / 0B 316ce6c66fb4de jovial_hertz 0.11% 30.69MiB / 3.844GiB 0.78% 1.49kB / 158B 4.31MB / 0B 18
綜上所述,Spring Native帶來的優(yōu)勢是很明顯的,不過請注意:2021年03月11日官方宣布的Spring Native只是beta版本,請不要用于生產(chǎn)環(huán)境!!!
下載插件失敗在實(shí)際操作過程中,經(jīng)常會(huì)遇到maven插件或者docker鏡像下載失敗的情況,除了多試幾次,您還可以考慮將項(xiàng)目放到github上去,借助github action在云端完成鏡像構(gòu)建,具體操作請參考《用GitHub Actions制作Docker鏡像》
不用開發(fā),直接體驗(yàn)
我已將鏡像上傳到hub.docker.com,完整名稱是bolingcavalry/webmvc:1.0-SNAPSHOT,如果您只想體驗(yàn)一下native image的效果可以直接下載該鏡像使用;
以上就是Spring Native實(shí)戰(zhàn)(暢快體驗(yàn)79毫秒啟動(dòng)springboot應(yīng)用)的詳細(xì)內(nèi)容,更多關(guān)于Spring Native實(shí)戰(zhàn)的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Nginx+php配置文件及原理解析2. 解決啟動(dòng)django,瀏覽器顯示“服務(wù)器拒絕訪問”的問題3. Opencv+Python識(shí)別PCB板圖片的步驟4. css3溢出隱藏的方法5. ASP.NET MVC獲取多級類別組合下的產(chǎn)品6. IOS利用CocoaHttpServer搭建手機(jī)本地服務(wù)器7. python virtualenv和flask安裝沒有名為flask的模塊8. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效9. 關(guān)于HTML5的img標(biāo)簽10. python使用selenium爬蟲知乎的方法示例
