!18 add: 新增Nacos配置中心支持

Merge pull request !18 from 文涛/dev
This commit is contained in:
Wendal 2019-03-07 14:55:21 +08:00
commit fc46ccc929
4 changed files with 152 additions and 1 deletions

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nutzboot-starter</artifactId>
<groupId>org.nutz</groupId>
<version>2.3.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nutzboot-starter-nacos-config-client</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<description>NutzBoot, micoservice base on Nutz</description>
<dependencies>
<dependency>
<version>0.9.0</version>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,125 @@
package org.nutz.boot.starter.nacos;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import org.nutz.boot.AppContext;
import org.nutz.boot.annotation.PropDoc;
import org.nutz.boot.starter.ServerFace;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.lang.stream.StringInputStream;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.Executor;
/**
* @author wentao
* @email wentao0291@gmail.com
* @date 2019-03-06 21:45
*/
@IocBean
public class NacosConfigureStarter implements ServerFace {
/**
* 获取日志对象
*/
private static final Log log = Logs.get();
/**
* Nacos配置项前缀
*/
protected static final String NACOS_PRE = "nacos.config.";
/**
* Nacos远程地址配置项
*/
@PropDoc(value = "Nacos远程地址")
public static final String NACOS_ADDR = NACOS_PRE + "addr";
/**
* Nacos Data ID 配置项
*/
@PropDoc(value = "Nacos Data ID")
public static final String NACOS_DATA_ID = NACOS_PRE + "data_id";
/**
* Nacos分组配置项
*/
@PropDoc(value = "Nacos分组")
public static final String NACOS_GROUP = NACOS_PRE + "group";
/**
* Nacos数据类型配置项用于识别使用哪种方式解析配置项
* 支持配置 json, properties, xml
*/
@PropDoc(value = "Nacos数据类型")
public static final String NACOS_DATA_TYPE = NACOS_PRE + "data_type";
@Inject
protected AppContext appContext;
@Override
public void start() throws Exception {
PropertiesProxy conf = appContext.getConf();
String serverAddr = conf.get(NACOS_ADDR);
String dataId = conf.get(NACOS_DATA_ID);
String group = conf.get(NACOS_GROUP);
String dataType = conf.get(NACOS_DATA_TYPE);
if(Strings.isBlank(serverAddr) || Strings.isBlank(dataId) || Strings.isBlank(group) || Strings.isBlank(dataType)) {
log.debugf("nacos server config is not found or incomplete, skip...");
} else {
Properties properties = new Properties();
properties.put("serverAddr", serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties);
String configInfo = configService.getConfig(dataId, group, 5000);
log.debugf("get nacos config%s", configInfo);
if (Strings.isNotBlank(configInfo)) {
setConfig(configInfo, dataType);
}
configService.addListener(dataId, group, new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
log.debugf("receive nacos server config%s", configInfo);
if (Strings.isNotBlank(configInfo)) {
setConfig(configInfo, dataType);
}
}
@Override
public Executor getExecutor() {
return null;
}
});
}
}
private void setConfig(String content, String contentType) {
PropertiesProxy conf = appContext.getConf();
if ("json".equals(contentType)) {
NutMap configMap = new NutMap(content);
conf.putAll(configMap);
} else if ("xml".equals(contentType)) {
Properties properties = new Properties();
try {
properties.loadFromXML(new StringInputStream(content));
for (Object key : properties.keySet()) {
conf.put(key.toString(), properties.get(key).toString());
}
} catch (IOException e) {
throw Lang.makeThrow("nacos config xml parse error!");
}
} else if ("properties".equals(contentType)) {
PropertiesProxy propertiesProxy = new PropertiesProxy(new StringInputStream(content));
conf.putAll(propertiesProxy);
} else {
throw Lang.makeThrow("nacos.config.data_type is not found or not recognizeonly json,xml and properties are support!");
}
}
}

View File

@ -0,0 +1 @@
org.nutz.boot.starter.NacosConfigureStarter

View File

@ -72,6 +72,7 @@
<module>nutzboot-starter-logback-exts</module>
<module>nutzboot-starter-sentinel-annotation</module>
<module>nutzboot-starter-tio-websocket</module>
<module>nutzboot-starter-nacos-config-client</module>
</modules>
<dependencies>
<dependency>