Skip to content
Permalink
Browse files
#591 add namespace for nacos (#638)
fix #591, #628
  • Loading branch information
jiang2015 committed Jan 13, 2021
1 parent f3028e6 commit 8fc59272e6621ecd89bc956e730cd7eafa8b52e5
Showing 4 changed files with 33 additions and 8 deletions.
@@ -28,6 +28,7 @@ public class Constants {
public static final String DEFAULT_ROOT = "dubbo";
public static final String PATH_SEPARATOR = "/";
public static final String GROUP_KEY = "group";
public static final String NAMESPACE_KEY = "namespace";
public static final String CONFIG_KEY = "config" + PATH_SEPARATOR + "dubbo";
public static final String DUBBO_PROPERTY = "dubbo.properties";
public static final String PROVIDER_SIDE = "provider";
@@ -65,6 +65,15 @@ public class ConfigCenter {
@Value("${admin.metadata-report.group:dubbo}")
private String metadataGroup;

@Value("${admin.registry.namespace:dubbo}")
private String registryNameSpace;

@Value("${admin.config-center.namespace:dubbo}")
private String configCenterGroupNameSpace;

@Value("${admin.metadata-report.namespace:dubbo}")
private String metadataGroupNameSpace;

@Value("${admin.config-center.username:}")
private String username;
@Value("${admin.config-center.password:}")
@@ -86,7 +95,7 @@ GovernanceConfiguration getDynamicConfiguration() {
GovernanceConfiguration dynamicConfiguration = null;

if (StringUtils.isNotEmpty(configCenter)) {
configCenterUrl = formUrl(configCenter, configCenterGroup, username, password);
configCenterUrl = formUrl(configCenter, configCenterGroup, configCenterGroupNameSpace, username, password);
dynamicConfiguration = ExtensionLoader.getExtensionLoader(GovernanceConfiguration.class).getExtension(configCenterUrl.getProtocol());
dynamicConfiguration.setUrl(configCenterUrl);
dynamicConfiguration.init();
@@ -96,16 +105,16 @@ GovernanceConfiguration getDynamicConfiguration() {
Arrays.stream(config.split("\n")).forEach( s -> {
if(s.startsWith(Constants.REGISTRY_ADDRESS)) {
String registryAddress = s.split("=")[1].trim();
registryUrl = formUrl(registryAddress, configCenterGroup, username, password);
registryUrl = formUrl(registryAddress, configCenterGroup, registryNameSpace, username, password);
} else if (s.startsWith(Constants.METADATA_ADDRESS)) {
metadataUrl = formUrl(s.split("=")[1].trim(), configCenterGroup, username, password);
metadataUrl = formUrl(s.split("=")[1].trim(), configCenterGroup, metadataGroupNameSpace, username, password);
}
});
}
}
if (dynamicConfiguration == null) {
if (StringUtils.isNotEmpty(registryAddress)) {
registryUrl = formUrl(registryAddress, registryGroup, username, password);
registryUrl = formUrl(registryAddress, registryGroup, registryNameSpace, username, password);
dynamicConfiguration = ExtensionLoader.getExtensionLoader(GovernanceConfiguration.class).getExtension(registryUrl.getProtocol());
dynamicConfiguration.setUrl(registryUrl);
dynamicConfiguration.init();
@@ -129,7 +138,7 @@ Registry getRegistry() {
if (StringUtils.isBlank(registryAddress)) {
throw new ConfigurationException("Either config center or registry address is needed, please refer to https://github.com/apache/incubator-dubbo-admin/wiki/Dubbo-Admin-configuration");
}
registryUrl = formUrl(registryAddress, registryGroup, username, password);
registryUrl = formUrl(registryAddress, registryGroup, registryNameSpace, username, password);
}
RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
registry = registryFactory.getRegistry(registryUrl);
@@ -145,7 +154,7 @@ MetaDataCollector getMetadataCollector() {
MetaDataCollector metaDataCollector = new NoOpMetadataCollector();
if (metadataUrl == null) {
if (StringUtils.isNotEmpty(metadataAddress)) {
metadataUrl = formUrl(metadataAddress, metadataGroup, username, password);
metadataUrl = formUrl(metadataAddress, metadataGroup, metadataGroupNameSpace, username, password);
metadataUrl = metadataUrl.addParameter(CLUSTER_KEY, cluster);
}
}
@@ -159,11 +168,14 @@ MetaDataCollector getMetadataCollector() {
return metaDataCollector;
}

private URL formUrl(String config, String group, String username, String password) {
private URL formUrl(String config, String group, String nameSpace, String username, String password) {
URL url = URL.valueOf(config);
if (StringUtils.isNotEmpty(group)) {
url = url.addParameter(Constants.GROUP_KEY, group);
}
if (StringUtils.isNotEmpty(nameSpace)){
url = url.addParameter(Constants.NAMESPACE_KEY, nameSpace);
}
if (StringUtils.isNotEmpty(username)) {
url = url.setUsername(username);
}
@@ -31,18 +31,20 @@
import java.util.Properties;

import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR;
import static com.alibaba.nacos.api.PropertyKeyConst.NAMESPACE;

public class NacosConfiguration implements GovernanceConfiguration {
private static final Logger logger = LoggerFactory.getLogger(NacosConfiguration.class);

private ConfigService configService;
private String nameSpace;
private String group;
private URL url;

@Override
public void init() {
group = url.getParameter(Constants.GROUP_KEY, "DEFAULT_GROUP");

nameSpace = url.getParameter(Constants.NAMESPACE_KEY, "public");
configService = buildConfigService(url);
}

@@ -72,6 +74,7 @@ private void setServerAddr(URL url, Properties properties) {
url.getPort() // Port
;
properties.put(SERVER_ADDR, serverAddr);
properties.put(NAMESPACE, nameSpace);
}


@@ -20,13 +20,22 @@ admin.registry.address=zookeeper://127.0.0.1:2183
admin.config-center=zookeeper://127.0.0.1:2183
admin.metadata-report.address=zookeeper://127.0.0.1:2183

#admin.registry.address=nacos://127.0.0.1:8848
#admin.config-center=nacos://127.0.0.1:8848
#admin.metadata-report.address=nacos://127.0.0.1:8848

admin.root.user.name=root
admin.root.user.password=root
#group
admin.registry.group=dubbo
admin.config-center.group=dubbo
admin.metadata-report.group=dubbo

#namespace used by nacos
admin.registry.namespace=public
admin.config-center.namespace=public
admin.metadata-report.namespace=public

admin.apollo.token=e16e5cd903fd0c97a116c873b448544b9d086de9
admin.apollo.appId=test
admin.apollo.env=dev

0 comments on commit 8fc5927

Please sign in to comment.