-
Spring Cloud Config 설정 파일 변경개발 2023. 2. 16. 20:38
1. Spring Cloud Bus 를 사용할 수 있습니다. 하지만 관리할 서버가 늘어난다면 POST 호출의 부담이 있음
2. Watcher
config 서버에서 설정 파일 변경을 감지하고 변경 사항을 수신하는 기능을 제공.
@Scheduled 어노테이션을 사용하면
주기적으로 설정 서버에 설정 변경 사항을 조회하고 업데이트할 수 있습니다. 이 방법은 매우 간단하고 구현하기 쉬우며, 서버와 클라이언트 간의 통신을 줄이기 위한 캐싱이 가능합니다. 그러나 변경 사항을 즉시 감지하는 것은 아니며, 설정 서버에 대한 주기적인 요청이 필요합니다.
반면 watcher는
설정 서버에서 변경 사항을 실시간으로 알릴 수 있는 방법입니다. 이 방법은 변경 사항을 즉시 감지하며, 변경 사항이 발생할 때마다 설정 서버로부터 새로운 설정을 가져옵니다. 그러나 이 방법은 불필요한 통신을 일으킬 수 있으며, 설정 서버와 클라이언트 간의 통신 빈도를 높일 수 있습니다.
따라서 @Scheduled 어노테이션은 주기적으로 업데이트하는 것이 적합한 상황에 사용될 수 있으며, watcher는 즉각적인 변경 사항을 필요로 하는 상황에서 사용될 수 있습니다.
같이 사용하는 방법
@Configuration public class ConfigWatcher { private ConfigurableApplicationContext context; private Environment environment; private ConfigServicePropertySourceLocator locator; @Autowired public ConfigWatcher(ConfigurableApplicationContext context, Environment environment, ConfigServicePropertySourceLocator locator) { this.context = context; this.environment = environment; this.locator = locator; } //일정 시간마다 설정 서버에서 설정을 조회 @Scheduled(fixedDelay = 5000) public void scheduledUpdate() { this.locator.setFailFast(true); this.context.getEnvironment().getPropertySources() .addFirst(this.locator.locate(this.environment)); this.locator.setFailFast(false); } @EventListener public void onApplicationEvent(EnvironmentChangeEvent event) { for (String key : event.getKeys()) { if (key.startsWith("spring.cloud.config")) { this.scheduledUpdate(); break; } } } }
scheduledUpdate(): @Scheduled 로 주기적 실행
onApplicationEvent(): watcher에서 변경 사항이 발생하면 실행. EnviromentChangeEvent 이벤트를 처리하며, 변경 사항을 확인하고 scheduledUpdate()메소드를 실행하여 설정을 업데이트 함.
watcher 의 변경 사항 감지
watcher는 @RefreshScope가 적용된 Bean 프록시를 생성하고, 해당 Bean의 프로터피를 모니터링하여 설정 변경이 감지되면 @ConfigurationProperties를 통해 값을 다시 바인딩 하여 Bean을 업데이트함.
1. Spring Cloud Config 서버에서 설정 파일이 변경
2. 클라이언트는 해당 파일을 내려받아 새로운 값으로 갱신
3. 이 때 Watcher는 설정 파일을 다시 읽어 @RefreshScope에 적용된 Bean을 업데이트하고 @ConfigurationProperties 를 통해 새로운 값을 바인딩하여 애플리케이션의 설정을 업데이트함.
watcher와 scheduling을 같이 사용한다면 watcher가 주기적으로 설정 파일을 모니터링하고
변경사항이 있을 경우 @Scheduleing에 지정된 주기에 따라 Bean을 업데이트 합니다.
import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Component; @Component @RefreshScope public class MyConfig { @Value("${my.property}") private String myProperty; // getter, setter } import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.context.scope.refresh.RefreshScope; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ConfigWatcher { @Autowired private RefreshScope refreshScope; //5초마다 호출하여 파일을 다시 읽어들이도록 설정 @Scheduled(fixedRate = 5000) public void watchConfig() { refreshScope.refresh("myConfig"); } }
@RefreshScope 설정된 빈은 애플리케이션이 시잘될 때 한번만 초기화 됩니다.
이후 변경 사항을 감지하면 자동으로 갱신되는데 감지하는 방법은 Cloud Config서버와의 일정한 주기로 Http 통신을 통해 이루어집니다.
기본적으로 5초 주기로 HTTP 요청을 보내도록 설정됐고 변경하려면 spring.cloud.config.watch.delay 프로퍼티를 설정하면 됩니다.
Watcher를 사용하여 설정 변경사항을 감지하고 업데이트하는 작업은 RefreshScope클래스에 구현됩니다.
'개발' 카테고리의 다른 글
homebrew install mysql (0) 2023.03.24 git info (0) 2023.03.22 Feign Client Except SSL (0) 2021.02.09 mysqlclient 1.4.0 or newer is required; you have 0.10.0 (0) 2020.08.26 IoT (Internet of Things), ICT (Information & Communication Technology) (0) 2020.08.13