Spring

스프링 부트 시작시간 줄이는 5가지 방법

kimc 2022. 2. 9. 23:45
반응형

 

이번 글을 통해 배워 갈 내용

  1. Spring boot 시작 시간을 줄이는 5가지 방법

 


 

1. Lazy Initialization 기능

1-1 버전

Spring boot 2.2.M1 버전부터 지원 가능한 기능입니다.

 

1-2 사용법

Lazy initialization 기능을 추가하는 법은 간단하게

appliction.properties 파일에

spring.main.lazy-initialization=true​

를 추가해주면 됩니다.

 

1-3 장단점

시작시간을 줄여주는 장점이 있으나.....

시작시 몇 가지 에러를 확인하지 못해서 ClassNotFoundException을 발생시키는 경우와

부트 후 첫 API 요청이 느려지는 경우가 있습니다.

 

 

2. auto-configuration 설정

1-1 개요

Spring Boot는 auto-configuration을 많이 하고 이중에 필요 없는 auto-configuration 인경우도 있습니다.

이를 제외해주는 작업입니다.

 

2-2 사용법

application properties에 아래와 같이 추가합니다.

logging.level.org.springframework.boot.autoconfigure=DEBUG

그다음 앱을 실행하고

 

AUTO-CONFIGURATION REPORT

 

AUTO-CONFIGURATION REPORT를 잘 확인해서

아래와 같이 필요한 auto-configuration만 추가해줍니다.

@Configuration
@Import({
        HttpMessageConvertersAutoConfiguration.class,
        JacksonAutoConfiguration.class,
        ServerPropertiesAutoConfiguration.class,
        PropertyPlaceholderAutoConfiguration.class,
        DispatcherServletAutoConfiguration.class,
        EmbeddedServletContainerAutoConfiguration.class,
        ErrorMvcAutoConfiguration.class,
        HttpEncodingAutoConfiguration.class,
        WebMvcAutoConfiguration.class,
        WebSocketAutoConfiguration.class,
})
public class SampleApp {

2-3 장단점

장점은 물론 속도가 빨라지는 것이지만

수작업을 해야 되는 단점이 있습니다.

 

3. hibernate 설정

배포 시에 hibernate를 확인 validate 대신 none을 써주면 시작 시 확인을 안 해서 빨라지는 경우가 있습니다.

다만 이는 확인을 안하기 때문에 위험성이 증가합니다.

 

before

spring.jpa.hibernate.ddl-auto=validate

after

spring.jpa.hibernate.ddl-auto=none

 

4. JVM 설정

아래 Stackoverflow 글을 보시면

"Starting the JVM with -Xverify:none helps significantly."이라는 말씀이 있습니다.

이 방법은 해보지는 않았지만

JVM 설정을 바꿔서 실행해보면 됩니다.

 

5. 성능 좋은 CPU

당연한 이야기입니다.

 

6. 보너스 (그 외)

스택오버플로우 글들에 따르면 아래와 같은 추가 조치가 가능합니다.

actuators 사용을 안 할 수 있으면 안 하기,

JMX 끄기

fat jar 파일을 unpack 하고 명시적 class path로 실행하기

++ 아래 글 참조

 


참조 및 인용

https://docs.spring.io/spring-framework/docs/5.3.x/reference/html/core.html#context-functionality-startup

 

Core Technologies

In the preceding scenario, using @Autowired works well and provides the desired modularity, but determining exactly where the autowired bean definitions are declared is still somewhat ambiguous. For example, as a developer looking at ServiceConfig, how do

docs.spring.io

https://stackoverflow.com/questions/27230702/speed-up-spring-boot-startup-time

 

Speed up Spring Boot startup time

I have a Spring Boot application. I've added a lot of dependencies (unfortunately, looks I need all of them) and the startup time went up quite a lot. Just doing a SpringApplication.run(source, args)

stackoverflow.com

 


블로그 추천 포스트

https://codemasterkimc.tistory.com/50

 

300년차 개발자의 좋은 코드 5계명 (Clean Code)

이번 글을 통해 배워갈 내용  좋은 코드(Clean Code)를 작성하기 위해 개발자로서 생각해볼 5가지 요소를 알아보겠습니다. 개요 좋은 코드란 무엇일까요? 저는 자원이 한정적인 컴퓨터 세상에서 좋

codemasterkimc.tistory.com

 

 

오늘도 즐거운 코딩 하시길 바랍니다 ~ :)

 


 

반응형