Registers and promotes ConsulDiscoveryClient if created

This commit is contained in:
spencergibb
2020-09-22 12:29:33 -04:00
parent b113cdaf85
commit 48a24d2c61
2 changed files with 42 additions and 13 deletions

View File

@@ -38,6 +38,7 @@ public class ConsulConfigServerBootstrapper implements Bootstrapper {
if (!ClassUtils.isPresent("org.springframework.cloud.config.client.ConfigServerInstanceProvider", null)) {
return;
}
// create consul client
registry.registerIfAbsent(ConsulProperties.class, context -> {
Binder binder = context.get(Binder.class);
return binder.bind(ConsulProperties.PREFIX, ConsulProperties.class).orElseGet(ConsulProperties::new);
@@ -46,19 +47,37 @@ public class ConsulConfigServerBootstrapper implements Bootstrapper {
ConsulProperties consulProperties = context.get(ConsulProperties.class);
return ConsulAutoConfiguration.createConsulClient(consulProperties);
});
registry.registerIfAbsent(ConfigServerInstanceProvider.Function.class, context -> {
registry.registerIfAbsent(ConsulDiscoveryClient.class, context -> {
Binder binder = context.get(Binder.class);
boolean enabled = binder.bind(ConfigClientProperties.CONFIG_DISCOVERY_ENABLED, Boolean.class).orElse(false);
if (!enabled) {
if (!isDiscoveryEnabled(binder)) {
return null;
}
ConsulClient consulClient = context.get(ConsulClient.class);
ConsulDiscoveryProperties properties = binder
.bind(ConsulDiscoveryProperties.PREFIX, ConsulDiscoveryProperties.class)
.orElseGet(() -> new ConsulDiscoveryProperties(new InetUtils(new InetUtilsProperties())));
return new ConsulDiscoveryClient(consulClient, properties)::getInstances;
return new ConsulDiscoveryClient(consulClient, properties);
});
// promote discovery client if created
registry.addCloseListener(event -> {
ConsulDiscoveryClient discoveryClient = event.getBootstrapContext().get(ConsulDiscoveryClient.class);
if (discoveryClient != null) {
event.getApplicationContext().getBeanFactory()
.registerSingleton("consulDiscoveryClient", discoveryClient);
}
});
registry.registerIfAbsent(ConfigServerInstanceProvider.Function.class, context -> {
if (!isDiscoveryEnabled(context.get(Binder.class))) {
return null;
}
ConsulDiscoveryClient discoveryClient = context.get(ConsulDiscoveryClient.class);
return discoveryClient::getInstances;
});
}
private boolean isDiscoveryEnabled(Binder binder) {
return binder.bind(ConfigClientProperties.CONFIG_DISCOVERY_ENABLED, Boolean.class).orElse(false);
}
}

View File

@@ -16,12 +16,17 @@
package org.springframework.cloud.consul.discovery.configclient;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.config.client.ConfigServerInstanceProvider;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
@@ -41,15 +46,20 @@ public class ConsulConfigServerBootstrapperTests {
@Test
public void enabledAddsInstanceProviderFn() {
new SpringApplicationBuilder(TestConfig.class)
.properties("--server.port=0", "spring.cloud.config.discovery.enabled=true",
"spring.cloud.service-registry.auto-registration.enabled=false")
.addBootstrapper(registry -> registry.addCloseListener(event -> {
ConfigServerInstanceProvider.Function providerFn = event.getBootstrapContext()
.get(ConfigServerInstanceProvider.Function.class);
assertThat(providerFn).as("ConfigServerInstanceProvider.Function was not created when it should.")
.isNotNull();
})).run().close();
AtomicReference<ConsulDiscoveryClient> bootstrapDiscoveryClient = new AtomicReference<>();
ConfigurableApplicationContext context = new SpringApplicationBuilder(TestConfig.class)
.properties("--server.port=0", "spring.cloud.config.discovery.enabled=true",
"spring.cloud.service-registry.auto-registration.enabled=false")
.addBootstrapper(registry -> registry.addCloseListener(event -> {
bootstrapDiscoveryClient.set(event.getBootstrapContext().get(ConsulDiscoveryClient.class));
ConfigServerInstanceProvider.Function providerFn = event.getBootstrapContext()
.get(ConfigServerInstanceProvider.Function.class);
assertThat(providerFn).as("ConfigServerInstanceProvider.Function was not created when it should.")
.isNotNull();
})).run();
ConsulDiscoveryClient discoveryClient = context.getBean(ConsulDiscoveryClient.class);
assertThat(discoveryClient == bootstrapDiscoveryClient.get()).isTrue();
context.close();
}
@SpringBootConfiguration