Previous:Too rolled, a company has completely open source the core system (smart system) that it has operated for many years….

 Hello everyone, I’m a top architect.

Recently, I made a microservices architecture project with Spring Cloud Alibaba, and I encountered a problem when deploying: there was not enough memory. At present, the project has 7 microservices, because I only have one Alibaba Cloud server (2C 4G), so I can only deploy all the microservices on one server, the deployment method is to use docker to make a springboot fat jar image, each microservice without adding any JVM tuning parameters without adding any JVM tuning parameters occupies about 500M of memory.

Because it is a microservice, it must be deployed: nacos, in addition to the use of reddis, sentinel, rocketmq, elk, etc. (mysql bought by Alibaba Cloud), just running these applications will occupy 2 more than G of memory, the remaining 1 multi-G memory is full after deploying 4 microservices, so the memory of the springboot application began to be initially optimized:

Add JVM parameters to optimize memory size

When the default free heap memory is less than 40%, the JVM increases the heap up to the maximum limit of -Xmx; When the free heap memory is greater than 70%, the JVM reduces the minimum limit for the heap up to -Xms.

Therefore, the server generally sets -Xms, -Xmx, and so on to avoid resizing the heap after each GC. The heap memory for an object is reclaimed by an automatic memory management system called the garbage collector.

By default, when the number of CPUs is less than 8, the value of ParallelGCThreads is equal to the number of CPUs, and my server is 2C, so this parameter can be omitted. After the configuration is completed, the startup service finds that the memory has indeed become smaller, from the original 500M to 100~200M, but it is not the effect I want, the effect I expect is to reach the level of tens of M.

After consulting a lot of information on the Internet, I learned that I could use Spring Native as a new technology to achieve my needs. (The technology is in a rapid iteration phase, with large changes, recommended for individual learning, not for production)

After the SpringBoot project uses Spring Native:

In short, to improve Java’s cloud-native competitiveness (personal understanding).

The following is an excerpt from Spring Native’s readme file on GitHub:

Spring Native provides beta support for compiling Spring applications into native executables using the GraalVM native image compiler to provide native deployment options typically designed to be packaged in lightweight containers. In fact, the goal is to support the nearly unchanged Spring Boot application on this new platform.

The following is an excerpt from other blogs:

In recent years, the term “native” has been flooded in cloud computing, edge computing and other fields, and the native favorite language has always been Golang, Rust and other development languages that are not running from Sandbox. Java has benefited from the popular compilation of the last century, the concept of execution everywhere, popular to this day, but also because of this reason, Java programs can not be separated from the JVM running environment, making it less favored by native programs. In today’s cloud-native world, the bloated JVM makes Java applications incomparably larger than other languages, and various gods have thought of many ways to make Java more “native”.

The environmental information related to this practice is as follows:

From the official documentation (above)

Applications that use Spring Native should be compiled using Java 11 or Java 17.

There are two main ways to build a Spring Boot-native application:

After going through various stepping pits, Method 1 and Method 2 were successfully used on this machine. In simple terms:

Method 1 is that after SpringBoot 2.3, you can use the spring-boot-maven-plugin plugin to build a docker image, use mvn spring-boot:build-image command combined with Docker’s API to implement the construction of Spring Bot native application, after successful execution, it will directly generate a docker image, and then run this image can be. Instead of writing Dockerfile, the relevant parameter configurations are configured in the pom .xml (the plugin’s configuration tag is very similar to fabric8 or spotify’s docker-maven-plugin).

Method 2 does not need to install docker, but to install Visual Studio, then execute the mvn -Pnative package command will generate an executable file (.exe), just run.

The main differences are as follows

Because each microservice uses Docker deployments instead of exe files, Method 1 fits my needs, so later in this article uses Spring Boot Buildpacks to build Spring Boot-native applications.

Official download address:

https://www.graalvm.org/downloads/

For method 1, the above three diagrams seem to only use the configuration JAVA_HOME on the line, if you want to succeed once, it is recommended to match 3, and then you can test yourself.

Oh! N open source projects necessary for private work! Hurry up and collect

Verify that the installation was successful

Open the new cmd, enter the following command, and wait for the installation

This step I failed to execute, the solution is to manually download native-image from github, and then unzip and install

https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-21.3.0/native-image-installable-svm-java11-windows-amd64-21.3.0.jar

jar with WinRAR can also be decompressed, after decompression is as follows

Open cmd in the bin directory, enter the following command, and wait for the installation

The specific steps are omitted, and you can follow the official documents:

https://docs.docker.com/desktop/windows/install/

The preparation work is all ahead, and this step is the beginning of the point

Start by quickly creating a Spring Boot project, which I named spring-native

The full pom is as follows

This article describes the Spring Native 0.11.1 version, its corresponding Spring Boot version must be 2.6.2, the above is just the most basic configuration case, the actual development also needs to be configured in the spring-boot-maven-plugin plugin configuration tag under the configuration tag of many other parameters.

For example, docker remote address and certificate path, jvm tuning parameters, configuration file designation, docker image name port warehouse address, etc., the best way is to see the official documentation of spring-boot-maven-plugin, here to configure jvm parameters as an example

The official documentation tells you that you only need to configure it under the configuration tab, for example

There are many other configuration parameters.

Official Documentation:

https://docs.spring.io/spring-boot/docs/2.6.2/maven-plugin/reference/htmlsingle/#build-image

After downloading the relevant dependencies, the computer fan began to whirr around, and the task manager found that the CPU utilization was 100%, the memory usage soared, and finally stabilized at 90%+.

Build successful

View all images

Spring-native is the image of the build

Create and run the container

Check the logs in Docker Desktop and find that the app started successfully, but it only takes time to start. That’s 59ms, which really confirms that Spring Native startup is millisecond level.

The interface was called successfully

In Docker Desktop to see the memory occupancy, only about 28M.

Don’t use Spring Native to launch apps

It takes 3s to start, occupying up to 511M of memory.

The article is for reference only, and it is recommended to study in conjunction with the latest official documents of Spring Native.

https://docs.spring.io/spring-native/docs/current/reference/htmlsingle/index.html

There are more high-quality project system learning resources in GitHub, welcome to share them with other students!

Everyone is welcome to discuss and collide their views and express their own views. If you have questions, you can also talk to me and communicate. Extension: Take on private work

Finally, I sorted out a copy of the BAT big factory interview real questions, and the required scannable code reply to the “interview questions” can be obtained.

 “Top Architects” has established a reader architect exchange group, and everyone can add a small WeChat to add a group. Welcome friends who have ideas and are willing to share ideas to exchange and learn together.

Scan to add friends to invite you to the architect group, add me when you indicate [name + company + position]

Copyright notice: The content comes from the network, and the copyright belongs to the original author. If there is any infringement, please let us know, we will delete it immediately and apologize. Thank you.

Guess you still want to see it

Recommend an open source common background management system (with source code)

Look at people’s IM instant messaging system, that’s called an elegant (with source code)

Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled, how to achieve it?

Alibaba technical expert: Yiwen teaches you to draw a technical architecture diagram efficiently

Awesome! Receive N system items necessary for private work! Hurry up and collect it (with source code collection issue 3)!

A rapid development modular scaffolding, saving you time and cost in development!

Several common JVM tuning scenarios (recommended collection)

How hard is it to make a nucleic acid system that doesn’t collapse?

Slow query MySQL positioning optimization trick, optimized from 10s to 300ms

Nginx+SpringBoott for load balancing

9 ways to get the /resources directory resource file, and who wouldn’t?!

Interviewer: How to design the sub-database sub-table of Meituan Takeaway?

Collection: Popularize the working principle of computers