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);
});
}
}
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);
}
});
}
}
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;
}
}
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);
}
}
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);
}
}
Aggregations