Search in sources :

Example 6 with NewService

use of com.ecwid.consul.v1.agent.model.NewService in project motan by weibocom.

the class ConsulEcwidClient method registerService.

@Override
public void registerService(ConsulService service) {
    NewService newService = convertService(service);
    client.agentServiceRegister(newService);
}
Also used : NewService(com.ecwid.consul.v1.agent.model.NewService)

Example 7 with NewService

use of com.ecwid.consul.v1.agent.model.NewService in project motan by weibocom.

the class ConsulEcwidClient method lookupHealthService.

@Override
public ConsulResponse<List<ConsulService>> lookupHealthService(String serviceName, long lastConsulIndex) {
    QueryParams queryParams = new QueryParams(ConsulConstants.CONSUL_BLOCK_TIME_SECONDS, lastConsulIndex);
    Response<List<HealthService>> orgResponse = client.getHealthServices(serviceName, true, queryParams);
    ConsulResponse<List<ConsulService>> newResponse = null;
    if (orgResponse != null && orgResponse.getValue() != null && !orgResponse.getValue().isEmpty()) {
        List<HealthService> HealthServices = orgResponse.getValue();
        List<ConsulService> ConsulServcies = new ArrayList<ConsulService>(HealthServices.size());
        for (HealthService orgService : HealthServices) {
            try {
                ConsulService newService = convertToConsulService(orgService);
                ConsulServcies.add(newService);
            } catch (Exception e) {
                String servcieid = "null";
                if (orgService.getService() != null) {
                    servcieid = orgService.getService().getId();
                }
                LoggerUtil.error("convert consul service fail. org consulservice:" + servcieid, e);
            }
        }
        if (!ConsulServcies.isEmpty()) {
            newResponse = new ConsulResponse<List<ConsulService>>();
            newResponse.setValue(ConsulServcies);
            newResponse.setConsulIndex(orgResponse.getConsulIndex());
            newResponse.setConsulLastContact(orgResponse.getConsulLastContact());
            newResponse.setConsulKnownLeader(orgResponse.isConsulKnownLeader());
        }
    }
    return newResponse;
}
Also used : HealthService(com.ecwid.consul.v1.health.model.HealthService) ArrayList(java.util.ArrayList) QueryParams(com.ecwid.consul.v1.QueryParams) ArrayList(java.util.ArrayList) List(java.util.List) ConsulService(com.weibo.api.motan.registry.consul.ConsulService)

Example 8 with NewService

use of com.ecwid.consul.v1.agent.model.NewService 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 9 with NewService

use of com.ecwid.consul.v1.agent.model.NewService in project spring-cloud-consul by spring-cloud.

the class ConsulServerListTests method createService.

private NewService createService(String id, int port, List<String> tags) {
    NewService service = new NewService();
    service.setName(name);
    service.setId(name + id);
    service.setAddress("localhost");
    service.setPort(port);
    if (tags != null) {
        service.setTags(tags);
    }
    return service;
}
Also used : NewService(com.ecwid.consul.v1.agent.model.NewService)

Example 10 with NewService

use of com.ecwid.consul.v1.agent.model.NewService in project spring-cloud-consul by spring-cloud.

the class ConsulDiscoveryClientDefaultQueryTagTests method serviceForEnvironment.

private NewService serviceForEnvironment(String env, int port) {
    NewService service = new NewService();
    service.setAddress("localhost");
    service.setId(NAME + env);
    service.setName(NAME);
    service.setPort(port);
    service.setTags(Arrays.asList(env));
    return service;
}
Also used : NewService(com.ecwid.consul.v1.agent.model.NewService)

Aggregations

NewService (com.ecwid.consul.v1.agent.model.NewService)11 Test (org.junit.Test)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 ConsulClient (com.ecwid.consul.v1.ConsulClient)1 QueryParams (com.ecwid.consul.v1.QueryParams)1 HealthService (com.ecwid.consul.v1.health.model.HealthService)1 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)1 ConsulService (com.weibo.api.motan.registry.consul.ConsulService)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 InetUtils (org.springframework.cloud.commons.util.InetUtils)1 InetUtilsProperties (org.springframework.cloud.commons.util.InetUtilsProperties)1