diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index 97b7f1ab1..4d50e675f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -174,7 +174,7 @@ public class NacosNamingService implements NamingService { public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException { BeatInfo beatInfo = new BeatInfo(); - beatInfo.setServiceName(serviceName); + beatInfo.setServiceName(groupName + Constants.SERVICE_INFO_SPLITER + serviceName); beatInfo.setIp(instance.getIp()); beatInfo.setPort(instance.getPort()); beatInfo.setCluster(instance.getClusterName()); @@ -182,9 +182,9 @@ public class NacosNamingService implements NamingService { beatInfo.setMetadata(instance.getMetadata()); 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 @@ -204,8 +204,8 @@ public class NacosNamingService implements NamingService { @Override public void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException { - beatReactor.removeBeatInfo(serviceName, groupName, ip, port); - serverProxy.deregisterService(serviceName, ip, port, clusterName); + beatReactor.removeBeatInfo(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, ip, port); + serverProxy.deregisterService(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, ip, port, clusterName); } @Override diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java index da9a44aa4..b0792ffdf 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java @@ -58,20 +58,20 @@ public class BeatReactor { 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); - dom2Beat.put(buildKey(dom, groupName, beatInfo.getIp(), beatInfo.getPort()), beatInfo); + dom2Beat.put(buildKey(serviceName, beatInfo.getIp(), beatInfo.getPort()), beatInfo); MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size()); } - public void removeBeatInfo(String dom, String groupName, String ip, int port) { - NAMING_LOGGER.info("[BEAT] removing beat: {}:{}:{} from beat map.", dom, ip, port); - dom2Beat.remove(buildKey(dom, groupName, ip, port)); + public void removeBeatInfo(String serviceName, String ip, int port) { + NAMING_LOGGER.info("[BEAT] removing beat: {}:{}:{} from beat map.", serviceName, ip, port); + dom2Beat.remove(buildKey(serviceName, ip, port)); MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size()); } - public String buildKey(String serviceName, String groupName, String ip, int port) { - return serviceName + Constants.NAMING_INSTANCE_ID_SPLITTER + groupName + Constants.NAMING_INSTANCE_ID_SPLITTER + public String buildKey(String serviceName, String ip, int port) { + return serviceName + Constants.NAMING_INSTANCE_ID_SPLITTER + ip + Constants.NAMING_INSTANCE_ID_SPLITTER + port; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java b/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java index 7219a0399..3585a392d 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java @@ -97,7 +97,7 @@ public class DistroFilter implements Filter { headerList.add(req.getHeader(headerName)); } 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()); try { diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java index 8b1b04669..6637265be 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java @@ -46,7 +46,7 @@ import static com.alibaba.nacos.test.naming.NamingBase.randomDomainName; */ @RunWith(SpringRunner.class) @SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class AutoDeregisterInstance_ITCase { private NamingService naming; @@ -57,10 +57,10 @@ public class AutoDeregisterInstance_ITCase { @Before public void init() throws Exception { - NamingBase.prepareServer(port); +// NamingBase.prepareServer(port); 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) { @@ -94,7 +94,8 @@ public class AutoDeregisterInstance_ITCase { 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); instances = naming.getAllInstances(serviceName); @@ -131,7 +132,8 @@ public class AutoDeregisterInstance_ITCase { 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); instances = naming.getAllInstances(serviceName); @@ -163,7 +165,8 @@ public class AutoDeregisterInstance_ITCase { 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); @@ -175,7 +178,9 @@ public class AutoDeregisterInstance_ITCase { beatInfo.setIp("127.0.0.1"); 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); instances = naming.getAllInstances(serviceName); @@ -206,13 +211,14 @@ public class AutoDeregisterInstance_ITCase { 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); instances = naming.getAllInstances(serviceName); - Assert.assertEquals(instances.size(), 1); + Assert.assertEquals(1, instances.size()); BeatInfo beatInfo = new BeatInfo(); beatInfo.setServiceName(serviceName); beatInfo.setIp("127.0.0.1"); @@ -220,7 +226,8 @@ public class AutoDeregisterInstance_ITCase { 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); verifyInstanceList(instances, 2, serviceName); @@ -239,7 +246,7 @@ public class AutoDeregisterInstance_ITCase { public void verifyInstanceList(List instances, int size, String serviceName) throws Exception { int i = 0; - while ( i < 20 ) { + while (i < 20) { instances = naming.getAllInstances(serviceName); if (instances.size() == size) { break; diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/MultiTenant_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/MultiTenant_ITCase.java index 7c3dfc31d..ce9fbbc1b 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/MultiTenant_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/MultiTenant_ITCase.java @@ -238,6 +238,7 @@ public class MultiTenant_ITCase { @Test public void multipleTenant_group_getInstances() throws Exception { String serviceName = randomDomainName(); + System.out.println(serviceName); naming1.registerInstance(serviceName, TEST_GROUP_1,"11.11.11.22", 80); naming1.registerInstance(serviceName, TEST_GROUP_2,"11.11.11.11", 80); @@ -247,9 +248,7 @@ public class MultiTenant_ITCase { List instances = naming1.getAllInstances(serviceName); - Assert.assertEquals(1, instances.size()); - Assert.assertEquals("11.11.11.11", instances.get(0).getIp()); - Assert.assertEquals(80, instances.get(0).getPort()); + Assert.assertEquals(0, instances.size()); instances = naming.getAllInstances(serviceName); Assert.assertEquals(1, instances.size());