Search in sources :

Example 1 with ConsulDiscoveryProperties

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;
}
Also used : ConsulDiscoveryProperties(org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties) Bean(org.springframework.context.annotation.Bean)

Example 2 with ConsulDiscoveryProperties

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");
}
Also used : ConsulDiscoveryProperties(org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties) Test(org.junit.Test)

Example 3 with ConsulDiscoveryProperties

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;
    });
}
Also used : Binder(org.springframework.boot.context.properties.bind.Binder) ConsulDiscoveryClient(org.springframework.cloud.consul.discovery.ConsulDiscoveryClient) ConfigServerInstanceProvider(org.springframework.cloud.config.client.ConfigServerInstanceProvider) ConsulClient(com.ecwid.consul.v1.ConsulClient) InetUtilsProperties(org.springframework.cloud.commons.util.InetUtilsProperties) InetUtils(org.springframework.cloud.commons.util.InetUtils) ConsulProperties(org.springframework.cloud.consul.ConsulProperties) ConsulDiscoveryProperties(org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties)

Aggregations

ConsulDiscoveryProperties (org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties)3 ConsulClient (com.ecwid.consul.v1.ConsulClient)1 Test (org.junit.Test)1 Binder (org.springframework.boot.context.properties.bind.Binder)1 InetUtils (org.springframework.cloud.commons.util.InetUtils)1 InetUtilsProperties (org.springframework.cloud.commons.util.InetUtilsProperties)1 ConfigServerInstanceProvider (org.springframework.cloud.config.client.ConfigServerInstanceProvider)1 ConsulProperties (org.springframework.cloud.consul.ConsulProperties)1 ConsulDiscoveryClient (org.springframework.cloud.consul.discovery.ConsulDiscoveryClient)1 Bean (org.springframework.context.annotation.Bean)1