Search in sources :

Example 11 with GetValue

use of com.ecwid.consul.v1.kv.model.GetValue 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)

Example 12 with GetValue

use of com.ecwid.consul.v1.kv.model.GetValue in project spring-cloud-consul by spring-cloud.

the class ConfigWatchTests method setupWatch.

private void setupWatch(ApplicationEventPublisher eventPublisher, GetValue getValue, String context, String aclToken) {
    ConsulClient consul = mock(ConsulClient.class);
    List<GetValue> getValues = null;
    if (getValue != null) {
        getValues = Arrays.asList(getValue);
    }
    Response<List<GetValue>> response = new Response<>(getValues, 1L, false, 1L);
    when(consul.getKVValues(eq(context), nullable(String.class), any(QueryParams.class))).thenReturn(response);
    if (StringUtils.hasText(aclToken)) {
        configProperties.setAclToken(aclToken);
    }
    LinkedHashMap<String, Long> initialIndexes = new LinkedHashMap<>();
    initialIndexes.put(context, 0L);
    ConfigWatch watch = new ConfigWatch(configProperties, consul, initialIndexes);
    watch.setApplicationEventPublisher(eventPublisher);
    watch.start();
    watch.watchConfigKeyValues();
}
Also used : Response(com.ecwid.consul.v1.Response) ConsulClient(com.ecwid.consul.v1.ConsulClient) List(java.util.List) QueryParams(com.ecwid.consul.v1.QueryParams) Matchers.anyString(org.mockito.Matchers.anyString) GetValue(com.ecwid.consul.v1.kv.model.GetValue) LinkedHashMap(java.util.LinkedHashMap)

Example 13 with GetValue

use of com.ecwid.consul.v1.kv.model.GetValue in project spring-cloud-consul by spring-cloud.

the class ConfigWatchTests method firstCallDoesNotPublishEvent.

@Test
public void firstCallDoesNotPublishEvent() {
    ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class);
    configProperties.setFormat(FILES);
    GetValue getValue = new GetValue();
    String context = "/config/app.yml";
    ConsulClient consul = mock(ConsulClient.class);
    List<GetValue> getValues = Collections.singletonList(getValue);
    Response<List<GetValue>> response = new Response<>(getValues, 1L, false, 1L);
    when(consul.getKVValues(eq(context), anyString(), any(QueryParams.class))).thenReturn(response);
    ConfigWatch watch = new ConfigWatch(configProperties, consul, new LinkedHashMap<String, Long>());
    watch.setApplicationEventPublisher(eventPublisher);
    watch.watchConfigKeyValues();
    verify(eventPublisher, times(0)).publishEvent(any(RefreshEvent.class));
}
Also used : RefreshEvent(org.springframework.cloud.endpoint.event.RefreshEvent) ConsulClient(com.ecwid.consul.v1.ConsulClient) Matchers.anyString(org.mockito.Matchers.anyString) Response(com.ecwid.consul.v1.Response) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) List(java.util.List) QueryParams(com.ecwid.consul.v1.QueryParams) GetValue(com.ecwid.consul.v1.kv.model.GetValue) Test(org.junit.Test)

Example 14 with GetValue

use of com.ecwid.consul.v1.kv.model.GetValue 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 15 with GetValue

use of com.ecwid.consul.v1.kv.model.GetValue in project motan by weibocom.

the class ConsulCommandService method getCommands.

/**
 * 获取指定group的指令列表
 *
 * @param groupName
 * @return
 */
@Override
public String getCommands(String groupName) {
    Response<GetValue> response = consulClient.getKVValue(ConsulConstants.CONSUL_MOTAN_COMMAND + groupName);
    GetValue value = response.getValue();
    String command = "";
    if (value != null && value.getValue() != null) {
        command = new String(Base64.decodeBase64(value.getValue()));
    }
    return command;
}
Also used : GetValue(com.ecwid.consul.v1.kv.model.GetValue)

Aggregations

GetValue (com.ecwid.consul.v1.kv.model.GetValue)10 QueryParams (com.ecwid.consul.v1.QueryParams)5 RefreshEvent (org.springframework.cloud.endpoint.event.RefreshEvent)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Test (org.junit.Test)4 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)4 JSONObject (com.alibaba.fastjson.JSONObject)2 ConsulClient (com.ecwid.consul.v1.ConsulClient)2 Response (com.ecwid.consul.v1.Response)2 Check (com.ecwid.consul.v1.health.model.Check)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Member (com.ecwid.consul.v1.agent.model.Member)1 Server (com.netflix.loadbalancer.Server)1 Timed (io.micrometer.core.annotation.Timed)1 LinkedHashMap (java.util.LinkedHashMap)1 CompositePropertySource (org.springframework.core.env.CompositePropertySource)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1 Retryable (org.springframework.retry.annotation.Retryable)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1