update: nutz/nutzwx 应使用快照版 & nutzboot-starter-nacos-config-client 更名为 starter-nacos-config 并新增 nutzboot-starter-nacos-discovery

This commit is contained in:
Wizzercn 2020-01-08 17:41:58 +08:00
parent e5c6881611
commit 44d06f3f14
11 changed files with 301 additions and 116 deletions

View File

@ -1,109 +0,0 @@
package org.nutz.boot.starter.nacos;
import java.io.IOException;
import java.util.Properties;
import org.nutz.boot.AppContext;
import org.nutz.boot.annotation.PropDoc;
import org.nutz.boot.config.impl.AbstractConfigureLoader;
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 com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
/**
* @author wentao
* @author wendal
* @email wentao0291@gmail.com
* @email wendal1985@gmail.com
* @date 2019-03-06 21:45
*/
@IocBean
public class NacosConfigureLoader extends AbstractConfigureLoader {
/**
* 获取日志对象
*/
private static final Log log = Logs.get();
/**
* Nacos配置项前缀
*/
protected static final String NACOS_PRE = "nacos.config.";
/**
* Nacos远程地址配置项
*/
@PropDoc(value = "Nacos远程地址", defaultValue="127.0.0.1:8848")
public static final String NACOS_ADDR = NACOS_PRE + "addr";
/**
* Nacos Data ID 配置项
*/
@PropDoc(value = "Nacos Data ID", defaultValue="nutzboot")
public static final String NACOS_DATA_ID = NACOS_PRE + "data_id";
/**
* Nacos分组配置项
*/
@PropDoc(value = "Nacos分组", defaultValue="DEFAULT_GROUP")
public static final String NACOS_GROUP = NACOS_PRE + "group";
/**
* Nacos数据类型配置项用于识别使用哪种方式解析配置项 支持配置 json, properties, xml
*/
@PropDoc(value = "Nacos数据类型", defaultValue="properties")
public static final String NACOS_DATA_TYPE = NACOS_PRE + "data_type";
@Inject
protected AppContext appContext;
@Override
public PropertiesProxy get() {
return conf;
}
private void setConfig(String content, String contentType, PropertiesProxy conf) {
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) || "txt".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!");
}
}
@Override
public void init() throws Exception {
conf.setPaths("application.properties");
String serverAddr = conf.get(NACOS_ADDR, "127.0.0.1:8848");
String dataId = conf.get(NACOS_DATA_ID, "nutzboot");
String group = conf.get(NACOS_GROUP, "DEFAULT_GROUP");
String dataType = conf.get(NACOS_DATA_TYPE, "properties");
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, conf);
}
}
}

View File

@ -8,7 +8,7 @@
<version>2.3.9-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nutzboot-starter-nacos-config-client</artifactId>
<artifactId>nutzboot-starter-nacos-config</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
@ -17,7 +17,6 @@
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>

View File

@ -0,0 +1,163 @@
package org.nutz.boot.starter.nacos;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import org.nutz.boot.AppContext;
import org.nutz.boot.annotation.PropDoc;
import org.nutz.boot.config.impl.AbstractConfigureLoader;
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 static com.alibaba.nacos.api.PropertyKeyConst.*;
/**
* @author wentao
* @author wendal
* @author wizzer
* @email wentao0291@gmail.com
* @email wendal1985@gmail.com
* @email wizzer.cn@gmail.com
* @date 2019-03-06 21:45
*/
@IocBean
public class NacosConfigureLoader extends AbstractConfigureLoader {
/**
* 获取日志对象
*/
private static final Log log = Logs.get();
/**
* Nacos配置项前缀
*/
protected static final String NACOS_PRE = "nacos.config.";
/**
* Nacos远程地址配置项
*/
@PropDoc(value = "Nacos远程地址", defaultValue = "127.0.0.1:8848")
public static final String NACOS_ADDR = NACOS_PRE + "server-addr";
/**
* Nacos Data ID 配置项
*/
@PropDoc(value = "Nacos Data ID", defaultValue = "nutzboot")
public static final String NACOS_DATA_ID = NACOS_PRE + "data-id";
/**
* Nacos分组配置项
*/
@PropDoc(value = "Nacos分组", defaultValue = "DEFAULT_GROUP")
public static final String NACOS_GROUP = NACOS_PRE + "group";
/**
* Nacos数据类型配置项用于识别使用哪种方式解析配置项 支持配置 json, properties, xml
*/
@PropDoc(value = "Nacos 数据类型", defaultValue = "properties")
public static final String NACOS_DATA_TYPE = NACOS_PRE + "data-type";
@PropDoc(value = "Nacos 编码方式", defaultValue = "")
public static final String NACOS_ENCODE = NACOS_PRE + "encode";
@PropDoc(value = "Nacos 命名空间ID", defaultValue = "")
public static final String NACOS_NAMESPACE = NACOS_PRE + "namespace";
@PropDoc(value = "Nacos AccessKey", defaultValue = "")
public static final String NACOS_ACCESS_KEY = NACOS_PRE + "access-key";
@PropDoc(value = "Nacos SecretKey", defaultValue = "")
public static final String NACOS_SECRET_KEY = NACOS_PRE + "secret-key";
@PropDoc(value = "Nacos ContextPath", defaultValue = "")
public static final String NACOS_CONTEXT_PATH = NACOS_PRE + "context-path";
@PropDoc(value = "Nacos 集群名称", defaultValue = "")
public static final String NACOS_CLUSTER_NAME = NACOS_PRE + "cluster-name";
@PropDoc(value = "Nacos 最大重试次数", defaultValue = "")
public static final String NACOS_MAX_RETRY = NACOS_PRE + "max-retry";
@PropDoc(value = "Nacos 配置监听长轮询超时时间", defaultValue = "")
public static final String NACOS_CONFIG_LONG_POLL_TIMEOUT = NACOS_PRE + "config-long-poll-timeout";
@PropDoc(value = "Nacos 配置重试时间", defaultValue = "properties")
public static final String NACOS_CONFIG_RETRY_TIME = NACOS_PRE + "config-retry-time";
@PropDoc(value = "Nacos 启动时拉取配置", defaultValue = "false")
public static final String NACOS_ENABLE_REMOTE_SYNC_CONFIG = NACOS_PRE + "enable-remote-sync-config";
@PropDoc(value = "Nacos Endpoint", defaultValue = "properties")
public static final String NACOS_ENCODE_ENDPOINT = NACOS_PRE + "endpoint";
@Inject
protected AppContext appContext;
@Override
public PropertiesProxy get() {
return conf;
}
private void setConfig(String content, String contentType, PropertiesProxy conf) {
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) || "txt".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!");
}
}
@Override
public void init() throws Exception {
String dataId = conf.get(NACOS_DATA_ID, conf.get("nutz.application.name", "nutzboot"));
String group = conf.get(NACOS_GROUP, "DEFAULT_GROUP");
String dataType = conf.get(NACOS_DATA_TYPE, "properties");
ConfigService configService = NacosFactory.createConfigService(getNacosConfigProperties());
String configInfo = configService.getConfig(dataId, group, 5000);
log.debugf("get nacos config%s", configInfo);
if (Strings.isNotBlank(configInfo)) {
setConfig(configInfo, dataType, conf);
}
}
public Properties getNacosConfigProperties() {
Properties properties = new Properties();
properties.put(SERVER_ADDR, conf.get(NACOS_ADDR, "127.0.0.1:8848"));
properties.put(ENCODE, conf.get(NACOS_ENCODE, ""));
properties.put(NAMESPACE, conf.get(NACOS_NAMESPACE, ""));
properties.put(ACCESS_KEY, conf.get(NACOS_ACCESS_KEY, ""));
properties.put(SECRET_KEY, conf.get(NACOS_SECRET_KEY, ""));
properties.put(CONTEXT_PATH, conf.get(NACOS_CONTEXT_PATH, ""));
properties.put(CLUSTER_NAME, conf.get(NACOS_CLUSTER_NAME, ""));
properties.put(MAX_RETRY, conf.get(NACOS_MAX_RETRY, ""));
properties.put(CONFIG_LONG_POLL_TIMEOUT,
conf.get(NACOS_CONFIG_LONG_POLL_TIMEOUT, ""));
properties.put(CONFIG_RETRY_TIME, conf.get(NACOS_CONFIG_RETRY_TIME, ""));
properties.put(ENABLE_REMOTE_SYNC_CONFIG, conf.get(NACOS_ENABLE_REMOTE_SYNC_CONFIG, ""));
String endpoint = conf.get(NACOS_ENCODE_ENDPOINT, "");
if (endpoint.contains(":")) {
int index = endpoint.indexOf(":");
properties.put(ENDPOINT, endpoint.substring(0, index));
properties.put(ENDPOINT_PORT, endpoint.substring(index + 1));
} else {
properties.put(ENDPOINT, endpoint);
}
return properties;
}
}

View File

@ -0,0 +1,23 @@
<?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.9-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nutzboot-starter-nacos-discovery</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<description>NutzBoot, micoservice base on Nutz</description>
<dependencies>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,95 @@
package org.nutz.boot.starter.nacos;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import org.nutz.boot.AppContext;
import org.nutz.boot.annotation.PropDoc;
import org.nutz.boot.config.impl.AbstractConfigureLoader;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import java.util.Properties;
import static com.alibaba.nacos.api.PropertyKeyConst.*;
/**
* @author wizzer(wizzer.cn)
* @date 2020/1/8
*/
@IocBean
public class NacosDiscoveryLoader extends AbstractConfigureLoader {
/**
* 获取日志对象
*/
private static final Log log = Logs.get();
/**
* Nacos配置项前缀
*/
protected static final String NACOS_PRE = "nacos.discovery.";
/**
* Nacos远程地址配置项
*/
@PropDoc(value = "Nacos 远程地址", defaultValue = "127.0.0.1:8848")
public static final String NACOS_ADDR = NACOS_PRE + "server-addr";
@PropDoc(value = "Nacos 命名空间ID", defaultValue = "")
public static final String NACOS_NAMESPACE = NACOS_PRE + "namespace";
@PropDoc(value = "Nacos 日志文件名", defaultValue = "")
public static final String NACOS_LOG_FILENAME = NACOS_PRE + "log-filename";
@PropDoc(value = "Nacos 日志等级", defaultValue = "")
public static final String NACOS_LOG_LEVEL = NACOS_PRE + "log-level";
@PropDoc(value = "Nacos Endpoint", defaultValue = "")
public static final String NACOS_ENCODE_ENDPOINT = NACOS_PRE + "endpoint";
@PropDoc(value = "Nacos AccessKey", defaultValue = "")
public static final String NACOS_ACCESS_KEY = NACOS_PRE + "access-key";
@PropDoc(value = "Nacos SecretKey", defaultValue = "")
public static final String NACOS_SECRET_KEY = NACOS_PRE + "secret-key";
@PropDoc(value = "Nacos 集群名称", defaultValue = "")
public static final String NACOS_CLUSTER_NAME = NACOS_PRE + "cluster-name";
@PropDoc(value = "Nacos 启动时加载缓存", defaultValue = "false")
public static final String NACOS_NAMING_LOAD_CACHE_AT_START = NACOS_PRE + "naming-load-cache-at-start";
@Inject
protected AppContext appContext;
@Override
public PropertiesProxy get() {
return conf;
}
@Override
public void init() throws Exception {
NacosFactory.createNamingService(getNacosDiscoveryProperties());
}
public Properties getNacosDiscoveryProperties() {
Properties properties = new Properties();
properties.put(SERVER_ADDR, conf.get(NACOS_ADDR, ""));
properties.put(NAMESPACE, conf.get(NACOS_NAMESPACE, ""));
properties.put(UtilAndComs.NACOS_NAMING_LOG_NAME, conf.get(NACOS_LOG_FILENAME, ""));
properties.put(UtilAndComs.NACOS_NAMING_LOG_LEVEL, conf.get(NACOS_LOG_LEVEL, ""));
String endpoint = conf.get(NACOS_ENCODE_ENDPOINT, "");
if (endpoint.contains(":")) {
int index = endpoint.indexOf(":");
properties.put(ENDPOINT, endpoint.substring(0, index));
properties.put(ENDPOINT_PORT, endpoint.substring(index + 1));
} else {
properties.put(ENDPOINT, endpoint);
}
properties.put(ACCESS_KEY, conf.get(NACOS_ACCESS_KEY, ""));
properties.put(SECRET_KEY, conf.get(NACOS_SECRET_KEY, ""));
properties.put(CLUSTER_NAME, conf.get(NACOS_CLUSTER_NAME, ""));
properties.put(NAMING_LOAD_CACHE_AT_START, conf.get(NACOS_NAMING_LOAD_CACHE_AT_START, "false"));
return properties;
}
}

View File

@ -0,0 +1 @@
org.nutz.boot.starter.nacos.NacosDiscoveryLoader

View File

@ -0,0 +1 @@
org.nutz.boot.starter.nacos.NacosDiscoveryLoader

View File

@ -72,7 +72,8 @@
<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>
<module>nutzboot-starter-nacos-config</module>
<module>nutzboot-starter-nacos-discovery</module>
<module>nutzboot-starter-ftp</module>
<module>nutzboot-starter-fastdfs</module>
<module>nutzboot-starter-seata</module>

19
pom.xml
View File

@ -8,9 +8,9 @@
<name>nutzboot-parent</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<nutz.version>1.r.68.v20191031</nutz.version>
<nutz.version>1.r.68-SNAPSHOT</nutz.version>
<nutzboot.version>2.3.9-SNAPSHOT</nutzboot.version>
<nutzwx.version>1.r.68.v20191031</nutzwx.version>
<nutzwx.version>1.r.68-SNAPSHOT</nutzwx.version>
<slf4j.version>1.7.28</slf4j.version>
<slf4j-api.version>1.7.28</slf4j-api.version>
<logback.version>1.2.3</logback.version>
@ -50,6 +50,7 @@
<seata.version>0.9.0</seata.version>
<redisson.version>3.11.4</redisson.version>
<mybatis.version>3.5.2</mybatis.version>
<nacos.version>1.1.4</nacos.version>
</properties>
<description>NutzBoot, micoservice base on Nutz</description>
@ -84,7 +85,7 @@
<!--<module>nutzboot-demo</module>-->
<module>nutzcloud</module>
</modules>
</modules>
<profiles>
<profile>
<id>demo</id>
@ -968,7 +969,12 @@
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-nacos-config-client</artifactId>
<artifactId>nutzboot-starter-nacos-config</artifactId>
<version>${nutzboot.version}</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-nacos-discovery</artifactId>
<version>${nutzboot.version}</version>
</dependency>
<dependency>
@ -999,6 +1005,11 @@
<artifactId>nutzcloud-perca</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>