Search in sources :

Example 1 with Config

use of org.noear.solon.cloud.model.Config in project solon by noear.

the class CloudClient method configLoad.

/**
 * 云端配置服务,加载默认配置
 */
@Note("云端配置服务,加载默认配置")
public static void configLoad(String group, String key) {
    if (CloudClient.config() == null) {
        return;
    }
    if (Utils.isNotEmpty(key)) {
        Config config = CloudClient.config().pull(group, key);
        if (config != null && Utils.isNotEmpty(config.value())) {
            Properties properties = config.toProps();
            Solon.cfg().loadAdd(properties);
        }
        // 关注实时更新
        CloudClient.config().attention(group, key, (cfg) -> {
            Properties properties = config.toProps();
            Solon.cfg().loadAdd(properties);
        });
    }
}
Also used : Config(org.noear.solon.cloud.model.Config) Properties(java.util.Properties) Note(org.noear.solon.annotation.Note)

Example 2 with Config

use of org.noear.solon.cloud.model.Config in project solon by noear.

the class CloudConfigServiceConsulImpl method run0.

private void run0() {
    Map<String, Config> cfgTmp = new HashMap<>();
    for (Map.Entry<CloudConfigHandler, CloudConfigObserverEntity> kv : observerMap.entrySet()) {
        CloudConfigObserverEntity entity = kv.getValue();
        String cfgKey = entity.group + "/" + entity.key;
        GetValue newV = real.getKVValue(cfgKey, token).getValue();
        if (newV != null) {
            Config oldV = configMap.get(cfgKey);
            if (oldV == null) {
                oldV = new Config(entity.group, entity.key, newV.getDecodedValue(), newV.getModifyIndex());
                configMap.put(cfgKey, oldV);
                cfgTmp.put(cfgKey, oldV);
            } else if (newV.getModifyIndex() > oldV.version()) {
                oldV.updateValue(newV.getDecodedValue(), newV.getModifyIndex());
                cfgTmp.put(cfgKey, oldV);
            }
        }
    }
    for (Config cfg2 : cfgTmp.values()) {
        observerMap.forEach((k, v) -> {
            if (cfg2.group().equals(v.group) && cfg2.key().equals(v.key)) {
                v.handle(cfg2);
            }
        });
    }
}
Also used : CloudConfigObserverEntity(org.noear.solon.cloud.service.CloudConfigObserverEntity) HashMap(java.util.HashMap) Config(org.noear.solon.cloud.model.Config) CloudConfigHandler(org.noear.solon.cloud.CloudConfigHandler) HashMap(java.util.HashMap) Map(java.util.Map) GetValue(com.ecwid.consul.v1.kv.model.GetValue)

Example 3 with Config

use of org.noear.solon.cloud.model.Config in project solon by noear.

the class CloudConfigServiceConsulImpl method pull.

/**
 * 获取配置
 */
@Override
public Config pull(String group, String key) {
    if (Utils.isEmpty(group)) {
        group = Solon.cfg().appGroup();
        if (Utils.isEmpty(group)) {
            group = DEFAULT_GROUP;
        }
    }
    String cfgKey = group + "/" + key;
    GetValue newV = real.getKVValue(cfgKey, token).getValue();
    if (newV != null) {
        Config oldV = configMap.get(cfgKey);
        if (oldV == null) {
            oldV = new Config(group, key, newV.getDecodedValue(), newV.getModifyIndex());
            configMap.put(cfgKey, oldV);
        } else if (newV.getModifyIndex() > oldV.version()) {
            oldV.updateValue(newV.getDecodedValue(), newV.getModifyIndex());
        }
        return oldV;
    } else {
        return null;
    }
}
Also used : Config(org.noear.solon.cloud.model.Config) GetValue(com.ecwid.consul.v1.kv.model.GetValue)

Example 4 with Config

use of org.noear.solon.cloud.model.Config in project solon by noear.

the class CloudConfigServiceNacosImp method attention.

/**
 * 关注配置
 */
@Override
public void attention(String group, String key, CloudConfigHandler observer) {
    if (observerMap.containsKey(observer)) {
        return;
    }
    if (Utils.isEmpty(group)) {
        group = Solon.cfg().appGroup();
    }
    CloudConfigObserverEntity entity = new CloudConfigObserverEntity(group, key, observer);
    observerMap.put(observer, entity);
    try {
        group = groupReview(group);
        real.addListener(key, group, new Listener() {

            @Override
            public Executor getExecutor() {
                return null;
            }

            @Override
            public void receiveConfigInfo(String value) {
                entity.handle(new Config(entity.group, entity.key, value, 0));
            }
        });
    } catch (NacosException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : CloudConfigObserverEntity(org.noear.solon.cloud.service.CloudConfigObserverEntity) Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor) Config(org.noear.solon.cloud.model.Config) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 5 with Config

use of org.noear.solon.cloud.model.Config in project solon by noear.

the class CloudConfigBeanBuilder method doBuild.

@Override
public void doBuild(Class<?> clz, BeanWrap bw, CloudConfig anno) throws Exception {
    if (CloudClient.config() == null) {
        throw new IllegalArgumentException("Missing CloudConfigService component");
    }
    CloudConfigHandler handler;
    if (bw.raw() instanceof CloudConfigHandler) {
        handler = bw.raw();
    } else {
        handler = (Config cfg) -> {
            Properties val0 = cfg.toProps();
            Utils.injectProperties(bw.raw(), val0);
        };
    }
    CloudManager.register(anno, handler);
    if (CloudClient.config() != null) {
        // 支持${xxx}配置
        String name = Solon.cfg().getByParse(Utils.annoAlias(anno.value(), anno.name()));
        // 支持${xxx}配置
        String group = Solon.cfg().getByParse(anno.group());
        Config config = CloudClient.config().pull(group, name);
        if (config != null) {
            handler.handle(config);
        }
        // 关注配置
        CloudClient.config().attention(group, name, handler);
    }
}
Also used : Config(org.noear.solon.cloud.model.Config) CloudConfig(org.noear.solon.cloud.annotation.CloudConfig) CloudConfigHandler(org.noear.solon.cloud.CloudConfigHandler) Properties(java.util.Properties)

Aggregations

Config (org.noear.solon.cloud.model.Config)8 GetValue (com.ecwid.consul.v1.kv.model.GetValue)2 Properties (java.util.Properties)2 CloudConfigHandler (org.noear.solon.cloud.CloudConfigHandler)2 CloudConfigObserverEntity (org.noear.solon.cloud.service.CloudConfigObserverEntity)2 Listener (com.alibaba.nacos.api.config.listener.Listener)1 NacosException (com.alibaba.nacos.api.exception.NacosException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Executor (java.util.concurrent.Executor)1 Note (org.noear.solon.annotation.Note)1 CloudConfig (org.noear.solon.cloud.annotation.CloudConfig)1 HandlerCacheUpdate (org.noear.solon.cloud.extend.water.integration.msg.HandlerCacheUpdate)1 HandlerConfigUpdate (org.noear.solon.cloud.extend.water.integration.msg.HandlerConfigUpdate)1 ConfigM (org.noear.water.model.ConfigM)1