Search in sources :

Example 1 with ConsulDiscoveryClient

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;
}
Also used : ConsulServiceInstance(org.springframework.cloud.consul.discovery.ConsulServiceInstance) ConsulDiscoveryClient(org.springframework.cloud.consul.discovery.ConsulDiscoveryClient) HealthService(com.ecwid.consul.v1.health.model.HealthService) Bean(org.springframework.context.annotation.Bean)

Example 2 with ConsulDiscoveryClient

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();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) BindContext(org.springframework.boot.context.properties.bind.BindContext) Bindable(org.springframework.boot.context.properties.bind.Bindable) BootstrapRegistry(org.springframework.boot.BootstrapRegistry) EnableAutoConfiguration(org.springframework.boot.autoconfigure.EnableAutoConfiguration) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ConsulDiscoveryClient(org.springframework.cloud.consul.discovery.ConsulDiscoveryClient) ConfigServerInstanceProvider(org.springframework.cloud.config.client.ConfigServerInstanceProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test) BootstrapRegistryInitializer(org.springframework.boot.BootstrapRegistryInitializer) ConfigurationPropertyName(org.springframework.boot.context.properties.source.ConfigurationPropertyName) SpringBootConfiguration(org.springframework.boot.SpringBootConfiguration) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) BindHandler(org.springframework.boot.context.properties.bind.BindHandler) ConsulDiscoveryClient(org.springframework.cloud.consul.discovery.ConsulDiscoveryClient) ConfigServerInstanceProvider(org.springframework.cloud.config.client.ConfigServerInstanceProvider) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 3 with ConsulDiscoveryClient

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;
    });
}
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)

Example 4 with ConsulDiscoveryClient

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

Aggregations

ConsulDiscoveryClient (org.springframework.cloud.consul.discovery.ConsulDiscoveryClient)4 ConfigServerInstanceProvider (org.springframework.cloud.config.client.ConfigServerInstanceProvider)2 ConsulClient (com.ecwid.consul.v1.ConsulClient)1 HealthService (com.ecwid.consul.v1.health.model.HealthService)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1 BootstrapRegistry (org.springframework.boot.BootstrapRegistry)1 BootstrapRegistryInitializer (org.springframework.boot.BootstrapRegistryInitializer)1 SpringBootConfiguration (org.springframework.boot.SpringBootConfiguration)1 EnableAutoConfiguration (org.springframework.boot.autoconfigure.EnableAutoConfiguration)1 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)1 BindContext (org.springframework.boot.context.properties.bind.BindContext)1 BindHandler (org.springframework.boot.context.properties.bind.BindHandler)1 Bindable (org.springframework.boot.context.properties.bind.Bindable)1 Binder (org.springframework.boot.context.properties.bind.Binder)1 ConfigurationPropertyName (org.springframework.boot.context.properties.source.ConfigurationPropertyName)1 InetUtils (org.springframework.cloud.commons.util.InetUtils)1 InetUtilsProperties (org.springframework.cloud.commons.util.InetUtilsProperties)1