Skip to content

Commit

Permalink
Polish apache#4940 : NacosDynamicConfiguration supports getConfigKeys…
Browse files Browse the repository at this point in the history
… method
  • Loading branch information
mercyblitz committed Aug 26, 2019
1 parent 75939d2 commit a15ea1a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,40 @@
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.AbstractSharedListener;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.http.HttpAgent;
import com.alibaba.nacos.client.config.impl.HttpSimpleClient;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Executor;
import java.util.stream.Stream;

import static com.alibaba.nacos.api.PropertyKeyConst.ACCESS_KEY;
import static com.alibaba.nacos.api.PropertyKeyConst.CLUSTER_NAME;
import static com.alibaba.nacos.api.PropertyKeyConst.ENCODE;
import static com.alibaba.nacos.api.PropertyKeyConst.ENDPOINT;
import static com.alibaba.nacos.api.PropertyKeyConst.NAMESPACE;
import static com.alibaba.nacos.api.PropertyKeyConst.SECRET_KEY;
import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR;
import static com.alibaba.nacos.client.naming.utils.UtilAndComs.NACOS_NAMING_LOG_NAME;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_CHAR_SEPERATOR;
import static org.apache.dubbo.common.constants.RemotingConstants.BACKUP_KEY;

Expand All @@ -54,30 +68,37 @@
*/
public class NacosDynamicConfiguration implements DynamicConfiguration {

private static final String GET_CONFIG_KEYS_PATH = "/v1/cs/configs";

private final Logger logger = LoggerFactory.getLogger(getClass());
/**
* the default timeout in millis to get config from nacos
*/
private static final long DEFAULT_TIMEOUT = 5000L;

private Properties nacosProperties;

/**
* The nacos configService
*/
private final ConfigService configService;

private ConfigService configService;
private HttpAgent httpAgent;

/**
* The map store the key to {@link NacosConfigListener} mapping
*/
private final ConcurrentMap<String, NacosConfigListener> watchListenerMap;

NacosDynamicConfiguration(URL url) {
buildConfigService(url);
this.nacosProperties = buildNacosProperties(url);
this.configService = buildConfigService(url);
this.httpAgent = getHttpAgent(configService);
watchListenerMap = new ConcurrentHashMap<>();
}

private ConfigService buildConfigService(URL url) {
Properties nacosProperties = buildNacosProperties(url);
ConfigService configService = null;
try {
configService = NacosFactory.createConfigService(nacosProperties);
} catch (NacosException e) {
Expand All @@ -89,6 +110,18 @@ private ConfigService buildConfigService(URL url) {
return configService;
}

private HttpAgent getHttpAgent(ConfigService configService) {
HttpAgent agent = null;
try {
Field field = configService.getClass().getDeclaredField("agent");
field.setAccessible(true);
agent = (HttpAgent) field.get(configService);
} catch (Exception e) {
throw new IllegalStateException(e);
}
return agent;
}

public void publishNacosConfig(String key, String value) {
String[] keyAndGroup = getKeyAndGroup(key);
publishConfig(keyAndGroup[0], keyAndGroup[1], value);
Expand Down Expand Up @@ -143,6 +176,7 @@ private void setProperties(URL url, Properties properties) {
putPropertyIfAbsent(url, properties, ACCESS_KEY);
putPropertyIfAbsent(url, properties, SECRET_KEY);
putPropertyIfAbsent(url, properties, CLUSTER_NAME);
putPropertyIfAbsent(url, properties, ENCODE);
}

private void putPropertyIfAbsent(URL url, Properties properties, String propertyName) {
Expand Down Expand Up @@ -208,6 +242,36 @@ public Object getInternalProperty(String key) {
return null;
}

@Override
public SortedSet<String> getConfigKeys(String group) {
// TODO use Nacos Client API to replace HTTP Open API
SortedSet<String> keys = new TreeSet<>();
try {
List<String> paramsValues = asList("search", "accurate", "dataId", "", "group", group, "pageNo", "1", "pageSize", String.valueOf(Integer.MAX_VALUE));
String encoding = getProperty(ENCODE, "UTF-8");
HttpSimpleClient.HttpResult result = httpAgent.httpGet(GET_CONFIG_KEYS_PATH, emptyList(), paramsValues, encoding, 5 * 1000);
Stream<String> keysStream = toKeysStream(result.content);
keysStream.forEach(keys::add);
} catch (IOException e) {
if (logger.isErrorEnabled()) {
logger.error(e.getMessage(), e);
}
}
return keys;
}

private Stream<String> toKeysStream(String content) {
JSONObject jsonObject = JSON.parseObject(content);
JSONArray pageItems = jsonObject.getJSONArray("pageItems");
return pageItems.stream()
.map(object -> (JSONObject) object)
.map(json -> json.getString("dataId"));
}

private String getProperty(String name, String defaultValue) {
return nacosProperties.getProperty(name, defaultValue);
}

public class NacosConfigListener extends AbstractSharedListener {

private Set<ConfigurationListener> listeners = new CopyOnWriteArraySet<>();
Expand Down Expand Up @@ -259,5 +323,4 @@ private ConfigChangeType getChangeType(String configInfo, String oldValue) {
return ConfigChangeType.MODIFIED;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.concurrent.CountDownLatch;


Expand Down Expand Up @@ -99,6 +100,18 @@ public void testAddListener() throws Exception {

}

@Test
public void testGetConfigKeys() {

put("key1", "a");
put("key2", "b");

SortedSet<String> keys = config.getConfigKeys(DynamicConfiguration.DEFAULT_GROUP);

Assertions.assertFalse(keys.isEmpty());

}

private void put(String key, String value) {
put(key, DynamicConfiguration.DEFAULT_GROUP, value);
}
Expand Down

0 comments on commit a15ea1a

Please sign in to comment.