Search in sources :

Example 6 with ConsulClient

use of com.ecwid.consul.v1.ConsulClient in project spring-cloud-consul by spring-cloud.

the class ConsulServerListTests method tagsWork.

@Test
public void tagsWork() {
    ConsulClient consul = new ConsulClient();
    NewService nonTagged = createService("NonTagged", 8080, null);
    String tag = "mytag";
    NewService tagged = createService("Tagged", 9080, Arrays.asList(tag));
    String zone = "myzone";
    NewService withZone = createService("WithZone", 10080, Arrays.asList("zone=" + zone));
    String group = "test";
    NewService withGroup = createService("WithGroup", 11080, Arrays.asList("group=" + group));
    try {
        consul.agentServiceRegister(nonTagged);
        consul.agentServiceRegister(tagged);
        consul.agentServiceRegister(withZone);
        consul.agentServiceRegister(withGroup);
        InetUtils inetUtils = new InetUtils(new InetUtilsProperties());
        DefaultClientConfigImpl config = new DefaultClientConfigImpl();
        config.setClientName(name);
        ConsulServerList serverList = new ConsulServerList(consul, new ConsulDiscoveryProperties(inetUtils));
        serverList.initWithNiwsConfig(config);
        List<ConsulServer> servers = serverList.getInitialListOfServers();
        assertThat("servers was wrong size", servers, hasSize(4));
        int serverWithZoneCount = 0;
        for (ConsulServer server : servers) {
            if (server.getMetadata().containsKey("zone")) {
                serverWithZoneCount++;
                assertThat("server was wrong zone", server.getZone(), is(zone));
            } else {
                assertThat("server was wrong zone", server.getZone(), is(ConsulServer.UNKNOWN_ZONE));
            }
        }
        assertThat("server was wrong zone", serverWithZoneCount, is(1));
        serverList = new ConsulServerList(consul, getProperties(name, tag, inetUtils));
        serverList.initWithNiwsConfig(config);
        servers = serverList.getInitialListOfServers();
        assertThat("servers was wrong size", servers, hasSize(1));
        ConsulServer server = servers.get(0);
        assertThat("server was wrong", server.getPort(), is(9080));
        // test server group
        serverList = new ConsulServerList(consul, getProperties(name, "group=" + group, inetUtils));
        serverList.initWithNiwsConfig(config);
        servers = serverList.getInitialListOfServers();
        assertThat("servers was wrong size", servers, hasSize(1));
        server = servers.get(0);
        assertThat("server was wrong", server.getPort(), is(11080));
        assertThat("server group was wrong", server.getMetaInfo().getServerGroup(), is(group));
    } finally {
        consul.agentServiceDeregister(nonTagged.getId());
        consul.agentServiceDeregister(tagged.getId());
        consul.agentServiceDeregister(withZone.getId());
        consul.agentServiceDeregister(withGroup.getId());
    }
}
Also used : ConsulClient(com.ecwid.consul.v1.ConsulClient) InetUtilsProperties(org.springframework.cloud.commons.util.InetUtilsProperties) NewService(com.ecwid.consul.v1.agent.model.NewService) InetUtils(org.springframework.cloud.commons.util.InetUtils) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) Test(org.junit.Test)

Example 7 with ConsulClient

use of com.ecwid.consul.v1.ConsulClient in project spring-cloud-consul by spring-cloud.

the class ConsulPropertySourceLocatorFilesTests method setup.

@Before
public void setup() {
    this.properties = new ConsulProperties();
    this.client = new ConsulClient(properties.getHost(), properties.getPort());
    this.client.setKVValue(ROOT + APPLICATION_YML, "foo: bar\nmy.baz: ${foo}");
    this.client.setKVValue(ROOT + APPLICATION_DEV_YML, "foo: bar-dev\nmy.baz: ${foo}");
    this.client.setKVValue(ROOT + "/master.ref", UUID.randomUUID().toString());
    this.client.setKVValue(ROOT + APP_NAME_PROPS, "foo: bar-app\nmy.baz: ${foo}");
    this.client.setKVValue(ROOT + APP_NAME_DEV_PROPS, "foo: bar-app-dev\nmy.baz: ${foo}");
    this.context = new SpringApplicationBuilder(Config.class).web(false).run("--spring.application.name=" + APP_NAME, "--spring.cloud.consul.config.prefix=" + ROOT, "--spring.cloud.consul.config.format=FILES", "--spring.profiles.active=dev", "spring.cloud.consul.config.watch.delay=1");
    this.client = context.getBean(ConsulClient.class);
    this.properties = context.getBean(ConsulProperties.class);
    this.environment = context.getEnvironment();
}
Also used : ConsulClient(com.ecwid.consul.v1.ConsulClient) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) ConsulProperties(org.springframework.cloud.consul.ConsulProperties) Before(org.junit.Before)

Example 8 with ConsulClient

use of com.ecwid.consul.v1.ConsulClient in project spring-cloud-consul by spring-cloud.

the class ConfigWatchTests method setupWatch.

private void setupWatch(ApplicationEventPublisher eventPublisher, GetValue getValue, String context, String aclToken) {
    ConsulClient consul = mock(ConsulClient.class);
    List<GetValue> getValues = null;
    if (getValue != null) {
        getValues = Arrays.asList(getValue);
    }
    Response<List<GetValue>> response = new Response<>(getValues, 1L, false, 1L);
    when(consul.getKVValues(eq(context), nullable(String.class), any(QueryParams.class))).thenReturn(response);
    if (StringUtils.hasText(aclToken)) {
        configProperties.setAclToken(aclToken);
    }
    LinkedHashMap<String, Long> initialIndexes = new LinkedHashMap<>();
    initialIndexes.put(context, 0L);
    ConfigWatch watch = new ConfigWatch(configProperties, consul, initialIndexes);
    watch.setApplicationEventPublisher(eventPublisher);
    watch.start();
    watch.watchConfigKeyValues();
}
Also used : Response(com.ecwid.consul.v1.Response) ConsulClient(com.ecwid.consul.v1.ConsulClient) List(java.util.List) QueryParams(com.ecwid.consul.v1.QueryParams) Matchers.anyString(org.mockito.Matchers.anyString) GetValue(com.ecwid.consul.v1.kv.model.GetValue) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with ConsulClient

use of com.ecwid.consul.v1.ConsulClient in project spring-cloud-consul by spring-cloud.

the class ConfigWatchTests method firstCallDoesNotPublishEvent.

@Test
public void firstCallDoesNotPublishEvent() {
    ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class);
    configProperties.setFormat(FILES);
    GetValue getValue = new GetValue();
    String context = "/config/app.yml";
    ConsulClient consul = mock(ConsulClient.class);
    List<GetValue> getValues = Collections.singletonList(getValue);
    Response<List<GetValue>> response = new Response<>(getValues, 1L, false, 1L);
    when(consul.getKVValues(eq(context), anyString(), any(QueryParams.class))).thenReturn(response);
    ConfigWatch watch = new ConfigWatch(configProperties, consul, new LinkedHashMap<String, Long>());
    watch.setApplicationEventPublisher(eventPublisher);
    watch.watchConfigKeyValues();
    verify(eventPublisher, times(0)).publishEvent(any(RefreshEvent.class));
}
Also used : RefreshEvent(org.springframework.cloud.endpoint.event.RefreshEvent) ConsulClient(com.ecwid.consul.v1.ConsulClient) Matchers.anyString(org.mockito.Matchers.anyString) Response(com.ecwid.consul.v1.Response) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) List(java.util.List) QueryParams(com.ecwid.consul.v1.QueryParams) GetValue(com.ecwid.consul.v1.kv.model.GetValue) Test(org.junit.Test)

Example 10 with ConsulClient

use of com.ecwid.consul.v1.ConsulClient 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)

Aggregations

ConsulClient (com.ecwid.consul.v1.ConsulClient)17 Before (org.junit.Before)5 ConsulProperties (org.springframework.cloud.consul.ConsulProperties)5 Test (org.junit.Test)4 ConsulRawClient (com.ecwid.consul.v1.ConsulRawClient)3 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)3 QueryParams (com.ecwid.consul.v1.QueryParams)2 Response (com.ecwid.consul.v1.Response)2 GetValue (com.ecwid.consul.v1.kv.model.GetValue)2 Ambari (com.sequenceiq.periscope.domain.Ambari)2 TlsConfiguration (com.sequenceiq.periscope.model.TlsConfiguration)2 List (java.util.List)2 Matchers.anyString (org.mockito.Matchers.anyString)2 RawResponse (com.ecwid.consul.transport.RawResponse)1 NewService (com.ecwid.consul.v1.agent.model.NewService)1 CatalogService (com.ecwid.consul.v1.catalog.model.CatalogService)1 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)1 PluginFailureException (com.sequenceiq.cloudbreak.service.cluster.PluginFailureException)1 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 Field (java.lang.reflect.Field)1