Search code examples
javaspring-bootconsul

How can I change a value in consul K/V Store from Spring Boot app


I use Consul's Key/Value Store as a PropertySource in a Spring Boot 2 app. (org.springframework.cloud:spring-cloud-starter-consul-config)

I can read the properties from the K/V Store with @ConfigurationProperties and even update them with @RefreshScope when I change the value via the Consul web interface.

But I do have some dynamic properties which can change in the application. How do I propagate these changes to Consul, so that the values are actually changed. I tried to use the Setter for the property but that did not change the value in Consul.


Solution

  • Use this code to set KV values. Create private variable.

    @Autowired
    private ConsulClient consulClient;
    

    change KVs using setKVValue() method.

    consulClient.setKVValue("key", "value")