0

I'm working with a SpringBoot app and try to update new value from consul server when the config is changed. Follow the document from Spring Cloud but can not update new value without restarting the spring boot application. Here is the bootstrap.yml config file:

spring:
  application:
    name: api-app
  cloud:
    consul:
      discovery:
        enabled: true
        register: false
      host: ${CONSUL_IP:192.168.1.10}
      port: ${CONSUL_PORT:8500}
      config:
        acl-token: ${CONSUL_ACL_TOKEN:xxx-xxx-xxx-xxx} 
        enabled: true
        defaultContext: configuration
        format: YAML
        watch:
          enabled: true

application.java

@SpringBootApplication
@EnableConfigurationProperties(value= {ConsulValueProperties.class})
@EnableJpaRepositories
@EnableCaching
@EnableDiscoveryClient
public class SpringApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        setRegisterErrorPageFilter(false); 
        return application.sources(SpringApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringApplication.class, args);
    }
}

I've tried this document and seem application auto update Spring Cloud Consul: 3.5. Consul Catalog Watch

So, will spring boot automatically update the value when consul changes without restarting the spring boot application? If yes, how to config that or document? Thank you for reading and support!!!

Miner
  • 21
  • 6
Phong DLL
  • 1
  • 3

1 Answers1

0

Here is a thing you need to do, to make this work. use @RefreshScope in each and every been that uses your configuration. this makes your bean instantiate again when an EnvironmentChangeEvent received by your application.

if you have a config class which includes your @Value annotations, @RefreshScope should be added to your class level. also where ever this config class injected, the @RefreshScope should be added.

if you are configurin beans with @Bean annotated method and using custom properties from your config class, then you need to use @RefreshScope on the bean definition method. also there are some property which gives you more control about the configuration update that I refer you to spring documentation for consul:

https://docs.spring.io/spring-cloud-consul/docs/3.1.2-SNAPSHOT/reference/html/#config-data-import https://docs.spring.io/spring-cloud-consul/docs/3.1.2-SNAPSHOT/reference/html/appendix.html

Ali mjz
  • 13
  • 4
  • Thank you! But in my case, cause I put the key-value in cloud.consul.config.defaultContext instead of cloud.consul.discovery . So the application can not update new value. – Phong DLL Mar 10 '23 at 06:54