Search in sources :

Example 21 with Service

use of com.ecwid.consul.v1.health.model.HealthService.Service 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 22 with Service

use of com.ecwid.consul.v1.health.model.HealthService.Service 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 23 with Service

use of com.ecwid.consul.v1.health.model.HealthService.Service 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)

Example 24 with Service

use of com.ecwid.consul.v1.health.model.HealthService.Service in project spring-cloud-consul by spring-cloud.

the class ConsulServiceRegistry method getStatus.

@Override
public Object getStatus(ConsulRegistration registration) {
    String serviceId = registration.getServiceId();
    Response<List<Check>> response = client.getHealthChecksForService(serviceId, QueryParams.DEFAULT);
    List<Check> checks = response.getValue();
    for (Check check : checks) {
        if (check.getServiceId().equals(registration.getInstanceId())) {
            if (check.getName().equalsIgnoreCase("Service Maintenance Mode")) {
                return OUT_OF_SERVICE.getCode();
            }
        }
    }
    return UP.getCode();
}
Also used : Check(com.ecwid.consul.v1.health.model.Check) List(java.util.List)

Example 25 with Service

use of com.ecwid.consul.v1.health.model.HealthService.Service in project spring-cloud-consul by spring-cloud.

the class ConsulAutoRegistration method lifecycleRegistration.

// TODO: do I need this here, or should I just copy what I need back into lifecycle?
@Deprecated
public static ConsulAutoRegistration lifecycleRegistration(Integer port, String instanceId, AutoServiceRegistrationProperties autoServiceRegistrationProperties, ConsulDiscoveryProperties properties, ApplicationContext context, List<ConsulRegistrationCustomizer> registrationCustomizers, HeartbeatProperties heartbeatProperties) {
    NewService service = new NewService();
    String appName = getAppName(properties, context.getEnvironment());
    service.setId(instanceId);
    if (!properties.isPreferAgentAddress()) {
        service.setAddress(properties.getHostname());
    }
    service.setName(normalizeForDns(appName));
    service.setTags(createTags(properties));
    // If an alternate external port is specified, register using it instead
    if (properties.getPort() != null) {
        service.setPort(properties.getPort());
    } else {
        service.setPort(port);
    }
    Assert.notNull(service.getPort(), "service.port may not be null");
    setCheck(service, autoServiceRegistrationProperties, properties, context, heartbeatProperties);
    ConsulAutoRegistration registration = new ConsulAutoRegistration(service, autoServiceRegistrationProperties, properties, context, heartbeatProperties);
    customize(registrationCustomizers, registration);
    return registration;
}
Also used : NewService(com.ecwid.consul.v1.agent.model.NewService)

Aggregations

Test (org.junit.Test)18 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)18 Service (com.ecwid.consul.v1.agent.model.Service)14 Map (java.util.Map)14 NewService (com.ecwid.consul.v1.agent.model.NewService)10 Check (com.ecwid.consul.v1.health.model.Check)6 HealthService (com.ecwid.consul.v1.health.model.HealthService)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 QueryParams (com.ecwid.consul.v1.QueryParams)3 CatalogService (com.ecwid.consul.v1.catalog.model.CatalogService)2 ConsulService (com.weibo.api.motan.registry.consul.ConsulService)2 JSONObject (com.alibaba.fastjson.JSONObject)1 ConsulClient (com.ecwid.consul.v1.ConsulClient)1 Service (com.ecwid.consul.v1.health.model.HealthService.Service)1 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)1 DefaultServiceInstance (org.springframework.cloud.client.DefaultServiceInstance)1 ConsulServer (org.springframework.cloud.consul.discovery.ConsulServer)1 ConsulServerList (org.springframework.cloud.consul.discovery.ConsulServerList)1