乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      編程語言Spring Native實戰(zhàn)(暢快體驗79毫秒啟動springboot應(yīng)用)

       冒險的K 2021-09-25

      歡迎訪問我的GitHub

      https://github.com/zq2599/blog_demos

      內(nèi)容:所有原創(chuàng)文章分類匯總及配套源碼,涉及Java、Docker、Kubernetes、DevOPS等;

      關(guān)于Spring Native

      • Spring官方博客于2021年03月11日宣布Spring Native的beta版本發(fā)布,借助Spring Native可以將spring應(yīng)用與GraalVM集成到native image中;

      • native image是GraalVM的一項技術(shù),會將java應(yīng)用的字節(jié)碼編譯成可執(zhí)行文件,還會與JDK的本地庫做靜態(tài)鏈接,運行應(yīng)用時無需Java虛擬機,自身已集成了內(nèi)存管理,線程調(diào)度等能力,更多信息請參考:https://www./reference-manual/native-image/

      • 本文以實戰(zhàn)為主,因此不會用太多篇幅介紹Spring Native的理論和優(yōu)勢,這里簡單小結(jié)幾個重要特性:

      1. 應(yīng)用啟動速度不超過100毫秒;

      2. 啟動即達到性能峰值(C1、C2等手段已經(jīng)用不上了)

      3. 運行時更低的內(nèi)存消耗;

      4. docker鏡像不含JDK(所需文件已經(jīng)抽取出來放入鏡像),官方展示的含有Spring Boot, Spring MVC, Jackson, Tomcat的鏡像大小是50M;

      5. 為了達到前面的效果,代價是構(gòu)建時間更長;

      Spring Native到底是什么

      個人的理解:Spring Native是Spring提供的、制作native image的技術(shù)方案,涉及到以下關(guān)鍵技術(shù):

      1. Spring ahead-of-time (AOT) 插件,對spring應(yīng)用做AOT處理,使得傳統(tǒng)虛擬機的class lazy loading在不復(fù)存在;

      2. spring-boot-maven-plugin插件在構(gòu)建docker鏡像的時候,使用了名為dmikusa/graalvm-tiny的鏡像作為構(gòu)建工具,這個工具負責(zé)將當前工程的構(gòu)建結(jié)果和GraalVM集成在一起,最終制作成native image;

      本篇概覽

      作為實戰(zhàn)風(fēng)格的文章,本篇主要內(nèi)容是開發(fā)springboot應(yīng)用再構(gòu)建為native image,然后驗證其功能和效果,本文由以下內(nèi)容構(gòu)成:

      1. 環(huán)境信息

      2. 新建名為spring-native-tutorials的maven父工程,對實戰(zhàn)用到的依賴庫、插件等做統(tǒng)一配置;

      3. 新建名為webmvc的maven子工程,這是個springboot應(yīng)用;

      4. 將webmvc構(gòu)建為native image,這是個docker鏡像;

      5. 在docker中啟動鏡像,驗證是否可用,并檢查相關(guān)相關(guān)指標;

      環(huán)境信息

      本次實戰(zhàn)相關(guān)的環(huán)境信息如下:

      1. 電腦:MacBook pro 13寸 2018

      2. 操作系統(tǒng):macOS Big Sur 11.2.3

      3. IDE:IntelliJ IDEA 2018.3.5 (Ultimate Edition)

      4. docker:20.10.5

      5. JDK:1.8.0_211

      6. maven:3.6.0

      7. springboot:2.5.0-SNAPSHOT

      8. spring-aot-maven-plugin:0.10.0-SNAPSHOT

      源碼下載

      名稱鏈接備注
      項目主頁https://github.com/zq2599/blog_demos該項目在GitHub上的主頁
      git倉庫地址(https)https://github.com/zq2599/blog_demos.git該項目源碼的倉庫地址,https協(xié)議
      git倉庫地址(ssh)git@github.com:zq2599/blog_demos.git該項目源碼的倉庫地址,ssh協(xié)議
      • 這個git項目中有多個文件夾,本次實戰(zhàn)的源碼在spring-native-tutorials文件夾下,如下圖紅框所示:

      在這里插入圖片描述

      新建名為spring-native-tutorials的maven父工程

      • 對Spring Native的學(xué)習(xí)不是寫出helloworld就完事,因此這里先創(chuàng)建一個父工程,為今后所有的應(yīng)用提供統(tǒng)一的依賴庫、插件管理;

      • 新建名為spring-native-tutorials的maven父工程,pom.xml內(nèi)容如下,有幾處要注意的地方稍后提到:

      <?xml version="1.0" encoding="UTF-8"?>4.0.0webmvcorg.springframework.bootspring-boot-starter-parent2.5.0-SNAPSHOTcom.bolingcavalryspring-native-tutorials1.0-SNAPSHOTpom1.8dmikusa/graalvm-tiny2020.0.2spring-releaseSpring releasehttps://repo./releasefalsespring-milestoneSpring milestonehttps://repo./milestonefalsespring-snapshotSpring Snapshotshttps://repo./snapshotfalsespring-releaseSpring releasehttps://repo./releasefalsespring-milestoneSpring milestonehttps://repo./milestonefalsespring-snapshotSpring Snapshotshttps://repo./snapshotfalseorg.springframework.experimentalspring-native0.10.0-SNAPSHOTorg.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.bootspring-boot-maven-plugin${classifier}
                                  ${builder}true${native.build.args}IF_NOT_PRESENTorg.springframework.experimentalspring-aot-maven-plugin0.10.0-SNAPSHOTtest-generatetest-generategenerategenerate
      • 上述pom.xml有以下幾處需要注意:

      1. 插件倉庫、依賴庫倉庫、依賴庫版本的配置都集中在這里;

      2. 配置好spring-aot-maven-plugin和spring-boot-maven-plugin這兩個插件,子工程會用到;

      3. spring-boot-maven-plugin插件制作docker鏡像的時候,又會用到dmikusa/graalvm-tiny鏡像,這才是真正構(gòu)建native image的工具;

      新建springboot類型的maven子工程

      • 新建名為webmvc的子工程,pom.xml內(nèi)容如下,可見內(nèi)容很簡單,就是常規(guī)依賴庫和父工程配置的兩個插件,一個負責(zé)執(zhí)行AOT,一個負責(zé)構(gòu)建鏡像:

      <?xml version="1.0" encoding="UTF-8"?>spring-native-tutorialscom.bolingcavalry1.0-SNAPSHOT4.0.0webmvcorg.springframework.experimentalspring-nativeorg.springframework.bootspring-boot-starter-weborg.apache.tomcat.embedtomcat-embed-coreorg.apache.tomcat.embedtomcat-embed-websocketorg.apache.tomcat.experimentaltomcat-embed-programmatic${tomcat.version}org.springframework.bootspring-boot-starter-testtestorg.springframework.experimentalspring-aot-maven-plugintrueorg.springframework.bootspring-boot-maven-plugin
      • 代碼很簡單,一個普通的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
      @RestController
      public 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鏡像吧,進入父工程的pom.xml所在目錄,執(zhí)行以下命令:

      mvn clean -U -DskipTests spring-boot:build-image
      • 構(gòu)建成功后輸出信息如下(篇幅所限僅截取最后一小段),耗時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)成,可見每個layer都不大,共計七十多M:

      (base) zhaoqindeMBP:~ zhaoqin$ docker history webmvc:1.0-SNAPSHOT
      IMAGE          CREATED        CREATED BY   SIZE      COMMENT
      b8ff54813ae0   41 years ago                69B41 years ago                452kB41 years ago                2.51MB41 years ago                57.2MB41 years ago                1.4MB41 years ago                268B41 years ago                17.3MB
      • 鏡像構(gòu)建成功,可以驗證基本功能了;

      驗證

      • 執(zhí)行以下命令,創(chuàng)建一個臨時容器(控制臺結(jié)束后容器會被清理掉):

      docker run --rm -p 8080:8080 webmvc:1.0-SNAPSHOT
      • 控制臺輸出如下,79毫秒啟動完成,真是一眨間的功夫:

      (base) zhaoqindeMBP:~ zhaoqin$ docker run --rm -p 8080:8080 webmvc:1.0-SNAPSHOT
      2021-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: default
      2021-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 init
      INFO: Initializing ProtocolHandler ["http-nio-8080"]
      May 22, 2021 9:34:57 AM org.apache.catalina.core.StandardService startInternal
      INFO: Starting service [Tomcat]
      May 22, 2021 9:34:57 AM org.apache.catalina.core.StandardEngine startInternal
      INFO: Starting Servlet engine: [Apache Tomcat/9.0.46]
      May 22, 2021 9:34:57 AM org.apache.catalina.core.ApplicationContext log
      INFO: Initializing Spring embedded WebApplicationContext
      2021-05-22 09:34:57.669  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 79 ms
      May 22, 2021 9:34:57 AM org.apache.coyote.AbstractProtocol start
      INFO: 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 CORRECT
      2021-05-22 09:34:57.714  INFO 1 --- [           main] o.s.b.a.ApplicationAvailabilityBean      : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
      • 瀏覽器訪問本機8080端口,如下圖,應(yīng)用基本功能正常:

      在這里插入圖片描述

      • 再看看資源使用情況,命令是docker stats,如下可見,內(nèi)存僅用了30M:

      CONTAINER ID   NAME               CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O     PIDS
      6ce6c66fb4de   jovial_hertz       0.11%     30.69MiB / 3.844GiB   0.78%     1.49kB / 158B     4.31MB / 0B   18
      • 我曾經(jīng)在hub.docker.com上放了一個傳統(tǒng)springboot應(yīng)用制作的鏡像bolingcavalry/hellojib:0.0.1-SNAPSHOT,現(xiàn)在拿來和Spring Native鏡像對比一下,啟動信息如下,耗時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: default
      2021-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 WebApplicationContext
      2021-05-22 11:13:30.241  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2036 ms
      2021-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     PIDS
      ffb32e5b68b9   eager_williamson   0.64%     356.3MiB / 3.844GiB   9.05%     3.46kB / 2.29kB   0B / 0B       31
      6ce6c66fb4de   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)境?。?!

      下載插件失敗

      在實際操作過程中,經(jīng)常會遇到maven插件或者docker鏡像下載失敗的情況,除了多試幾次,您還可以考慮將項目放到github上去,借助github action在云端完成鏡像構(gòu)建,具體操作請參考《用GitHub Actions制作Docker鏡像》

      不用開發(fā),直接體驗

      • 我已將鏡像上傳到hub.docker.com,完整名稱是bolingcavalry/webmvc:1.0-SNAPSHOT,如果您只想體驗一下native image的效果可以直接下載該鏡像使用;

      你不孤單,欣宸原創(chuàng)一路相伴

      1. Java系列

      2. Spring系列

      3. Docker系列

      4. kubernetes系列

      5. 數(shù)據(jù)庫+中間件系列

      6. DevOps系列

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多