diff --git a/extension-impl/registry-nacos/src/main/java/com/alipay/sofa/rpc/registry/nacos/NacosRegistry.java b/extension-impl/registry-nacos/src/main/java/com/alipay/sofa/rpc/registry/nacos/NacosRegistry.java
index 93379c3f4..19577d41c 100644
--- a/extension-impl/registry-nacos/src/main/java/com/alipay/sofa/rpc/registry/nacos/NacosRegistry.java
+++ b/extension-impl/registry-nacos/src/main/java/com/alipay/sofa/rpc/registry/nacos/NacosRegistry.java
@@ -54,7 +54,7 @@
*
Simple Nacos registry. Features:
* 1. register publisher as instance to nacos server.
* 2. subscribe instances change event
- *
+ *
*
* Structure of nacos storage:
* --sofa-rpc (namespace)
@@ -66,9 +66,9 @@
* |--com.alipay.sofa.rpc.example.EchoService (next serviceName)
* |......
*
- *
+ *
*
- *
+ *
* @author JervyShi
*/
@Extension("nacos")
@@ -92,6 +92,8 @@ public class NacosRegistry extends Registry {
private ConcurrentMap consumerListeners = new ConcurrentHashMap();
+ private Properties nacosConfig = new Properties();
+
/**
* Instantiates a new Nacos registry.
*
@@ -116,7 +118,11 @@ public synchronized void init() {
String address; // IP地址
if (idx > 0) {
address = addressInput.substring(0, idx);
- namespace = addressInput.substring(idx);
+ namespace = addressInput.substring(idx + 1);
+ //for host:port/ this scene
+ if (StringUtils.isBlank(namespace)) {
+ namespace = DEFAULT_NAMESPACE;
+ }
} else {
address = addressInput;
namespace = DEFAULT_NAMESPACE;
@@ -124,7 +130,6 @@ public synchronized void init() {
defaultCluster = Collections.singletonList(NacosRegistryHelper.DEFAULT_CLUSTER);
- Properties nacosConfig = new Properties();
nacosConfig.put(PropertyKeyConst.SERVER_ADDR, address);
nacosConfig.put(PropertyKeyConst.NAMESPACE, namespace);
@@ -329,4 +334,8 @@ public void destroy() {
namingService = null;
providerObserver = null;
}
+
+ public Properties getNacosConfig() {
+ return nacosConfig;
+ }
}
diff --git a/extension-impl/registry-nacos/src/test/java/com/alipay/sofa/rpc/registry/nacos/NacosRegistryNamespaceTest.java b/extension-impl/registry-nacos/src/test/java/com/alipay/sofa/rpc/registry/nacos/NacosRegistryNamespaceTest.java
new file mode 100644
index 000000000..412fcce44
--- /dev/null
+++ b/extension-impl/registry-nacos/src/test/java/com/alipay/sofa/rpc/registry/nacos/NacosRegistryNamespaceTest.java
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.alipay.sofa.rpc.registry.nacos;
+
+import com.alibaba.nacos.api.PropertyKeyConst;
+import com.alipay.sofa.rpc.config.RegistryConfig;
+import com.alipay.sofa.rpc.registry.RegistryFactory;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Properties;
+
+/**
+ * @author bystander
+ * @version $Id: NacosRegistryNamespaceTest.java, v 0.1 2019年02月20日 18:30 bystander Exp $
+ */
+public class NacosRegistryNamespaceTest {
+
+ @Test
+ public void testWithNamespace() {
+ RegistryConfig registryConfig = new RegistryConfig()
+ .setProtocol("nacos")
+ .setSubscribe(true)
+ .setAddress("127.0.0.1:8848/namespace")
+ .setRegister(true);
+
+ NacosRegistry registry = (NacosRegistry) RegistryFactory.getRegistry(registryConfig);
+ registry.init();
+
+ Properties properties = registry.getNacosConfig();
+
+ String address = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
+
+ Assert.assertEquals("127.0.0.1:8848", address);
+ String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
+ Assert.assertEquals("namespace", namespace);
+
+ }
+
+ @Test
+ public void testWithoutNamespace() {
+ RegistryConfig registryConfig = new RegistryConfig()
+ .setProtocol("nacos")
+ .setSubscribe(true)
+ .setAddress("127.0.0.1:8848/")
+ .setRegister(true);
+
+ NacosRegistry registry = (NacosRegistry) RegistryFactory.getRegistry(registryConfig);
+ registry.init();
+
+ Properties properties = registry.getNacosConfig();
+
+ String address = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
+
+ Assert.assertEquals("127.0.0.1:8848", address);
+ String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
+ Assert.assertEquals("sofa-rpc", namespace);
+
+ }
+
+ @Test
+ public void testWithoutSlashNamespace() {
+ RegistryConfig registryConfig = new RegistryConfig()
+ .setProtocol("nacos")
+ .setSubscribe(true)
+ .setAddress("127.0.0.1:8848")
+ .setRegister(true);
+
+ NacosRegistry registry = (NacosRegistry) RegistryFactory.getRegistry(registryConfig);
+ registry.init();
+
+ Properties properties = registry.getNacosConfig();
+
+ String address = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
+
+ Assert.assertEquals("127.0.0.1:8848", address);
+ String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
+ //default namespace
+ Assert.assertEquals("sofa-rpc", namespace);
+
+ }
+}
\ No newline at end of file