use of org.springframework.cloud.consul.ConsulProperties in project spring-cloud-consul by spring-cloud.
the class ConsulConfigDataLocationResolverTests method testLoadProperties.
@Test
public void testLoadProperties() {
Binder binder = Binder.get(new MockEnvironment());
ConfigDataLocationResolverContext resolverContext = mock(ConfigDataLocationResolverContext.class);
when(resolverContext.getBinder()).thenReturn(binder);
when(resolverContext.getBootstrapContext()).thenReturn(new DefaultBootstrapContext());
ConsulProperties properties = createResolver().loadProperties(resolverContext, UriComponentsBuilder.fromUriString("consul://myhost:8502").build());
assertThat(properties.getHost()).isEqualTo("myhost");
assertThat(properties.getPort()).isEqualTo(8502);
}
use of org.springframework.cloud.consul.ConsulProperties in project spring-cloud-consul by spring-cloud.
the class ConsulPropertyPrefixTests method setup.
@Before
public void setup() {
ConsulProperties properties = new ConsulProperties();
client = new ConsulClient(properties.getHost(), properties.getPort());
}
use of org.springframework.cloud.consul.ConsulProperties 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.ConsulProperties in project spring-cloud-consul by spring-cloud.
the class ConsulConfigDataLocationResolver method loadProperties.
protected ConsulProperties loadProperties(ConfigDataLocationResolverContext resolverContext, UriComponents location) {
Binder binder = resolverContext.getBinder();
ConsulProperties consulProperties = binder.bind(ConsulProperties.PREFIX, Bindable.of(ConsulProperties.class), getBindHandler(resolverContext)).orElseGet(ConsulProperties::new);
if (location != null) {
if (StringUtils.hasText(location.getHost())) {
consulProperties.setHost(location.getHost());
}
if (location.getPort() >= 0) {
consulProperties.setPort(location.getPort());
}
}
return consulProperties;
}
Aggregations