com.github.javaclub.toolbox.conf.NacosConfigLoader Maven / Gradle / Ivy
/*
* @(#)NacosConfigLoader.java 2024-01-16 20:26:27
*
* Copyright (c) 2024 - 2099. All Rights Reserved.
*
*/
package com.github.javaclub.toolbox.conf;
import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Executor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.github.javaclub.toolbox.Entry;
import com.github.javaclub.toolbox.ToolBox.Collections;
import com.github.javaclub.toolbox.ToolBox.IO;
import com.github.javaclub.toolbox.ToolBox.Strings;
import com.github.javaclub.toolbox.conf.yml.YmlConfigFile;
import com.google.common.collect.Lists;
/**
* NacosConfigLoader
*
* @author Gerald Chen
* @version $Id: NacosConfigLoader.java 2024-01-16 20:26:27 Exp $
*/
public class NacosConfigLoader {
private static final Logger log = LoggerFactory.getLogger(NacosConfigLoader.class);
private static class SingletonHolder {
private static final NacosConfigLoader INSTANCE = new NacosConfigLoader();
}
static final String NACOS_GROUP_DEFAULT = "DEFAULT_GROUP";
private volatile String nacosGroup = NACOS_GROUP_DEFAULT;
private volatile String nacosAddr;
private volatile List> nacosDataIdArray;
private NacosConfigLoader() {
}
public static final NacosConfigLoader getInstance() {
return SingletonHolder.INSTANCE;
}
public void setNacosConfig(String nacosAddr, String group) {
this.nacosAddr = nacosAddr;
this.nacosGroup = group;
}
public void setNacosConfig(String nacosAddr, Entry ... configs) {
this.nacosAddr = nacosAddr;
this.nacosDataIdArray = Lists.newArrayList(configs);
}
public void setNacosAddr(String nacosAddr) {
this.nacosAddr = nacosAddr;
}
public void setNacosGroup(String group) {
this.nacosGroup = group;
}
public String getNacosGroup() {
return nacosGroup;
}
public String getNacosAddr() {
return nacosAddr;
}
public Properties loadConfigs() {
if (Strings.isBlank(nacosAddr)) {
log.warn("Nacos serverAddr is empty!!! => ignore NacosConfig");
return null;
}
if (Collections.isEmpty(nacosDataIdArray)) {
log.warn("No Nacos dataIds!!! => ignore NacosConfig");
return null;
}
log.info("nacosAddr={}, nacosDataIdArray={}", nacosAddr, nacosDataIdArray);
final Properties loaded = new Properties();
String group = this.getNacosGroup();
Properties mata = new Properties();
mata.put(PropertyKeyConst.SERVER_ADDR, nacosAddr);
for (Entry entry : nacosDataIdArray) {
String dataId = entry.getKey();
try {
ConfigService configService = NacosFactory.createConfigService(mata);
String content = configService.getConfig(dataId, group, 6000);
if (Strings.isNotBlank(content)) {
Properties props = this.parseConfigType(content, entry.getValue());
if (null != props) {
loaded.putAll(props);
}
}
configService.addListener(dataId, group, new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
Properties p = parseConfigType(configInfo, entry.getValue());
if (null != p && p.size() > 0) {
CompositeAppConfigProperties.getInstance().merge(p);
log.warn("NacosConfig {} changed, {} configKeys merged", dataId, p.size());
}
}
@Override
public Executor getExecutor() {
return null;
}
});
} catch (NacosException e) {
log.error("nacosAddr={}" + nacosAddr, e);
}
}
return loaded;
}
Properties parseConfigType(String content, ConfigType type) {
switch (type) {
case YAML:
YmlConfigFile ymlConfigFile = new YmlConfigFile(content);
return ymlConfigFile.asProperties();
case PROPERTIES:
ByteArrayInputStream input = null;
try {
input = new ByteArrayInputStream(content.getBytes());
Properties p = new Properties();
p.load(input);
return p;
} catch (Exception e) {
// TODO: handle exception
} finally {
IO.closeQuietly(input);
}
default:
break;
}
return null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy