Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to optimize spring boot application

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces "how to optimize spring boot application" in detail, the content is detailed, the steps are clear, and the details are handled properly. I hope this "how to optimize spring boot application" article can help you solve your doubts.

Preface

Taptap-developer is a pure Grpc service driven by spring boot framework, so after only four steps, after removing the modules related to web and spring cloud, the startup speed is maintained steadily within 6s. In addition to the increase in startup speed, the memory has been sharply reduced by about 50% in service standby, from about 500m to less than 250m.

Analysis log

Log is the facade of an application, and the general technical composition of the application can be analyzed through the initiated log output before an in-depth understanding of the architecture of an application. Before analyzing the logs, I emphasize that this application is a pure Grpc service. For example, the log posted in the above figure is the output of the system log before optimization, with four red arrows pointing from top to bottom, which is the key information of this log analysis. Then the common optimization methods are summarized.

Optimization point 1: about SPRING DATA REPOSITORY SCANNING

Spring Data repository is a highly abstract data access layer interface. Common implementations include redis, jdbc, jpa, MongoDB, elasticsearch and so on. To implement a Spring-data-xxx package, you need to implement

Org.springframework.data.repository.core.support.RepositoryFactorySupport Abstract Class

Then define the implementation class in the! / META-INF/spring.factories file. When the spring container starts, it scans for information about loading the factories. If multiple spring-data-xxx implementations are scanned in a project, the log will be printed at startup

Multiple Spring Data modules found, entering strict repository configuration mode!

Optimization: when we see this log, we need to check whether these features are used in the project. For example, when spring-data-redis is introduced, only the jedis carried by it is used, and the jedis instance may be instantiated by itself. You can disable the repository feature at this time. The reference configuration is as follows:

Spring.data.redis.repositories.enabled=false

Spring Data repository has three built-in initialization modes, which correspond to the following:

DEFAULT: like other Spring Bean, initialized when the container context loads

DEFERRED: lazy loading, initialization starts after the container context is started

LAZY: lazy loading and delayed injection, container context initialization starts when the first request is received

For example, log output: Bootstrapping Spring Data repositories in DEFAULT mode. Initialization starts when the container starts by default.

Optimization: delayed loading can be selected according to the business characteristics.

Refer to configuration spring.data.jpa.repositories.bootstrap-mode=lazy

Spring Data repository scans the project for classes that implement the repository interface, and by default blindly scans all jar packages

Log output: Finished Spring Data repository scanning in 148ms. Found 0 repository interfaces.

Prints out the time-consuming situation of scanning the repository interface.

Optimization:

Here you can specify the scanning path through @ EnableRedisRepositories (basePackages = "com.taptap")

It can significantly improve the speed of scan loading.

Optimization point 2: about WEBAPPLICATIONCONTEXT

In spring, WebApplicationContext is an enhancement of ApplicationContext, implemented by spring-web-mvc, and adds web-related content such as servlet, session and so on.

As you can see from the log Initializing Spring embedded WebApplicationContext, we initialize a web container, while the pure Grpc service does not use the Web container context, so remove the following dependencies

Optimization:

Remove implementation ('org.springframework.boot:spring-boot-starter-web')

Optimization point 3: about SERVLET containers

Spring-web-mvc is based on the java web standard servlet design architecture. Servlet is driven by servlet containers, and common servlet include tocmat, jetty, undertow and so on. As you can see from the log, we started a 8081 servlet container. This should not appear in pure Grpc services, so just remove it.

Optimization:

Remove implementation 'org.springframework.boot:spring-boot-starter-undertow'

Optimization point 4: about ARCHAIUS configuration components

As can be seen from the log information pointed to by the last arrow, the project introduces the archaius configuration loading component, so when the project starts, archaius attempts to load the configuration source for the default policy. And our overall technology stack, the configuration center uniformly uses apollo, so it can be removed directly. Finally, through analysis and positioning, archaius is not introduced separately, but along with spring-cloud-starter-netflix-hystrix. This component is the most commonly used component of the spring-cloud- Netflix micro-service framework, but here, all the micro-services are registered directly to the K8s container. The circuit breakers, current limits and load balancers of all services have sunk to the container base facility platform, so although this package is introduced into the application layer, it has no practical effect, so the relevant components of spring cloud are removed finally.

Optimization:

Remove implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' and

Implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix' components,

The optimized log output is attached:

Changes in system resources

Before optimization

Optimized

Finally, based on the resource monitoring chart, the optimized resource occupancy is summarized from three dimensions:

Resource name optimization before optimization about 500m memory about 250m total number of threads 10778 load class 1292210041 read here, this "how to optimize spring boot application" article has been introduced, want to master the knowledge of this article also need to do your own practice to understand, if you want to know more related content of the article, welcome to pay attention to the industry information channel.

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: 265

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report