Search in sources :

Example 6 with QueryParams

use of com.ecwid.consul.v1.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 7 with QueryParams

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

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

the class ConfigWatch method watchConfigKeyValues.

@Scheduled(fixedDelayString = "${spring.cloud.consul.config.watch.delay:1000}")
@Timed(value = "consul.watch-config-keys")
public void watchConfigKeyValues() {
    if (this.running.get()) {
        for (String context : this.consulIndexes.keySet()) {
            // turn the context into a Consul folder path (unless our config format are FILES)
            if (properties.getFormat() != FILES && !context.endsWith("/")) {
                context = context + "/";
            }
            try {
                Long currentIndex = this.consulIndexes.get(context);
                if (currentIndex == null) {
                    currentIndex = -1L;
                }
                // use the consul ACL token if found
                String aclToken = properties.getAclToken();
                if (StringUtils.isEmpty(aclToken)) {
                    aclToken = null;
                }
                Response<List<GetValue>> response = this.consul.getKVValues(context, aclToken, new QueryParams(this.properties.getWatch().getWaitTime(), currentIndex));
                // reducing churn if there wasn't anything
                if (response.getValue() != null && !response.getValue().isEmpty()) {
                    Long newIndex = response.getConsulIndex();
                    if (newIndex != null && !newIndex.equals(currentIndex)) {
                        // don't publish the same index again, don't publish the first time (-1) so index can be primed
                        if (!this.consulIndexes.containsValue(newIndex) && !currentIndex.equals(-1L)) {
                            RefreshEventData data = new RefreshEventData(context, currentIndex, newIndex);
                            this.publisher.publishEvent(new RefreshEvent(this, data, data.toString()));
                        }
                        this.consulIndexes.put(context, newIndex);
                    }
                }
            } catch (Exception e) {
                // only fail fast on the initial query, otherwise just log the error
                if (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());
                }
            }
        }
    }
    firstTime = false;
}
Also used : RefreshEvent(org.springframework.cloud.endpoint.event.RefreshEvent) List(java.util.List) QueryParams(com.ecwid.consul.v1.QueryParams) Scheduled(org.springframework.scheduling.annotation.Scheduled) Timed(io.micrometer.core.annotation.Timed)

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