use of org.springframework.cloud.consul.discovery.ConsulDiscoveryClient in project spring-cloud-consul by spring-cloud.
the class TestConsulDiscoveryClientBootstrapConfiguration method consulDiscoveryClient.
@Bean
public ConsulDiscoveryClient consulDiscoveryClient(ConsulDiscoveryProperties properties) {
ConsulDiscoveryClient client = mock(ConsulDiscoveryClient.class);
ConsulServiceInstance instance = new ConsulServiceInstance("configserver1", "configserver", properties.getHostname(), properties.getPort(), false);
instance.setHealthService(mock(HealthService.class));
given(client.getInstances("configserver")).willReturn(Arrays.asList(instance));
return client;
}
use of org.springframework.cloud.consul.discovery.ConsulDiscoveryClient in project spring-cloud-consul by spring-cloud.
the class ConsulConfigServerBootstrapperTests method enabledAddsInstanceProviderFn.
@Test
public void enabledAddsInstanceProviderFn() {
AtomicReference<ConsulDiscoveryClient> bootstrapDiscoveryClient = new AtomicReference<>();
BindHandlerBootstrapper bindHandlerBootstrapper = new BindHandlerBootstrapper();
ConfigurableApplicationContext context = new SpringApplicationBuilder(TestConfig.class).properties("--server.port=0", "spring.cloud.config.discovery.enabled=true", "spring.cloud.consul.discovery.hostname=myhost", "spring.cloud.service-registry.auto-registration.enabled=false", "spring.cloud.consul.host=localhost").addBootstrapRegistryInitializer(bindHandlerBootstrapper).addBootstrapRegistryInitializer(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();
assertThat(bindHandlerBootstrapper.onSuccessCount).isGreaterThan(0);
context.close();
}
use of org.springframework.cloud.consul.discovery.ConsulDiscoveryClient in project spring-cloud-consul by spring-cloud.
the class ConsulConfigServerBootstrapper method initialize.
@Override
public void initialize(BootstrapRegistry registry) {
if (!ClassUtils.isPresent("org.springframework.cloud.config.client.ConfigServerInstanceProvider", null) || // don't run if bootstrap enabled, how to check the property?
ClassUtils.isPresent("org.springframework.cloud.bootstrap.marker.Marker", null)) {
return;
}
// create consul client
registry.registerIfAbsent(ConsulProperties.class, context -> {
Binder binder = context.get(Binder.class);
if (!isDiscoveryEnabled(binder)) {
return null;
}
return binder.bind(ConsulProperties.PREFIX, Bindable.of(ConsulProperties.class), getBindHandler(context)).orElseGet(ConsulProperties::new);
});
registry.registerIfAbsent(ConsulClient.class, context -> {
if (!isDiscoveryEnabled(context.get(Binder.class))) {
return null;
}
ConsulProperties consulProperties = context.get(ConsulProperties.class);
return ConsulAutoConfiguration.createConsulClient(consulProperties);
});
registry.registerIfAbsent(ConsulDiscoveryClient.class, context -> {
Binder binder = context.get(Binder.class);
if (!isDiscoveryEnabled(binder)) {
return null;
}
ConsulClient consulClient = context.get(ConsulClient.class);
ConsulDiscoveryProperties properties = binder.bind(ConsulDiscoveryProperties.PREFIX, Bindable.of(ConsulDiscoveryProperties.class), getBindHandler(context)).orElseGet(() -> new ConsulDiscoveryProperties(new InetUtils(new InetUtilsProperties())));
return new ConsulDiscoveryClient(consulClient, properties);
});
// promote discovery client if created
registry.addCloseListener(event -> {
if (!isDiscoveryEnabled(event.getBootstrapContext().get(Binder.class))) {
return;
}
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;
});
}
use of org.springframework.cloud.consul.discovery.ConsulDiscoveryClient in project spring-cloud-consul by spring-cloud.
the class DiscoveryClientConfigServiceAutoConfigurationTests method onWhenRequested.
@Test
public void onWhenRequested() throws Exception {
ConsulTestcontainers.start();
Integer port = ConsulTestcontainers.getPort();
String host = ConsulTestcontainers.getHost();
setup("server.port=0", "spring.cloud.config.discovery.enabled=true", "spring.cloud.consul.port=" + port, "spring.cloud.consul.host=" + host, "logging.level.org.springframework.cloud.config.client=DEBUG", "spring.cloud.consul.discovery.catalog-services-watch.enabled=false", "spring.cloud.consul.discovery.test.enabled:true", "spring.application.name=discoveryclientconfigservicetest", "spring.jmx.enabled=false", "spring.cloud.consul.discovery.port:7001", "spring.cloud.consul.discovery.hostname:foo", "spring.cloud.config.discovery.service-id:configserver");
assertThat(this.context.getBeanNamesForType(ConsulConfigServerAutoConfiguration.class).length).isEqualTo(1);
ConsulDiscoveryClient client = this.context.getParent().getBean(ConsulDiscoveryClient.class);
verify(client, atLeast(2)).getInstances("configserver");
ConfigClientProperties locator = this.context.getBean(ConfigClientProperties.class);
assertThat(locator.getUri()[0]).isEqualTo("http://foo:7001/");
}
Aggregations