Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade nacos-client version and construct NacosDataSource by Properties #348

Merged
merged 2 commits into from
Dec 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
sczyh30 marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}