關(guān)于SpringBoot改動后0.03秒啟動的問題
SpringBoot改動后0.03秒啟動
一、概述
GraalVM 是一種高性能運行時,可顯著提高應(yīng)用程序性能和效率,非常適合微服務(wù). 對于 Java 程序 GraalVM 負責(zé)將 Java 字節(jié)碼編譯成機器碼,映像生成過程使用靜態(tài)分析來查找可從主 Java 方法訪問的任何代碼,然后執(zhí)行完全提前 (AOT) 編譯。生成的本機二進制文件包含機器代碼形式的整個程序,以便立即執(zhí)行。
二、環(huán)境準(zhǔn)備
下載 GraalVM : https://www.graalvm.org/downloads/,需要選擇Java 17
版本,下載完成后解壓配置java環(huán)境變量
GRAALVM_HOME: D:\devops\graalvm-ce-java17-22.3.0 JAVA_HOME: %GRAALVM_HOME% PATH: %GRAALVM_HOME%\bin
cmd(不要使用powershell,powershell可能回報錯) 運行 gu install native-image
三、創(chuàng)建SpringBoot項目
SDK選擇17
pom文件如下,都是自動生成的,沒有修改
<?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>3.0.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo6</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo6</name> <description>demo6</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
創(chuàng)建IndexController,內(nèi)容如下:
@RestController @RequestMapping public class IndexController { @RequestMapping("/") public String test(){ return "spring-native"; } }
四、打包鏡像
在項目目錄里啟動cmd,輸入命令
mvn clean -U -DskipTests -Pnative spring-boot:build-image
運行命令:
C:\Windows\system32>docker images demo6 0.0.1-SNAPSHOT f025642103bf 43 years ago 96.8MB C:\Windows\system32> docker run -di -p 8080:8080 demo6:0.0.1-SNAPSHOT C:\Windows\system32>docker stats CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 159c9bd5f0dc jolly_hellman 0.01% 47.51MiB / 25.01GiB 0.19% 1.98kB / 538B 0B / 0B 18 PS D:\devops\java-project\demo6> docker logs -f 159c9bd5f0dc . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.0.0) 2022-12-21T07:19:43.970Z INFO 1 --- [ main] com.example.demo.Demo6Application : Starting AOT-processed Demo6Application using Java 17.0.5 with PID 1 (/workspace/com.example.demo.Demo6Application started by cnb in /workspace) 2022-12-21T07:19:43.971Z INFO 1 --- [ main] com.example.demo.Demo6Application : No active profile set, falling back to 1 default profile: "default" 2022-12-21T07:19:43.979Z INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-12-21T07:19:43.980Z INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-12-21T07:19:43.980Z INFO 1 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.1] 2022-12-21T07:19:43.983Z INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-12-21T07:19:43.983Z INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 12 ms 2022-12-21T07:19:43.997Z INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-12-21T07:19:43.997Z INFO 1 --- [ main] com.example.demo.Demo6Application : Started Demo6Application in 0.032 seconds (process running for 0.034) 2022-12-21T07:20:13.975Z INFO 1 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2022-12-21T07:20:13.975Z INFO 1 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2022-12-21T07:20:13.976Z INFO 1 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
可以看出使用native,打出的包為96M,運行內(nèi)存47M,啟動時間0.032S,占用資源和啟動速度方面還是非常有優(yōu)勢的
到此這篇關(guān)于SpringBoot改動后0.03秒啟動的文章就介紹到這了,更多相關(guān)SpringBoot改動后0.03秒啟動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在SpringBoot中使用@Value注解來設(shè)置默認值的方法
Spring Boot提供了一種使用注解設(shè)置默認值的方式,即使用 @Value 注解,下面這篇文章主要給大家介紹了關(guān)于如何在SpringBoot中使用@Value注解來設(shè)置默認值的相關(guān)資料,需要的朋友可以參考下2023-10-10thymeleaf實現(xiàn)前后端數(shù)據(jù)交換的示例詳解
Thymeleaf?是一款用于渲染?XML/XHTML/HTML5?內(nèi)容的模板引擎,當(dāng)通過?Web?應(yīng)用程序訪問時,Thymeleaf?會動態(tài)地替換掉靜態(tài)內(nèi)容,使頁面動態(tài)顯示,這篇文章主要介紹了thymeleaf實現(xiàn)前后端數(shù)據(jù)交換,需要的朋友可以參考下2022-07-07springboot中使用Feign整合nacos,gateway進行微服務(wù)之間的調(diào)用方法
這篇文章主要介紹了springboot中使用Feign整合nacos,gateway進行微服務(wù)之間的調(diào)用方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03java設(shè)計模式Ctrl?C和Ctrl?V的原型模式詳解
這篇文章主要為大家介紹了java設(shè)計模式Ctrl?C和Ctrl?V的原型模式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02基于 SpringBoot 實現(xiàn) MySQL 讀寫分離的問題
這篇文章主要介紹了基于 SpringBoot 實現(xiàn) MySQL 讀寫分離的問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02JAVA 中實現(xiàn)整句漢字拆分、轉(zhuǎn)換為ASCII實例詳解
這篇文章主要介紹了JAVA 中實現(xiàn)整句漢字拆分、轉(zhuǎn)換為ASCII實例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04maven如何動態(tài)統(tǒng)一修改版本號的方法步驟
這篇文章主要介紹了maven如何動態(tài)統(tǒng)一修改版本號的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Java操作Mongodb數(shù)據(jù)庫實現(xiàn)數(shù)據(jù)的增刪查改功能示例
這篇文章主要介紹了Java操作Mongodb數(shù)據(jù)庫實現(xiàn)數(shù)據(jù)的增刪查改功能,結(jié)合完整實例形式分析了java針對MongoDB數(shù)據(jù)庫的連接、增刪改查等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08