Merge branch 'feature_naming_group' of https://github.com/alibaba/nacos into feature_naming_group

This commit is contained in:
xiaochun.xxc 2019-03-05 11:45:07 +08:00
commit 8293662940
5 changed files with 33 additions and 27 deletions

View File

@ -174,7 +174,7 @@ public class NacosNamingService implements NamingService {
public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException { public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
BeatInfo beatInfo = new BeatInfo(); BeatInfo beatInfo = new BeatInfo();
beatInfo.setServiceName(serviceName); beatInfo.setServiceName(groupName + Constants.SERVICE_INFO_SPLITER + serviceName);
beatInfo.setIp(instance.getIp()); beatInfo.setIp(instance.getIp());
beatInfo.setPort(instance.getPort()); beatInfo.setPort(instance.getPort());
beatInfo.setCluster(instance.getClusterName()); beatInfo.setCluster(instance.getClusterName());
@ -182,9 +182,9 @@ public class NacosNamingService implements NamingService {
beatInfo.setMetadata(instance.getMetadata()); beatInfo.setMetadata(instance.getMetadata());
beatInfo.setScheduled(false); beatInfo.setScheduled(false);
beatReactor.addBeatInfo(serviceName, groupName, beatInfo); beatReactor.addBeatInfo(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, beatInfo);
serverProxy.registerService(serviceName, groupName, instance); serverProxy.registerService(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, groupName, instance);
} }
@Override @Override
@ -204,8 +204,8 @@ public class NacosNamingService implements NamingService {
@Override @Override
public void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException { public void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException {
beatReactor.removeBeatInfo(serviceName, groupName, ip, port); beatReactor.removeBeatInfo(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, ip, port);
serverProxy.deregisterService(serviceName, ip, port, clusterName); serverProxy.deregisterService(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, ip, port, clusterName);
} }
@Override @Override

View File

@ -58,20 +58,20 @@ public class BeatReactor {
executorService.schedule(new BeatProcessor(), 0, TimeUnit.MILLISECONDS); executorService.schedule(new BeatProcessor(), 0, TimeUnit.MILLISECONDS);
} }
public void addBeatInfo(String dom, String groupName, BeatInfo beatInfo) { public void addBeatInfo(String serviceName, BeatInfo beatInfo) {
NAMING_LOGGER.info("[BEAT] adding beat: {} to beat map.", beatInfo); NAMING_LOGGER.info("[BEAT] adding beat: {} to beat map.", beatInfo);
dom2Beat.put(buildKey(dom, groupName, beatInfo.getIp(), beatInfo.getPort()), beatInfo); dom2Beat.put(buildKey(serviceName, beatInfo.getIp(), beatInfo.getPort()), beatInfo);
MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size()); MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size());
} }
public void removeBeatInfo(String dom, String groupName, String ip, int port) { public void removeBeatInfo(String serviceName, String ip, int port) {
NAMING_LOGGER.info("[BEAT] removing beat: {}:{}:{} from beat map.", dom, ip, port); NAMING_LOGGER.info("[BEAT] removing beat: {}:{}:{} from beat map.", serviceName, ip, port);
dom2Beat.remove(buildKey(dom, groupName, ip, port)); dom2Beat.remove(buildKey(serviceName, ip, port));
MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size()); MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size());
} }
public String buildKey(String serviceName, String groupName, String ip, int port) { public String buildKey(String serviceName, String ip, int port) {
return serviceName + Constants.NAMING_INSTANCE_ID_SPLITTER + groupName + Constants.NAMING_INSTANCE_ID_SPLITTER return serviceName + Constants.NAMING_INSTANCE_ID_SPLITTER
+ ip + Constants.NAMING_INSTANCE_ID_SPLITTER + port; + ip + Constants.NAMING_INSTANCE_ID_SPLITTER + port;
} }

View File

@ -97,7 +97,7 @@ public class DistroFilter implements Filter {
headerList.add(req.getHeader(headerName)); headerList.add(req.getHeader(headerName));
} }
HttpClient.HttpResult result = HttpClient.HttpResult result =
HttpClient.request(distroMapper.mapSrv(serviceName) + urlString, headerList, new HashMap<>(2) HttpClient.request("http://" + distroMapper.mapSrv(serviceName) + urlString, headerList, new HashMap<>(2)
, PROXY_CONNECT_TIMEOUT, PROXY_READ_TIMEOUT, "UTF-8", req.getMethod()); , PROXY_CONNECT_TIMEOUT, PROXY_READ_TIMEOUT, "UTF-8", req.getMethod());
try { try {

View File

@ -57,10 +57,10 @@ public class AutoDeregisterInstance_ITCase {
@Before @Before
public void init() throws Exception { public void init() throws Exception {
NamingBase.prepareServer(port); // NamingBase.prepareServer(port);
if (naming == null) { if (naming == null) {
naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); naming = NamingFactory.createNamingService("11.239.112.161:8848,11.239.113.204:8848,11.239.114.187:8848");
} }
while (true) { while (true) {
@ -94,7 +94,8 @@ public class AutoDeregisterInstance_ITCase {
NacosNamingService namingServiceImpl = (NacosNamingService) naming; NacosNamingService namingServiceImpl = (NacosNamingService) naming;
namingServiceImpl.getBeatReactor().removeBeatInfo(serviceName, Constants.DEFAULT_GROUP, "127.0.0.1", TEST_PORT); namingServiceImpl.getBeatReactor().
removeBeatInfo(Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER + serviceName, "127.0.0.1", TEST_PORT);
verifyInstanceList(instances, 1, serviceName); verifyInstanceList(instances, 1, serviceName);
instances = naming.getAllInstances(serviceName); instances = naming.getAllInstances(serviceName);
@ -131,7 +132,8 @@ public class AutoDeregisterInstance_ITCase {
NacosNamingService namingServiceImpl = (NacosNamingService) naming; NacosNamingService namingServiceImpl = (NacosNamingService) naming;
namingServiceImpl.getBeatReactor().removeBeatInfo(serviceName, "DEFAULT_GROUP", "127.0.0.1", TEST_PORT); namingServiceImpl.getBeatReactor().
removeBeatInfo(Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER + serviceName, "127.0.0.1", TEST_PORT);
verifyInstanceList(instances, 1, serviceName); verifyInstanceList(instances, 1, serviceName);
instances = naming.getAllInstances(serviceName); instances = naming.getAllInstances(serviceName);
@ -163,7 +165,8 @@ public class AutoDeregisterInstance_ITCase {
NacosNamingService namingServiceImpl = (NacosNamingService) naming; NacosNamingService namingServiceImpl = (NacosNamingService) naming;
namingServiceImpl.getBeatReactor().removeBeatInfo(serviceName, "DEFAULT_GROUP", "127.0.0.1", TEST_PORT); namingServiceImpl.getBeatReactor().
removeBeatInfo(Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER + serviceName, "127.0.0.1", TEST_PORT);
verifyInstanceList(instances, 1, serviceName); verifyInstanceList(instances, 1, serviceName);
@ -175,7 +178,9 @@ public class AutoDeregisterInstance_ITCase {
beatInfo.setIp("127.0.0.1"); beatInfo.setIp("127.0.0.1");
beatInfo.setPort(TEST_PORT); beatInfo.setPort(TEST_PORT);
namingServiceImpl.getBeatReactor().addBeatInfo(serviceName, "DEFAULT_GROUP", beatInfo); namingServiceImpl.getBeatReactor().
addBeatInfo(Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER + serviceName, beatInfo);
verifyInstanceList(instances, 2, serviceName); verifyInstanceList(instances, 2, serviceName);
instances = naming.getAllInstances(serviceName); instances = naming.getAllInstances(serviceName);
@ -206,13 +211,14 @@ public class AutoDeregisterInstance_ITCase {
NacosNamingService namingServiceImpl = (NacosNamingService) naming; NacosNamingService namingServiceImpl = (NacosNamingService) naming;
namingServiceImpl.getBeatReactor().removeBeatInfo(serviceName, "DEFAULT_GROUP", "127.0.0.1", TEST_PORT); namingServiceImpl.getBeatReactor().
removeBeatInfo(Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER + serviceName, "127.0.0.1", TEST_PORT);
verifyInstanceList(instances, 1, serviceName); verifyInstanceList(instances, 1, serviceName);
instances = naming.getAllInstances(serviceName); instances = naming.getAllInstances(serviceName);
Assert.assertEquals(instances.size(), 1); Assert.assertEquals(1, instances.size());
BeatInfo beatInfo = new BeatInfo(); BeatInfo beatInfo = new BeatInfo();
beatInfo.setServiceName(serviceName); beatInfo.setServiceName(serviceName);
beatInfo.setIp("127.0.0.1"); beatInfo.setIp("127.0.0.1");
@ -220,7 +226,8 @@ public class AutoDeregisterInstance_ITCase {
beatInfo.setCluster("c1"); beatInfo.setCluster("c1");
namingServiceImpl.getBeatReactor().addBeatInfo(serviceName, "DEFAULT_GROUP", beatInfo); namingServiceImpl.getBeatReactor().
addBeatInfo(Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER + serviceName, beatInfo);
//TimeUnit.SECONDS.sleep(15); //TimeUnit.SECONDS.sleep(15);
verifyInstanceList(instances, 2, serviceName); verifyInstanceList(instances, 2, serviceName);
@ -239,7 +246,7 @@ public class AutoDeregisterInstance_ITCase {
public void verifyInstanceList(List<Instance> instances, int size, String serviceName) throws Exception { public void verifyInstanceList(List<Instance> instances, int size, String serviceName) throws Exception {
int i = 0; int i = 0;
while ( i < 20 ) { while (i < 20) {
instances = naming.getAllInstances(serviceName); instances = naming.getAllInstances(serviceName);
if (instances.size() == size) { if (instances.size() == size) {
break; break;

View File

@ -238,6 +238,7 @@ public class MultiTenant_ITCase {
@Test @Test
public void multipleTenant_group_getInstances() throws Exception { public void multipleTenant_group_getInstances() throws Exception {
String serviceName = randomDomainName(); String serviceName = randomDomainName();
System.out.println(serviceName);
naming1.registerInstance(serviceName, TEST_GROUP_1,"11.11.11.22", 80); naming1.registerInstance(serviceName, TEST_GROUP_1,"11.11.11.22", 80);
naming1.registerInstance(serviceName, TEST_GROUP_2,"11.11.11.11", 80); naming1.registerInstance(serviceName, TEST_GROUP_2,"11.11.11.11", 80);
@ -247,9 +248,7 @@ public class MultiTenant_ITCase {
List<Instance> instances = naming1.getAllInstances(serviceName); List<Instance> instances = naming1.getAllInstances(serviceName);
Assert.assertEquals(1, instances.size()); Assert.assertEquals(0, instances.size());
Assert.assertEquals("11.11.11.11", instances.get(0).getIp());
Assert.assertEquals(80, instances.get(0).getPort());
instances = naming.getAllInstances(serviceName); instances = naming.getAllInstances(serviceName);
Assert.assertEquals(1, instances.size()); Assert.assertEquals(1, instances.size());