InterviewSolution
Saved Bookmarks
| 1. |
How to refresh configuration changes on the fly in Spring Cloud environment? |
|
Answer» Using config-server, it's possible to refresh the configuration on the fly. The configuration changes will only be picked by Beans that are declared with @RefreshScope annotation. The following code illustrates the same. The PROPERTY message is defined in the config-server and changes to this property can be made at runtime without restarting the microservices. PACKAGE hello; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SPRINGBOOTAPPLICATION; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } } @RefreshScope 1 @RestController class MessageRestController { @Value("${message:Hello WORLD}") private String message; @RequestMapping("/message") String getMessage() { return this.message; }}1 @RefreshScope makes it possible to dynamically reload the configuration for this bean. |
|