Search in sources :

Example 11 with QueryParams

use of com.recurly.v3.QueryParams in project incubator-shenyu by apache.

the class ShenyuConsulConfigWatch method watchConfigKeyValues.

private void watchConfigKeyValues() {
    if (this.running.get()) {
        for (String context : this.consulIndexes.keySet()) {
            try {
                Long currentIndex = this.consulIndexes.get(context);
                if (currentIndex == null) {
                    currentIndex = -1L;
                }
                Response<List<GetValue>> response = this.consul.getKVValues(context, null, new QueryParams(waitTime, currentIndex));
                if (response.getValue() != null && !response.getValue().isEmpty()) {
                    Long newIndex = response.getConsulIndex();
                    if (Objects.nonNull(newIndex) && !newIndex.equals(currentIndex)) {
                        if (!this.consulIndexes.containsValue(newIndex) && !currentIndex.equals(-1L)) {
                            LOGGER.trace("Context {} has new index {}", context, newIndex);
                            Map<String, GetValue> valueMap = extractGetValue(response);
                            publisher.publishEvent(new ConsulConfigChangedEvent(this, newIndex, valueMap));
                        } else if (LOGGER.isTraceEnabled()) {
                            LOGGER.info("Event for index already published for context {}", context);
                        }
                        this.consulIndexes.put(context, newIndex);
                    } else if (LOGGER.isTraceEnabled()) {
                        LOGGER.trace("Same index for context {}", context);
                    }
                } else if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("No value for context {}", context);
                }
            } catch (Exception e) {
                LOGGER.warn("Error querying consul Key/Values for context '{}'. Message: {}", context, e.getMessage());
            }
        }
    }
}
Also used : List(java.util.List) QueryParams(com.ecwid.consul.v1.QueryParams) GetValue(com.ecwid.consul.v1.kv.model.GetValue)

Example 12 with QueryParams

use of com.recurly.v3.QueryParams in project incubator-shenyu by apache.

the class ConsulSyncDataService method watchConfigKeyValues.

private void watchConfigKeyValues() {
    if (this.running.get()) {
        for (String context : this.consulIndexes.keySet()) {
            try {
                Long currentIndex = this.consulIndexes.get(context);
                if (Objects.isNull(currentIndex)) {
                    currentIndex = ConsulConstants.INIT_CONFIG_VERSION_INDEX;
                }
                Response<List<GetValue>> response = this.consulClient.getKVValues(context, null, new QueryParams(consulConfig.getWaitTime(), currentIndex));
                if (Objects.isNull(response.getValue()) || response.getValue().isEmpty()) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("No value for context " + context);
                    }
                    continue;
                }
                Long newIndex = response.getConsulIndex();
                if (Objects.isNull(newIndex) || Objects.equals(newIndex, currentIndex)) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Same index for context " + context);
                    }
                    continue;
                }
                if (!this.consulIndexes.containsValue(newIndex) && !currentIndex.equals(ConsulConstants.INIT_CONFIG_VERSION_INDEX)) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Context " + context + " has new index " + newIndex);
                    }
                    final Long lastIndex = currentIndex;
                    response.getValue().forEach(data -> {
                        if (data.getModifyIndex() == lastIndex) {
                            // data has not changed
                            return;
                        }
                        groupMap.get(data.getKey()).change(data.getDecodedValue());
                    });
                } else if (LOG.isTraceEnabled()) {
                    LOG.info("Event for index already published for context " + context);
                }
                this.consulIndexes.put(context, newIndex);
            } catch (Exception e) {
                LOG.warn("Error querying consul Key/Values for context '" + context + "'. Message: " + e.getMessage());
            }
        }
    }
}
Also used : List(java.util.List) QueryParams(com.ecwid.consul.v1.QueryParams)

Example 13 with QueryParams

use of com.recurly.v3.QueryParams in project recurly-client-java by recurly.

the class MockClient method listResources.

public Pager<MyResource> listResources(QueryParams queryParams) {
    final String url = "/resources";
    final HashMap<String, String> urlParams = new HashMap<String, String>();
    if (queryParams == null)
        queryParams = new QueryParams();
    final HashMap<String, Object> paramsMap = queryParams.getParams();
    final String path = this.interpolatePath(url, urlParams);
    Type parameterizedType = TypeToken.getParameterized(Pager.class, MyResource.class).getType();
    return new Pager<>(path, paramsMap, this, parameterizedType);
}
Also used : Type(java.lang.reflect.Type) MediaType(okhttp3.MediaType) HashMap(java.util.HashMap) Pager(com.recurly.v3.Pager) QueryParams(com.recurly.v3.QueryParams)

Example 14 with QueryParams

use of com.recurly.v3.QueryParams in project polaris-java by polarismesh.

the class ConsulAPIConnector method syncGetServiceInstances.

@Override
public List<DefaultInstance> syncGetServiceInstances(ServiceUpdateTask serviceUpdateTask) {
    List<DefaultInstance> instanceList = new ArrayList<>();
    try {
        HealthServicesRequest request = HealthServicesRequest.newBuilder().setQueryParams(new QueryParams(ConsistencyMode.DEFAULT)).build();
        Response<List<HealthService>> response = this.consulClient.getHealthServices(serviceUpdateTask.getServiceEventKey().getService(), request);
        if (response.getValue() == null || response.getValue().isEmpty()) {
            return Collections.emptyList();
        }
        for (HealthService service : response.getValue()) {
            DefaultInstance instance = new DefaultInstance();
            instance.setId(service.getService().getId());
            instance.setService(service.getService().getService());
            instance.setHost(service.getService().getAddress());
            instance.setPort(service.getService().getPort());
            instanceList.add(instance);
        }
    } catch (ConsulException e) {
        throw ServerErrorResponseException.build(ErrorCode.SERVER_USER_ERROR.ordinal(), String.format("Get service instances of %s sync failed.", serviceUpdateTask.getServiceEventKey().getServiceKey()));
    }
    return instanceList;
}
Also used : HealthServicesRequest(com.ecwid.consul.v1.health.HealthServicesRequest) ConsulException(com.ecwid.consul.ConsulException) HealthService(com.ecwid.consul.v1.health.model.HealthService) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) ArrayList(java.util.ArrayList) QueryParams(com.ecwid.consul.v1.QueryParams) ArrayList(java.util.ArrayList) List(java.util.List)

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