back end/java

[spring boot] 무중단 배포 - application.properties

노루아부지 2025. 5. 2. 10:48
반응형

1. 개발 환경

  • intellij
  • java 17
  • spring boot 3.2.0

 

2. dependency 추가

implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.cloud:spring-cloud-starter'

 

 

3. application.properties에 refresh 추가

management.endpoints.web.exposure.include=refresh

 

 

4. @ConfigurationProperties Class 생성

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

@Getter
@Setter
@Component
@RefreshScope
@ConfigurationProperties(prefix = "common.approval")
public class TestConfigProperties {
  private String yn;
}

 

 

[중요] @Value 는 자동으로 갱신되지 않고 위와 같이 만들어진 class의 값만 변경 가능

 

 

5. properties 사용

 

호출 방법은 class에서 직접 호출 하는 방법과, URL을 호출 하는 방법이 있다.

 

1) 직접 호출

publisher.publishEvent(new RefreshEvent(this, null, "Refresh triggered manually"));

 

2) URL 호출

* /actuator/refresh는 POST 만 지원

curl -X POST http://localhost:8080/actuator/refresh

 

 

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.endpoint.event.RefreshEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
@Getter
@Setter
public class TestService {
  private final TestConfigProperties testConfigProperties;

  @Value("${common.approval.yn}")
  private String approvalYn;

  @Autowired
  private ApplicationEventPublisher publisher;

  @Scheduled(cron = "*/1 * * * * *")
  public void testScheduled() {
    publisher.publishEvent(new RefreshEvent(this, null, "Refresh triggered manually"));
    log.error("approvalYn11:"+ testConfigProperties.getYn());
    log.error("approvalYn22:"+approvalYn);
  }
}

 

728x90
반응형
loading