diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/configclient/ConsulConfigServerBootstrapper.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/configclient/ConsulConfigServerBootstrapper.java index a60a8702..dee5bf73 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/configclient/ConsulConfigServerBootstrapper.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/configclient/ConsulConfigServerBootstrapper.java @@ -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); + } + } diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/configclient/ConsulConfigServerBootstrapperTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/configclient/ConsulConfigServerBootstrapperTests.java index 946e7ffc..37ea7cb3 100644 --- a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/configclient/ConsulConfigServerBootstrapperTests.java +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/configclient/ConsulConfigServerBootstrapperTests.java @@ -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 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