有兴趣的伙伴可以根据原理自行开发可视化管理应用统一管理配置中心。
实现后的效果:


0.定义远程配置规则
属性 = :diko:{/项目名/模块名/属性}1.加载本地配置文件
/**
* Created By Diko
* Time:2019/7/31 8:57
* E-mail:kedongyu@sina.cn
* Function:
*/
public class PropertiesLoader {
private final static String DUBBO_REGISTRY_ADDRESS = "dubbo.registry.address";
private final static String EXTEND_CONFIG_LIST = "spring.config.location";
/**
* 加载配置文件
* @param url 文件路径
* @return
* @throws IOException
*/
public static Properties loadProperties(String url) throws IOException {
Properties properties = loadFilePath(url);
if(properties == null){
return null;
}
properties.put("spring.config.name","extend");
String zkHost = String.valueOf(properties.get(DUBBO_REGISTRY_ADDRESS));
if(!StringUtils.isEmpty(zkHost)&&!zkHost.trim().equals("null")){
return DikoRetomeApplicationConfig.readRemoteParameter(properties,zkHost);
}
return properties;
}
/**
* 加载配置文件
* @param url 文件路径
* @return
* @throws IOException
*/
private static Properties loadFilePath(String url) throws IOException {
File configFile = new File(url);
if(configFile.exists()){
return loadFile(configFile);
}
return null;
}
/**
* 加载配置文件
* @param file 配置文件
* @return
* @throws IOException
*/
private static Properties loadFile(File file) throws IOException {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
Properties properties = new Properties();
properties.load(fileInputStream);
return properties;
} catch (IOException e) {
e.printStackTrace();
}
finally {
if ( fileInputStream != null) {
fileInputStream.close();
}
}
return null;
}
}2.获取远程参数
/**
* Created By Diko
* Time:2019/7/31 8:57
* E-mail:kedongyu@sina.cn
* Function:
*/
public class DikoRetomeApplicationConfig {
private final static String HEAD = ":diko:{";
private final static String TAIL = "}";
/**
* 读取远程参数
* @param properties
* @param zkHost
* @return
*/
public static Properties readRemoteParameter(Properties properties, String zkHost) {
CuratorFramework zkClient = null;
try {
zkClient = initZkClient(zkHost);
Set<Map.Entry<Object, Object>> set = properties.entrySet();
Properties parameters = new Properties();
for (Map.Entry<Object, Object> entry : set) {
String key = String.valueOf(entry.getKey());
String value = String.valueOf(entry.getValue()).trim();
if (value.contains(HEAD) && value.contains(TAIL)) {
int start = value.indexOf(HEAD);
int end = value.lastIndexOf(TAIL);
String path = value.substring(start + HEAD.length(), end);
if (zkClient.checkExists().forPath(path) == null){
throw new RuntimeException(path + '\t' + "该节点不存在!");
}
byte[] data = zkClient.getData().forPath(path);
String remoteValue = null;
if(data !=null){
remoteValue = new String(data, "UTF-8").trim();
}
parameters.put(key,remoteValue);
}
else{
parameters.setProperty(key,value);
}
}
return parameters;
} catch (Exception e) {
e.printStackTrace();
}
finally {
zkClient.close();
}
return null;
}
/**
* 获取远程zk客户端
* @param zkHost zk地址
* @return
*/
private static CuratorFramework initZkClient(String zkHost) {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework zkClient = CuratorFrameworkFactory.newClient(zkHost, retryPolicy);
zkClient.start();
return zkClient;
}
}
3.测试
/**
* Created By Diko
* Time:2019/7/31 8:57
* E-mail:kedongyu@sina.cn
* Function:
*/
public class RemoteApplication {
public static void main(String[] args) throws IOException {
String propertiesUrl = args[0];
Properties properties = PropertiesLoader.loadProperties(propertiesUrl);
SpringApplication application = new SpringApplication(RemoteApplication.class);
application.setDefaultProperties(properties);
application.run(args);
}
}
dubbo.registry.address = 106.14.226.75:2181
server.port = :diko:{/remote/load/server.port}
版权声明:本文为Kedongyu_原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。