Home > Java > javaTutorial > What is the reason why SpringBoot starts slowly?

What is the reason why SpringBoot starts slowly?

WBOY
Release: 2023-05-17 11:01:26
forward
4009 people have browsed it

    The reason why SpringBoot starts slowly

    The architecture of Spring Boot is so common that if you don’t master it, sometimes you are embarrassed to say that you are engaged in Java Work. But fire returns to fire, and there are some small problems that we need to pay attention to.

    If it is a single project and there are no requirements on startup speed, please ignore it.

    Let’s enter the topic below. Under what circumstances will the spring boot project start slower?

    Prerequisites

    The machine hardware configuration for starting the project cannot be delayed, otherwise you will say that the project The startup is slow, and the project still feels that the machine is not good... For example, the network, memory size, number of CPU cores, etc. The requirements are not too high, at least it must be enough.

    Put aside all kinds of artificial Factors such as long connection times and slow startup of other components lead to waiting...

    Cause 1

    There are too many useless configurations used in the project. For example, in the simplest web, you have also added various other maven configurations and configured them into the project (this is just an exaggerated example, these configurations are not needed), and then the project itself does not understand it. Since you configured , then load it.

    One takes 0.1 seconds, 10 takes 1 second, 100 takes 10 seconds...and so on. This is the easiest thing to understand.

    Reason 2

    Automatic configuration of spring boot. Although automatic configuration is a highlight of Spring Boot, it is also a drawback that leads to slow startup times.

    No way, usually something that is easy to use and has full functions will always have defects in some places.

    As mentioned above, there are many useless configurations. What I want to say here is that spring boot has too many things assembled by itself.

    You can see that the spring.factories files are stored in the relevant jar packages of spring boot. When you look inside, you will see that most of them have written a lot of classes, which implement the automatic assembly of spring boot. One of the core.

    But if you look carefully, you will find that there are actually many categories that you may not use at all.

    But what can you do? Whether you use it or not, at least it will check it for you. Isn’t this a place to spend time?

    Reason 3

    The project is not streamlined enough. The concept of microservices is quite popular now. I think its core meaning is simplicity and simplicity, and most of the places that need to emphasize startup speed are mostly in microservice projects.

    In a microservice project, it is best to start it within 10 seconds. But most of them can't do it because the projects are not divided into details enough.

    Whether it is faster to start one interface, or 10, or more, the answer is obvious. Many projects are large and comprehensive. The so-called large and comprehensive means that there are many functions, complete configurations, various expandable configurations, etc. It may not be comprehensive, but what does this mean? You have many configurations. It doesn’t matter whether they are useful or not. At least there will be a lot more class files that need to be compiled and loaded. You said you want such a project to start quickly, how can it start quickly?

    Solution

      Check whether there are too many useless dependencies in maven
    • It’s more complicated, use spring-boot-maven Start the plug-in debug, check which configurations are used in the project and which configurations are useless, then directly indicate in the startup class that only the used classes will be started, delete the @SpringBootApplication annotation, and manually fill in the @Configuration and @Import annotations , and add those configured classes in the @Import annotation.
    • Replace some dependencies with faster configuration. For example, you use A database connection pool, which has full functions and many configurations, but is slower. However, in fact, B connection pool can also meet the project needs, but has relatively few functions, so there are less configurations and faster startup. Do you need You should choose one between A and B.
    • Split out more streamlined projects to run independently. Big and comprehensive means more but not refined, lean and simple means less but proficient. Their speed is self-evident.
    • How to improve the startup speed of SpringBoot application

    Narrow the scope of @ComponentScan and @SpringBootApplication scanning classes

    2. Turn off Spring Boot’s JMX monitoring. Set spring.jmx.enabled=false

    3. Set the JVM parameter -noverify, and do not verify the class

    4. Delay loading of beans that are not necessarily loaded at startup

    5. Use Spring Boot’s global lazy loading

    6. Try not to use annotations for AOP aspects, which will cause all methods to be scanned at startup

    7. Turn off some endpoint monitoring functions

    8. Exclude unnecessary jar dependencies of the project

    9. When swagger scans the interface, specify to scan only the classes under a certain path

    10. Scan the shrink package of the Feign client interface Scan range

    The above is the detailed content of What is the reason why SpringBoot starts slowly?. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template