Search in sources :

Example 1 with QueryParams

use of com.ecwid.consul.v1.QueryParams in project motan by weibocom.

the class ConsulRegistryService method getNodes.

@Override
public List<JSONObject> getNodes(String dcGroup, String service, String nodeType) {
    List<JSONObject> results = new ArrayList<JSONObject>();
    List<Check> checks = consulClient.getHealthChecksForService(getGroupName(dcGroup), new QueryParams(getDcName(dcGroup))).getValue();
    for (Check check : checks) {
        String serviceId = check.getServiceId();
        String[] strings = serviceId.split("-");
        if (strings[1].equals(service)) {
            Check.CheckStatus status = check.getStatus();
            JSONObject node = new JSONObject();
            if (nodeType.equals(status.toString())) {
                node.put("host", strings[0]);
                node.put("info", null);
                results.add(node);
            }
        }
    }
    return results;
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) Check(com.ecwid.consul.v1.health.model.Check) QueryParams(com.ecwid.consul.v1.QueryParams)

Example 2 with QueryParams

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

the class EventService method watch.

public List<Event> watch(Long lastIndex) {
    // TODO: parameterized or configurable watch time
    long index = -1;
    if (lastIndex != null) {
        index = lastIndex;
    }
    int eventTimeout = 5;
    if (properties != null) {
        eventTimeout = properties.getEventTimeout();
    }
    Response<List<Event>> watch = consul.eventList(new QueryParams(eventTimeout, index));
    return filterEvents(readEvents(watch), lastIndex);
}
Also used : List(java.util.List) QueryParams(com.ecwid.consul.v1.QueryParams)

Example 3 with QueryParams

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

the class ConsulCatalogWatch method catalogServicesWatch.

@Scheduled(fixedDelayString = "${spring.cloud.consul.discovery.catalogServicesWatchDelay:30000}")
public void catalogServicesWatch() {
    try {
        long index = -1;
        if (catalogServicesIndex.get() != null) {
            index = catalogServicesIndex.get().longValue();
        }
        Response<Map<String, List<String>>> response = consul.getCatalogServices(new QueryParams(properties.getCatalogServicesWatchTimeout(), index));
        Long consulIndex = response.getConsulIndex();
        if (consulIndex != null) {
            catalogServicesIndex.set(BigInteger.valueOf(consulIndex));
        }
        log.trace("Received services update from consul: {}, index: {}", response.getValue(), consulIndex);
        publisher.publishEvent(new HeartbeatEvent(this, consulIndex));
    } catch (Exception e) {
        log.error("Error watching Consul CatalogServices", e);
    }
}
Also used : HeartbeatEvent(org.springframework.cloud.client.discovery.event.HeartbeatEvent) QueryParams(com.ecwid.consul.v1.QueryParams) Map(java.util.Map) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 4 with QueryParams

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

the class ConsulDiscoveryClient method addInstancesToList.

private void addInstancesToList(List<ServiceInstance> instances, String serviceId, QueryParams queryParams) {
    String aclToken = properties.getAclToken();
    Response<List<HealthService>> services;
    if (StringUtils.hasText(aclToken)) {
        services = client.getHealthServices(serviceId, this.properties.getDefaultQueryTag(), this.properties.isQueryPassing(), queryParams, aclToken);
    } else {
        services = client.getHealthServices(serviceId, this.properties.getDefaultQueryTag(), this.properties.isQueryPassing(), queryParams);
    }
    for (HealthService service : services.getValue()) {
        String host = findHost(service);
        instances.add(new DefaultServiceInstance(serviceId, host, service.getService().getPort(), false, getMetadata(service)));
    }
}
Also used : DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) HealthService(com.ecwid.consul.v1.health.model.HealthService) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with QueryParams

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

the class ConsulDiscoveryClientTests method getInstancesForServiceRespectsQueryParams.

@Test
public void getInstancesForServiceRespectsQueryParams() {
    Response<List<String>> catalogDatacenters = consulClient.getCatalogDatacenters();
    List<String> dataCenterList = catalogDatacenters.getValue();
    assertFalse("no data centers found", dataCenterList.isEmpty());
    List<ServiceInstance> instances = discoveryClient.getInstances("consul", new QueryParams(dataCenterList.get(0)));
    assertFalse("instances was empty", instances.isEmpty());
    ServiceInstance instance = instances.get(0);
    assertIpAddress(instance);
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance) List(java.util.List) QueryParams(com.ecwid.consul.v1.QueryParams) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

QueryParams (com.ecwid.consul.v1.QueryParams)7 List (java.util.List)5 HealthService (com.ecwid.consul.v1.health.model.HealthService)2 ArrayList (java.util.ArrayList)2 Scheduled (org.springframework.scheduling.annotation.Scheduled)2 JSONObject (com.alibaba.fastjson.JSONObject)1 Check (com.ecwid.consul.v1.health.model.Check)1 ConsulService (com.weibo.api.motan.registry.consul.ConsulService)1 Timed (io.micrometer.core.annotation.Timed)1 Map (java.util.Map)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 DefaultServiceInstance (org.springframework.cloud.client.DefaultServiceInstance)1 ServiceInstance (org.springframework.cloud.client.ServiceInstance)1 HeartbeatEvent (org.springframework.cloud.client.discovery.event.HeartbeatEvent)1 RefreshEvent (org.springframework.cloud.endpoint.event.RefreshEvent)1