diff --git a/sentinel-extension/sentinel-datasource-nacos/pom.xml b/sentinel-extension/sentinel-datasource-nacos/pom.xml index 5903973589..573823ac03 100644 --- a/sentinel-extension/sentinel-datasource-nacos/pom.xml +++ b/sentinel-extension/sentinel-datasource-nacos/pom.xml @@ -13,7 +13,7 @@ jar - 0.2.1 + 0.6.2 diff --git a/sentinel-extension/sentinel-datasource-nacos/src/main/java/com/alibaba/csp/sentinel/datasource/nacos/NacosDataSource.java b/sentinel-extension/sentinel-datasource-nacos/src/main/java/com/alibaba/csp/sentinel/datasource/nacos/NacosDataSource.java index e8c7bcaa02..f7b4727697 100644 --- a/sentinel-extension/sentinel-datasource-nacos/src/main/java/com/alibaba/csp/sentinel/datasource/nacos/NacosDataSource.java +++ b/sentinel-extension/sentinel-datasource-nacos/src/main/java/com/alibaba/csp/sentinel/datasource/nacos/NacosDataSource.java @@ -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; @@ -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; @@ -50,6 +53,7 @@ public class NacosDataSource extends AbstractDataSource { 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. @@ -66,13 +70,27 @@ public class NacosDataSource extends AbstractDataSource { */ public NacosDataSource(final String serverAddr, final String groupId, final String dataId, Converter 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 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() { @@ -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(); } @@ -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) { @@ -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; + } }