use of org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties in project spring-cloud-consul by spring-cloud.
the class ConsulDiscoveryClientConfigServiceBootstrapConfiguration method consulDiscoveryProperties.
@Bean
public ConsulDiscoveryProperties consulDiscoveryProperties(InetUtils inetUtils) {
ConsulDiscoveryProperties properties = new ConsulDiscoveryProperties(inetUtils);
// for bootstrap, lifecycle (and hence registration) is not needed, just discovery client
properties.getLifecycle().setEnabled(false);
return properties;
}
use of org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties in project spring-cloud-consul by spring-cloud.
the class ConsulConfigServerAutoConfigurationTests method onWhenRequested.
@Test
public void onWhenRequested() {
setup("spring.cloud.config.server.prefix=/config", "spring.cloud.consul.discovery.tags-as-metadata=false");
assertThat(this.context.getBeanNamesForType(ConsulDiscoveryProperties.class).length).isEqualTo(1);
ConsulDiscoveryProperties properties = this.context.getBean(ConsulDiscoveryProperties.class);
assertThat(properties.getMetadata()).containsEntry("configPath", "/config");
}
use of org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties 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;
});
}
Aggregations