如何从 Spring Boot 应用程序更改 consul K/V Store 中的值

Chr*_*ian 2 java spring-boot consul

我在 Spring Boot 2 应用程序中使用 Consul 的键/值存储作为 PropertySource。(org.springframework.cloud:spring-cloud-starter-consul-config)

我可以使用 @ConfigurationProperties 从 K/V 存储中读取属性,甚至当我通过 Consul Web 界面更改值时使用 @RefreshScope 更新它们。

但我确实有一些可以在应用程序中更改的动态属性。我如何将这些更改传播到 Consul,以便这些值真正发生变化。我尝试使用该属性的 Setter,但这并没有改变 Consul 中的值。

小智 5

使用此代码设置 KV 值。创建私有变量。

@Autowired
private ConsulClient consulClient;
Run Code Online (Sandbox Code Playgroud)

使用 setKVValue() 方法更改 KV。

consulClient.setKVValue("key", "value")
Run Code Online (Sandbox Code Playgroud)