반응형
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
반응형
'back end > java' 카테고리의 다른 글
spring boot에서 Cache 사용하는 방법 (0) | 2025.04.09 |
---|---|
Hibernate 성능 튜닝 Tips (0) | 2025.03.10 |
[JUNIT] Spring Test MockMvc의 한글 깨짐 처리 (0) | 2025.03.10 |
Spring Data JPA에서 Insert 전에 select query를 하는 이유 (0) | 2024.02.02 |
[java] 문자열 자르기 함수 split 함수 사용 시 주의 사항. 문자열 자르는 방법 (0) | 2024.01.17 |