Search in sources :

Example 6 with GetValue

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

the class ConsulCommandService method getAllCommands.

/**
 * 获取所有指令
 *
 * @return
 */
@Override
public List<JSONObject> getAllCommands() {
    List<JSONObject> commands = new ArrayList<JSONObject>();
    Response<List<GetValue>> response = consulClient.getKVValues(ConsulConstants.CONSUL_MOTAN_COMMAND);
    List<GetValue> values = response.getValue();
    if (values != null) {
        for (GetValue value : values) {
            JSONObject node = new JSONObject();
            if (value.getValue() == null) {
                continue;
            }
            String group = value.getKey().substring(ConsulConstants.CONSUL_MOTAN_COMMAND.length());
            String command = new String(Base64.decodeBase64(value.getValue()));
            node.put("group", group);
            node.put("command", RpcCommandUtil.stringToCommand(command));
            commands.add(node);
        }
    }
    return commands;
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) GetValue(com.ecwid.consul.v1.kv.model.GetValue)

Example 7 with GetValue

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

the class FilteringAgentClient method getAliveAgents.

public List<Member> getAliveAgents() {
    List<Member> members = client.getAgentMembers().getValue();
    List<Member> liveMembers = new ArrayList<>(members.size());
    for (Member peer : members) {
        if (peer.getStatus() == ALIVE_STATUS) {
            liveMembers.add(peer);
        }
    }
    return liveMembers;
}
Also used : ArrayList(java.util.ArrayList) Member(com.ecwid.consul.v1.agent.model.Member)

Example 8 with GetValue

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

the class ServiceCheckServerListFilter method getFilteredListOfServers.

@Override
public List<Server> getFilteredListOfServers(List<Server> servers) {
    List<Server> okServers = new ArrayList<>(servers.size());
    for (Server server : servers) {
        String appName = server.getMetaInfo().getAppName();
        String instanceId = server.getMetaInfo().getInstanceId();
        // TODO: cache getHealthChecks? this is hit often
        List<Check> serviceChecks = client.getHealthChecksForService(appName, QueryParams.DEFAULT).getValue();
        boolean serviceOk = true;
        for (Check check : serviceChecks) {
            if (check.getServiceId().equals(instanceId) && check.getStatus() != Check.CheckStatus.PASSING) {
                serviceOk = false;
                // just need one to fail
                break;
            }
        }
        if (serviceOk) {
            okServers.add(server);
        }
    }
    return okServers;
}
Also used : Server(com.netflix.loadbalancer.Server) ArrayList(java.util.ArrayList) Check(com.ecwid.consul.v1.health.model.Check)

Example 9 with GetValue

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

the class ConsulPropertySource method parsePropertiesInKeyValueFormat.

/**
 * Parses the properties in key value style i.e., values are expected to be either a
 * sub key or a constant
 *
 * @param values
 */
protected void parsePropertiesInKeyValueFormat(List<GetValue> values) {
    if (values == null) {
        return;
    }
    for (GetValue getValue : values) {
        String key = getValue.getKey();
        if (!StringUtils.endsWithIgnoreCase(key, "/")) {
            key = key.replace(context, "").replace('/', '.');
            String value = getValue.getDecodedValue();
            properties.put(key, value);
        }
    }
}
Also used : Base64Utils.decodeFromString(org.springframework.util.Base64Utils.decodeFromString) GetValue(com.ecwid.consul.v1.kv.model.GetValue)

Example 10 with GetValue

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

the class ConsulPropertySourceLocator method locate.

@Override
@Retryable(interceptor = "consulRetryInterceptor")
public PropertySource<?> locate(Environment environment) {
    if (environment instanceof ConfigurableEnvironment) {
        ConfigurableEnvironment env = (ConfigurableEnvironment) environment;
        String appName = properties.getName();
        if (appName == null) {
            appName = env.getProperty("spring.application.name");
        }
        List<String> profiles = Arrays.asList(env.getActiveProfiles());
        String prefix = this.properties.getPrefix();
        List<String> suffixes = new ArrayList<>();
        if (this.properties.getFormat() != FILES) {
            suffixes.add("/");
        } else {
            suffixes.add(".yml");
            suffixes.add(".yaml");
            suffixes.add(".properties");
        }
        String defaultContext = getContext(prefix, this.properties.getDefaultContext());
        for (String suffix : suffixes) {
            this.contexts.add(defaultContext + suffix);
        }
        for (String suffix : suffixes) {
            addProfiles(this.contexts, defaultContext, profiles, suffix);
        }
        String baseContext = getContext(prefix, appName);
        for (String suffix : suffixes) {
            this.contexts.add(baseContext + suffix);
        }
        for (String suffix : suffixes) {
            addProfiles(this.contexts, baseContext, profiles, suffix);
        }
        Collections.reverse(this.contexts);
        CompositePropertySource composite = new CompositePropertySource("consul");
        for (String propertySourceContext : this.contexts) {
            try {
                ConsulPropertySource propertySource = null;
                if (this.properties.getFormat() == FILES) {
                    Response<GetValue> response = this.consul.getKVValue(propertySourceContext, this.properties.getAclToken());
                    addIndex(propertySourceContext, response.getConsulIndex());
                    if (response.getValue() != null) {
                        ConsulFilesPropertySource filesPropertySource = new ConsulFilesPropertySource(propertySourceContext, this.consul, this.properties);
                        filesPropertySource.init(response.getValue());
                        propertySource = filesPropertySource;
                    }
                } else {
                    propertySource = create(propertySourceContext, contextIndex);
                }
                if (propertySource != null) {
                    composite.addPropertySource(propertySource);
                }
            } catch (Exception e) {
                if (this.properties.isFailFast()) {
                    log.error("Fail fast is set and there was an error reading configuration from consul.");
                    ReflectionUtils.rethrowRuntimeException(e);
                } else {
                    log.warn("Unable to load consul config from " + propertySourceContext, e);
                }
            }
        }
        return composite;
    }
    return null;
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) CompositePropertySource(org.springframework.core.env.CompositePropertySource) ArrayList(java.util.ArrayList) GetValue(com.ecwid.consul.v1.kv.model.GetValue) Retryable(org.springframework.retry.annotation.Retryable)

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