Skip to content

Commit

Permalink
Upgrade nacos-client version and construct NacosDataSource by Propert…
Browse files Browse the repository at this point in the history
…ies (#348)

* Polish #272: upgrade nacos-client version and create a new constructor for NacosDataSource to support namespace, endpoint, ak, sk and so on
* check properties in NacosDataSource
  • Loading branch information
fangjian0423 authored and sczyh30 committed Dec 27, 2018
1 parent af33609 commit 4592c45
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion sentinel-extension/sentinel-datasource-nacos/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<packaging>jar</packaging>

<properties>
<nacos.version>0.2.1</nacos.version>
<nacos.version>0.6.2</nacos.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.csp.sentinel.datasource.nacos;

import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
Expand All @@ -25,8 +26,10 @@
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
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;

Expand All @@ -50,6 +53,7 @@ public class NacosDataSource<T> extends AbstractDataSource<String, T> {
private final Listener configListener;
private final String groupId;
private final String dataId;
private final Properties properties;

/**
* Note: The Nacos config might be null if its initialization failed.
Expand All @@ -66,13 +70,27 @@ public class NacosDataSource<T> extends AbstractDataSource<String, T> {
*/
public NacosDataSource(final String serverAddr, final String groupId, final String dataId,
Converter<String, T> parser) {
this(NacosDataSource.buildProperties(serverAddr), groupId, dataId, parser);
}

/**
*
* @param properties properties for construct {@link ConfigService} using {@link NacosFactory#createConfigService(Properties)}
* @param groupId group ID, cannot be empty
* @param dataId data ID, cannot be empty
* @param parser customized data parser, cannot be empty
*/
public NacosDataSource(final Properties properties, final String groupId, final String dataId,
Converter<String, T> parser) {
super(parser);
if (StringUtil.isBlank(serverAddr) || StringUtil.isBlank(groupId) || StringUtil.isBlank(dataId)) {
throw new IllegalArgumentException(String.format("Bad argument: serverAddr=[%s], groupId=[%s], dataId=[%s]",
serverAddr, groupId, dataId));
if (StringUtil.isBlank(groupId) || StringUtil.isBlank(dataId)) {
throw new IllegalArgumentException(String.format("Bad argument: groupId=[%s], dataId=[%s]",
groupId, dataId));
}
AssertUtil.notNull(properties, "Nacos properties must not be null, you could put some keys from PropertyKeyConst");
this.groupId = groupId;
this.dataId = dataId;
this.properties = properties;
this.configListener = new Listener() {
@Override
public Executor getExecutor() {
Expand All @@ -81,14 +99,14 @@ public Executor getExecutor() {

@Override
public void receiveConfigInfo(final String configInfo) {
RecordLog.info(String.format("[NacosDataSource] New property value received for (%s, %s, %s): %s",
serverAddr, dataId, groupId, configInfo));
RecordLog.info(String.format("[NacosDataSource] New property value received for (properties: %s) (dataId: %s, groupId: %s): %s",
properties, dataId, groupId, configInfo));
T newValue = NacosDataSource.this.parser.convert(configInfo);
// Update the new value to the property.
getProperty().updateValue(newValue);
}
};
initNacosListener(serverAddr);
initNacosListener();
loadInitialConfig();
}

Expand All @@ -104,9 +122,9 @@ private void loadInitialConfig() {
}
}

private void initNacosListener(String serverAddr) {
private void initNacosListener() {
try {
this.configService = NacosFactory.createConfigService(serverAddr);
this.configService = NacosFactory.createConfigService(this.properties);
// Add config listener.
configService.addListener(dataId, groupId, configListener);
} catch (Exception e) {
Expand All @@ -130,4 +148,10 @@ public void close() {
}
pool.shutdownNow();
}

private static Properties buildProperties(String serverAddr) {
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverAddr);
return properties;
}
}

0 comments on commit 4592c45

Please sign in to comment.