In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to realize the construction of Spring Boot based on GraalVM". In the daily operation, I believe that many people have doubts about how to realize the construction of Spring Boot based on GraalVM. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to achieve Spring Boot based on GraalVM". Next, please follow the editor to study!
Background
Containerization, functional, low code, cloud native various concepts and technologies emerge one after another, helpless, keep pace with the times, follow-up research, found that Quarkus is relatively popular recently, take the time to study, which leads to the pig foot of this article: GraalVM, the slogan is: Run Programs Faster Anywhere
Brief introduction
GraalVM Native Image is an AOT compiler developed by Oracle Labs that supports JVM-based high-level languages such as Java, Scala, Clojure, and Kotlin. Native Image takes Java bytecode as input and packages and compiles all the class dependencies and runtime libraries needed by the application to generate a separate executable file. It has the advantages of efficient startup and low runtime memory overhead.
Different from the traditional Java virtual machine, Native Image is a closed static analysis and compilation, does not support the dynamic loading of class, and many dependencies needed to run the program are completed in the static analysis phase. In addition, GraalVM Native Image runs on a lightweight virtual machine called SubstrateVM. Although a lightweight virtual machine, SubstrateVM has all the components necessary to run Java programs, including deoptimizer, gc, and thread scheduling. At present, GraalVM Native Image does not fully support the dynamic features of Java, such as class dynamic loading. Interested children's shoes can click to have a look, here is not too much introduction.
This article does not involve the installation and deployment of GraalVM, but only practices the construction of SpringBoot based on GraalVM
SpringBoot has supported GraalVM packaging since 2. 3.
target
Build a basic SpringBoot project
Use ordinary jar and Native-image to package and compare the resource usage.
Build a basic project
Build a basic SpringBoot project with tools you are familiar with
Cat DTO:
@ Data@Accessors (chain = true) public class CatDTO implements Serializable {private String id; private String name; private Integer age;}
Implement basic add, delete, modify and query API (simulation)
@ RestController@Slf4j@RequestMapping ("/ cats") public class CatController {@ PostMapping public CatDTO save (@ RequestBody @ Validated CatDTO param) {return param;} @ DeleteMapping ("/ {id}") public ResponseEntity delete (@ 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 ("list View");} @ GetMapping ("/ {id}") public CatDTO getById (@ PathVariable String id) {return new CatDTO ();}}
Other codes are omitted. For children's shoes you are interested in, please refer to: sample code
Package operation
Packing
$. / mvnw package
Running
$cd target$ java-jar graalvm-demo-0.0.1-SNAPSHOT.jar
Pay attention to the startup time, my local machine is:
. Started GraalvmDemoApplication in 1.774 seconds (JVM running for 2.212)
As you can see, it took nearly 2 seconds to start. If you introduce databases, MQ, and so on, there will certainly be more.
Take a look at memory usage:
Command: $ps aux | grep graalvm-demo | awk'{print $1 "\ t" $2 "\ t" $3 "\ t" $4 "\ t" $5 "\ t" $6DB 1024 "MB"\ t" 11 $12 $13 $14 $15 $16 $17 $18} 'result: 51460 0.02.9 10508892 473.863MB / usr/bin/java-jargraalvm-demo-0.0.1-SNAPSHOT.jar
473.863MB, such a simple function of the application, nearly ran out of 500MB memory. This resource takes up a lot! In contrast, PHP, C #, and even Nodejs achieve the same function, and none of them is as good as Java's dry rice expert.
Use GraalVM to package
For the installation of GraalVM, please refer to the official documentation, which is not described in this article.
After installation, remember to install the native-image local image packaging plug-in. Installation commands:
Gu install native-image
In this example, the specific environment is as follows:
/ / GraalVM version $java-versionopenjdk version "1.8.0mm 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) adjust the sample project
To use GraalVM packaging, the SpringBoot project needs to make some adjustments
Modify the startup class / / @ SpringBootApplication// where the proxyBeanMethods method agent closes @ SpringBootApplication (proxyBeanMethods = false) public class GraalvmDemoApplication {public static void main (String [] args) {SpringApplication.run (GraalvmDemoApplication.class, args);}} modify pom.xml
SringBoot provides a Maven plug-in packaged with GraalVM Native, which can be introduced in the following ways:
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 Native package
Package command:
. / mvnw-P native package
The following information is displayed:
[com.luter.graalvmdemo.graalvmdemoapplication:49751] classlist: 6378.68 ms 2.20 GB _ / _ / / _ / / _ (_) _ / / _ / / / _ / / _ _ `/ / / _` / / / | | / _ / / / _ / / / / / | _ /\ _ _ _ /\ _ / | _ _ /\ _ _ / _ / Removing unused configurationsVerification turned onRemoving XML supportRemoving SpEL supportRemoving JMX supportUse-Dspring.native.verbose=true on native-image call to see more detailed information from the feature [com.luter.graalvmdemo.graalvmdemoapplication:49751] (cap): 3263.36 ms 2.51 GBfeature operating mode: reflection (spring init active? False) Found # 15 types in static reflection list to registerSkipping # 12 types not on the classpathAttempting proxy registration of # 12 proxiesSkipped registration of # 4 proxies-relevant types not on classpath [com.luter.graalvmdemo.graalvmdemoapplication:49751] setup: 12937.89 ms, 2.93 GB. [com.luter.graalvmdemo.graalvmdemoapplication:49751] (clinit): 3141.89 ms, 5.59 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] (typeflow): 73234.16 ms, 5.59 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] (objects): 92334.47 ms, 5.59 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] (features): 18891.77 ms 5.59 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] analysis: 196029.89 ms, 5.59 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] universe: 5281.74 ms, 5.62 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] (parse): 17570.40 ms, 5.28 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] (inline): 23751.47 ms 7.13 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] (compile): 73916.12 ms, 7.74 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] compile: 123244.44 ms, 7.74 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] image: 15852.33 ms, 7.83 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] write: 3908.93 ms 7.83 GB [com.luter.graalvmdemo.graalvmdemoapplication:49751] [total]: 371061.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]-
This step is still relatively slow. It took me nearly 4 minutes. Please wait patiently. After the package is finished, the running file appears in the target directory: com.luter.graalvmdemo.graalvmdemoapplication, this file 57.6MB, and the normal Fat Jar package file: graalvm-demo-0.0.1-SNAPSHOT.jar 17.7MB, which is more than three times larger.
Run:
$. / com.luter.graalvmdemo.graalvmdemoapplication _ _ / / _ _ _ (() _ _ _ |'_ _ _ | |\ / _ _ _ | | | (_ _ | |) )'| _ |. _ _ | _ | | _ | _ | | _\ _ _ | | / = | _ | = | _ _ / = / _ /:: Spring Boot:: (v2.4.2) 2021-01-27 13 main 31purl 04.357 INFO 51785-[main] c.l.graalvmdemo.GraalvmDemoApplication: Starting GraalvmDemoApplication v0.0.1-SNAPSHOT using Java 1.8.0mm 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 13 INFO 31 INFO 04.357 INFO 51785-[graalvmdemostration] c.l.graalvmdemo.GraalvmDemoApplication: No active profile set Falling back to default profiles: default2021-01-27 13 INFO 31DA 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 initINFO: Initializing ProtocolHandler ["http-nio-10000"] Jan 27, 2021 1:31:04 PM org.apache.catalina.core.StandardService startInternalINFO: Starting service [Tomcat] Jan 27 2021 1:31:04 PM org.apache.catalina.core.StandardEngine startInternalINFO: Starting Servlet engine: [Apache Tomcat/9.0.41] Jan 27 2021 1:31:04 PM org.apache.catalina.core.ApplicationContext logINFO: Initializing Spring embedded WebApplicationContext2021-01-27 13 INFO 31VR 04.390 INFO 51785-[main] w.s.c.ServletWebServerApplicationContext: Root WebApplicationContext: initialization completed in 33 ms2021-01-27 13RV 31VR 04.401 INFO 51785-[main] o.s.s.concurrent.ThreadPoolTaskExecutor: Initializing ExecutorService 'applicationTaskExecutor'Jan 27 2021 1:31:04 PM org.apache.coyote.AbstractProtocol startINFO: Starting ProtocolHandler ["http-nio-10000"] 2021-01-27 13 INFO 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 http-nio-10000 31http-nio-10000 04.408 INFO 51785-- [main] c.l.graalvmdemo.GraalvmDemoApplication: Started GraalvmDemoApplication in 0.066 seconds (JVM running for 0.068)
This start-up is so smooth that it takes almost 0.066 seconds to drive after entering the car. Traditional Fatjar startup time: 1.774 seconds, more than 20 times the improvement. Just imagine, isn't it exciting to launch a large-scale project that always starts for half a minute?
And take a look at the memory footprint:
/ / Command $ps aux | grep com.luter.graalvmdemo.graalvmdemoapplication | grep S+ | awk'{print $1 "\ t" $2 "\ t" $3 "\ t" $4 "\ t" $5 "\ t" $6 MB ""\ t "$11}'/ / result 51785 0.0 0.3 4526816 53.8711MB. / com.luter.graalvmdemo.graalvmdemoapplication
Memory footprint: 53.8711MB, which is almost 10 times higher than the 473.863MB of traditional FatJar. From startup speed to resource consumption, it has been greatly reduced, which is very helpful to reduce deployment costs.
At this point, the study on "how to build Spring Boot based on GraalVM" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.