Skip to content

Commit

Permalink
Fix nacos namespace bug. (sofastack#522)
Browse files Browse the repository at this point in the history
* Fix nacos namespace bug
  • Loading branch information
leizhiyuan authored Feb 21, 2019
1 parent 9a174e1 commit cc428b7
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
* <p>Simple Nacos registry. Features: <br/>
* 1. register publisher as instance to nacos server.
* 2. subscribe instances change event
*
*
* <pre>
* Structure of nacos storage:
* --sofa-rpc (namespace)
Expand All @@ -66,9 +66,9 @@
* |--com.alipay.sofa.rpc.example.EchoService (next serviceName)
* |......
* </pre>
*
*
* </p>
*
*
* @author <a href=mailto:[email protected]>JervyShi</a>
*/
@Extension("nacos")
Expand All @@ -92,6 +92,8 @@ public class NacosRegistry extends Registry {

private ConcurrentMap<ConsumerConfig, EventListener> consumerListeners = new ConcurrentHashMap<ConsumerConfig, EventListener>();

private Properties nacosConfig = new Properties();

/**
* Instantiates a new Nacos registry.
*
Expand All @@ -116,15 +118,18 @@ 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;
}

defaultCluster = Collections.singletonList(NacosRegistryHelper.DEFAULT_CLUSTER);

Properties nacosConfig = new Properties();
nacosConfig.put(PropertyKeyConst.SERVER_ADDR, address);
nacosConfig.put(PropertyKeyConst.NAMESPACE, namespace);

Expand Down Expand Up @@ -329,4 +334,8 @@ public void destroy() {
namingService = null;
providerObserver = null;
}

public Properties getNacosConfig() {
return nacosConfig;
}
}
Original file line number Diff line number Diff line change
@@ -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);

}
}

0 comments on commit cc428b7

Please sign in to comment.