Search in sources :

Example 1 with ConsulProperties

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);
}
Also used : Binder(org.springframework.boot.context.properties.bind.Binder) DefaultBootstrapContext(org.springframework.boot.DefaultBootstrapContext) MockEnvironment(org.springframework.mock.env.MockEnvironment) ConfigDataLocationResolverContext(org.springframework.boot.context.config.ConfigDataLocationResolverContext) ConsulProperties(org.springframework.cloud.consul.ConsulProperties) Test(org.junit.jupiter.api.Test)

Example 2 with ConsulProperties

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());
}
Also used : ConsulClient(com.ecwid.consul.v1.ConsulClient) ConsulProperties(org.springframework.cloud.consul.ConsulProperties) Before(org.junit.Before)

Example 3 with ConsulProperties

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

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;
}
Also used : Binder(org.springframework.boot.context.properties.bind.Binder) ConsulProperties(org.springframework.cloud.consul.ConsulProperties)

Aggregations

ConsulProperties (org.springframework.cloud.consul.ConsulProperties)4 Binder (org.springframework.boot.context.properties.bind.Binder)3 ConsulClient (com.ecwid.consul.v1.ConsulClient)2 Before (org.junit.Before)1 Test (org.junit.jupiter.api.Test)1 DefaultBootstrapContext (org.springframework.boot.DefaultBootstrapContext)1 ConfigDataLocationResolverContext (org.springframework.boot.context.config.ConfigDataLocationResolverContext)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 ConsulDiscoveryClient (org.springframework.cloud.consul.discovery.ConsulDiscoveryClient)1 ConsulDiscoveryProperties (org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties)1 MockEnvironment (org.springframework.mock.env.MockEnvironment)1