Use ConfigInstanceMonitor for connection details (#2630)

The ConfigInstanceMonitor always has the most up to date connection details for the config server instances.  They are updated by the HeartBeatEvent.  Fixes #2624
This commit is contained in:
Ryan Baxter
2024-11-14 08:58:38 -05:00
committed by GitHub
parent 78c693b77c
commit 4d6c61f3a8
3 changed files with 44 additions and 6 deletions

View File

@@ -123,16 +123,17 @@ public class ConfigServerConfigDataLocationResolver
.orElse(false);
// In the case where discovery is enabled we need to extract the config server
// uris, username, and password
// from the properties from the context. These are set in
// from the ConfigServiceMonitor. These are set in
// ConfigServerInstanceMonitor.refresh which will only
// be called the first time we fetch configuration.
// They will continue to be updated as HeartbeatEvents are received.
if (discoveryEnabled) {
ConfigClientProperties bootstrapConfigClientProperties = context.getBootstrapContext()
.get(ConfigClientProperties.class);
ConfigServerInstanceMonitor instanceMonitor = context.getBootstrapContext()
.get(ConfigServerInstanceMonitor.class);
configClientProperties.setUri(bootstrapConfigClientProperties.getUri());
configClientProperties.setPassword(bootstrapConfigClientProperties.getPassword());
configClientProperties.setUsername(bootstrapConfigClientProperties.getUsername());
configClientProperties.setUri(instanceMonitor.getUri());
configClientProperties.setPassword(instanceMonitor.getPassword());
configClientProperties.setUsername(instanceMonitor.getUsername());
}
}
else {

View File

@@ -82,6 +82,18 @@ final class ConfigServerInstanceMonitor implements SmartApplicationListener {
}
}
String[] getUri() {
return this.config.getUri();
}
String getUsername() {
return this.config.getUsername();
}
String getPassword() {
return this.config.getPassword();
}
void refresh() {
try {
String serviceId = this.config.getDiscovery().getServiceId();

View File

@@ -35,6 +35,7 @@ import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
import org.springframework.cloud.config.client.ConfigClientProperties.Credentials;
import org.springframework.cloud.endpoint.event.RefreshEvent;
import org.springframework.context.ConfigurableApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
@@ -131,6 +132,30 @@ public class DiscoveryClientConfigDataConfigurationTests {
assertThat(credentials2.getUri()).isEqualTo("http://localhost1:8888/");
}
@Test
public void verifyInstanceMonitorUpdates() {
ServiceInstance info1 = new DefaultServiceInstance("app1:8888", "app", "localhost", 8888, true);
ServiceInstance info2 = new DefaultServiceInstance("app2:8888", "app", "localhost1", 8888, false);
givenDiscoveryClientReturnsInfo(info1, info2);
setupAndRun();
verifyDiscoveryClientCalledOnce();
ConfigServerInstanceMonitor instanceMonitor = this.context.getBean(ConfigServerInstanceMonitor.class);
assertThat(instanceMonitor.getUri().length).isEqualTo(2);
assertThat(instanceMonitor.getUri()[0]).isEqualTo("https://localhost:8888/");
assertThat(instanceMonitor.getUri()[1]).isEqualTo("http://localhost1:8888/");
givenDiscoveryClientReturnsInfo(info1);
context.publishEvent(new HeartbeatEvent(this.context, "new"));
context.publishEvent(new RefreshEvent(this.context, "new", null));
instanceMonitor = this.context.getBean(ConfigServerInstanceMonitor.class);
assertThat(instanceMonitor.getUri().length).isEqualTo(1);
assertThat(instanceMonitor.getUri()[0]).isEqualTo("https://localhost:8888/");
}
@Test
public void setsPasssword() {
this.info.getMetadata().put("password", "bar");