Search in sources :

Example 6 with QueryParams

use of com.recurly.v3.QueryParams in project spring-cloud-consul by spring-cloud.

the class ConsulCatalogWatch method catalogServicesWatch.

@Timed("consul.watch-catalog-services")
public void catalogServicesWatch() {
    try {
        long index = -1;
        if (this.catalogServicesIndex.get() != null) {
            index = this.catalogServicesIndex.get().longValue();
        }
        CatalogServicesRequest request = CatalogServicesRequest.newBuilder().setQueryParams(new QueryParams(this.properties.getCatalogServicesWatchTimeout(), index)).setToken(this.properties.getAclToken()).build();
        Response<Map<String, List<String>>> response = this.consul.getCatalogServices(request);
        Long consulIndex = response.getConsulIndex();
        if (consulIndex != null) {
            this.catalogServicesIndex.set(BigInteger.valueOf(consulIndex));
        }
        if (log.isTraceEnabled()) {
            log.trace("Received services update from consul: " + response.getValue() + ", index: " + consulIndex);
        }
        this.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) CatalogServicesRequest(com.ecwid.consul.v1.catalog.CatalogServicesRequest) QueryParams(com.ecwid.consul.v1.QueryParams) Map(java.util.Map) Timed(io.micrometer.core.annotation.Timed)

Example 7 with QueryParams

use of com.recurly.v3.QueryParams in project spring-cloud-consul by spring-cloud.

the class ConsulDiscoveryClientTests method getInstancesForServiceRespectsQueryParams.

@Test
public void getInstancesForServiceRespectsQueryParams() {
    Response<List<String>> catalogDatacenters = this.consulClient.getCatalogDatacenters();
    List<String> dataCenterList = catalogDatacenters.getValue();
    assertThat(dataCenterList.isEmpty()).as("no data centers found").isFalse();
    List<ServiceInstance> instances = this.discoveryClient.getInstances("testConsulDiscovery", new QueryParams(dataCenterList.get(0)));
    assertThat(instances.isEmpty()).as("instances was empty").isFalse();
    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)

Example 8 with QueryParams

use of com.recurly.v3.QueryParams in project motan by weibocom.

the class ConsulRegistryService method getGroups.

@Override
public List<String> getGroups() {
    List<String> groups = new ArrayList<String>();
    for (String dc : getDatacenters()) {
        QueryParams queryParams = new QueryParams(dc);
        Map<String, List<String>> serviceMap = consulClient.getCatalogServices(queryParams).getValue();
        serviceMap.remove("consul");
        for (String service : serviceMap.keySet()) {
            groups.add(formatGroupName(dc, service));
        }
    }
    return groups;
}
Also used : QueryParams(com.ecwid.consul.v1.QueryParams)

Example 9 with QueryParams

use of com.recurly.v3.QueryParams 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> ConsulServices = new ArrayList<ConsulService>(HealthServices.size());
        for (HealthService orgService : HealthServices) {
            try {
                ConsulService newService = convertToConsulService(orgService);
                ConsulServices.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 (!ConsulServices.isEmpty()) {
            newResponse = new ConsulResponse<List<ConsulService>>();
            newResponse.setValue(ConsulServices);
            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 10 with QueryParams

use of com.recurly.v3.QueryParams in project spring-cloud-consul by spring-cloud.

the class ConfigWatch method watchConfigKeyValues.

@Timed("consul.watch-config-keys")
public void watchConfigKeyValues() {
    if (!this.running.get()) {
        return;
    }
    for (String context : this.consulIndexes.keySet()) {
        // are FILES)
        if (this.properties.getFormat() != FILES && !context.endsWith("/")) {
            context = context + "/";
        }
        try {
            Long currentIndex = this.consulIndexes.get(context);
            if (currentIndex == null) {
                currentIndex = -1L;
            }
            if (log.isTraceEnabled()) {
                log.trace("watching consul for context '" + context + "' with index " + currentIndex);
            }
            // use the consul ACL token if found
            String aclToken = this.properties.getAclToken();
            if (StringUtils.isEmpty(aclToken)) {
                aclToken = null;
            }
            Response<List<GetValue>> response = this.consul.getKVValues(context, aclToken, new QueryParams(this.properties.getWatch().getWaitTime(), currentIndex));
            // 200, reducing churn if there wasn't anything
            if (response.getValue() != null && !response.getValue().isEmpty()) {
                Long newIndex = response.getConsulIndex();
                if (newIndex != null && !newIndex.equals(currentIndex)) {
                    // time (-1) so index can be primed
                    if (!this.consulIndexes.containsValue(newIndex) && !currentIndex.equals(-1L)) {
                        if (log.isTraceEnabled()) {
                            log.trace("Context " + context + " has new index " + newIndex);
                        }
                        RefreshEventData data = new RefreshEventData(context, currentIndex, newIndex);
                        this.publisher.publishEvent(new RefreshEvent(this, data, data.toString()));
                    } else if (log.isTraceEnabled()) {
                        log.trace("Event for index already published for context " + context);
                    }
                    this.consulIndexes.put(context, newIndex);
                } else if (log.isTraceEnabled()) {
                    log.trace("Same index for context " + context);
                }
            } else if (log.isTraceEnabled()) {
                log.trace("No value for context " + context);
            }
        } catch (Exception e) {
            // only fail fast on the initial query, otherwise just log the error
            if (this.firstTime && this.properties.isFailFast()) {
                log.error("Fail fast is set and there was an error reading configuration from consul.");
                ReflectionUtils.rethrowRuntimeException(e);
            } else if (log.isTraceEnabled()) {
                log.trace("Error querying consul Key/Values for context '" + context + "'", e);
            } else if (log.isWarnEnabled()) {
                // simplified one line log message in the event of an agent
                // failure
                log.warn("Error querying consul Key/Values for context '" + context + "'. Message: " + e.getMessage());
            }
        }
    }
    this.firstTime = false;
}
Also used : RefreshEvent(org.springframework.cloud.endpoint.event.RefreshEvent) List(java.util.List) QueryParams(com.ecwid.consul.v1.QueryParams) Timed(io.micrometer.core.annotation.Timed)

Aggregations

QueryParams (com.ecwid.consul.v1.QueryParams)13 List (java.util.List)9 HealthServicesRequest (com.ecwid.consul.v1.health.HealthServicesRequest)3 ArrayList (java.util.ArrayList)3 HealthService (com.ecwid.consul.v1.health.model.HealthService)2 Timed (io.micrometer.core.annotation.Timed)2 JSONObject (com.alibaba.fastjson.JSONObject)1 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)1 ConsulException (com.ecwid.consul.ConsulException)1 CatalogServicesRequest (com.ecwid.consul.v1.catalog.CatalogServicesRequest)1 Check (com.ecwid.consul.v1.health.model.Check)1 GetValue (com.ecwid.consul.v1.kv.model.GetValue)1 Pager (com.recurly.v3.Pager)1 QueryParams (com.recurly.v3.QueryParams)1 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)1 ConsulService (com.weibo.api.motan.registry.consul.ConsulService)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MediaType (okhttp3.MediaType)1