Spring boot/Spring boot library

spring-boot-devtools

metamong-data 2024. 7. 11. 15:00
728x90
반응형

spring-boot-devtools

개요

devtools는 Spring boot에서 제공하는 개발 편의를 라이브러리로, 실행 후 브라우저에서 보여주는 내용을 수정하려면 원래 어플리케이션을 재 시작해야 하지만 dev tools를 이용하여 이러한 불편한 점을 해결할 수 있습니다.

크게 아래의 5가지의 기능을 제공한다고 docs에서 알려주고 있습니다.

  • Property Defaults
  • Automatic Restart
  • Live Reload
  • Global Settings
  • Remote Application

사용 법

사용을 위해 아래와 같이 Maven 또는 Gradle에 추가 해야 합니다.

Maven

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

Gradle

dependencies {
    developmentOnly("org.springframework.boot:spring-boot-devtools")
}

🔥 주의 사항으로는 Devtools는 특히 다중 모듈 프로젝트에서 클래스 로딩 문제를 일으킬 수 있다고 합니다.

1. Property Defaults

Spring Boot에서 지원하는 여러 라이브러리는 성능을 개선하기 위해 cache 사용하지만, 배포된 환경에서는 유익하지만 개발 중에는 역효과가 있을 수 있으며, 애플리케이션에서 방금 변경 한 변경 사항을 볼 수 없게 됩니다. 이러한 이유로 기본적으로 Caching 옵션을 비활성화 합니다.

cache옵션은 일반적으로 application.properties파일의 설정에 따라 구성되며, 이러한 속성을 수동으로 설정할 필요 없이 모듈은 spring-boot-devtools자동으로 합리적인 개발 시간 구성을 적용합니다.
아래 표에는 적용되는 모든 속성들 입니다.

이름 기본값
server.error.include-binding-errors always
server.error.include-message always
server.error.include-stacktrace always
server.servlet.jsp.init-parameters.development true
server.servlet.session.persistent true
spring.docker.compose.readiness.wait only-if-started
spring.freemarker.cache false
spring.graphql.graphiql.enabled true
spring.groovy.template.cache false
spring.h2.console.enabled true
spring.mustache.servlet.cache false
spring.mvc.log-resolved-exception true
spring.reactor.netty.shutdown-quiet-period 0s
spring.template.provider.cache false
spring.thymeleaf.cache false
spring.web.resources.cache.period 0
spring.web.resources.chain.cache false

🗒️속성 기본값을 적용하지 않으려면 . 에서 spring.devtools.add-properties설정할 수 있습니다 . falseapplication.properties

2. Automatic Restart

클래스 경로의 파일이 변경될 때 마다 재 시작하는 기능이며, 코드 변경에 대한 매우 빠른 피드백 루프를 제공해주며, 기본적으로 디렉토리를 가리키는 클래스 경로의 모든 항목은 변경 사항이 있는지 모니터링 됩니다.

Restart vs Reload
Spring Boot에서 제공하는 재시작 기술은 두 개의 클래스 로더를 사용하여 작동합니다. 변경되지 않는 클래스는 기본 클래스 로더에 로드 되며, 개발 중인 클래스 즉 변경된 클래스는 재 시작 클래스 로더에 로드됩니다. 애플리케이션이 재시작되면 재 시작 클래스 로더가 사라지고 새 클래스 로더가 생성됩니다. 이 접근 방식은 기본 클래스 로더가 이미 사용 가능하고 채워져 있기 때문에 애플리케이션 재 시작이 일반적으로 “cold start” 보다 훨씬 빠른 것을 의미합니다.

Cold start : 최초 실행 ( 시스템에서 cache된 데이터가 없는 상태에서 시작)

Warm start : 이미 한번 이상 실행한 뒤로 데이터가 메모리에 일부 cache된 상태로 시작하는 것을 의미한다.

크게 아래와 같이 Automatic Restart를 설정 할 수 있다.

  • Logging Changes in Condition Evaluation (조건 평가에서 변경 사항 로깅)
  • Excluding Resources ( 리소스 제외)
  • Watching Additional Paths ( 추가 경로 보기 )
  • Disabling Restart ( 재 시작 비활성화 )
  • Using a Trigger File ( 트리거 파일 사용 )
  • Customizing the Restart Classloader ( 재 시작 클래스 로더 사용자 정의 )
  • Known Limitations ( 알려진 제한 사항 )

3. Live Reload

spring-boot-devtools모듈에는 리소스가 변경될 때 브라우저 새로 고침을 트리거하는 데 사용할 수 있는 내장형 LiveReload 서버가 포함되어 있습니다. LiveReload 브라우저 확장 프로그램은 Chrome, Firefox 및 Safari에서 무료로 제공됩니다. 선택한 브라우저의 마켓 플레이스나 스토어에서 'LiveReload'를 검색하여 이러한 확장 프로그램을 찾을 수 있습니다.

애플리케이션이 실행될 때 LiveReload 서버를 시작하지 않으려면 속성을 spring.devtools.livereload.enabled으로 설정할 수 있습니다.

🔥 파일이 변경될 때 LiveReload를 트리거 하려면 자동 재 시작을 활성화 해야 합니다.

4. Global Settings

다음 파일 $HOME/.config/spring-boot 경로에 추가하여 글로벌 DevTools 설정을 구성할 수 있습니다.

  1. spring-boot-devtools.properties
  2. spring-boot-devtools.yaml
  3. spring-boot-devtools.yml

이러한 파일에 추가된 모든 속성은 devtools를 사용하는 머신의 모든 Spring Boot 애플리케이션에 적용됩니다. 예를 들어, 항상 트리거 파일을 사용하도록 재시작을 구성하려면 다음 속성을 파일에 추가합니다 spring-boot-devtools.

아래 예

spring.devtools.restart.trigger-file=.reloadtrigger
  • Configuring File System Watcher
      spring.devtools.restart.poll-interval=2s
      spring.devtools.restart.quiet-period=1s
    위의 설정은 모니터링되는 클래스 경로 디렉토리는 이제 2초마다 변경 사항을 폴링하며, 추가 클래스 변경 사항이 없는지 확인하기 위해 1초 동안 조용한 상태가 유지하는 설정입니다.
  • 특정 시간 간격으로 클래스 변경 사항을 폴링한 다음, 사전 정의된 조용한 기간을 기다려 더 이상 변경 사항이 없는지 확인합니다.

5. Remote Application

원격으로 애플리케이션을 실행할 때 여러 기능을 사용할 수도 있습니다. 원격 지원은 활성화하면 보안 위험이 될 수 있으므로 opt-in 방식입니다.
devtools이를 활성화하려면 다음 목록에 표시된 대로 아래처럼 설정에 포함되어 있는지 확인해야 합니다 .

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludeDevtools>false</excludeDevtools>
            </configuration>
        </plugin>
    </plugins>
</build>
  • Running the Remote Client Application ( 원격 클라이언트 애플리케이션 실행 )
  • Remote Update ( 원격 업데이트 )

참고 링크 : https://docs.spring.io/spring-boot/reference/using/devtools.html#using.devtools

728x90