From f6d4ee12527f62fc9922ec3fc61a5815d8271038 Mon Sep 17 00:00:00 2001 From: Nicholas2015 <799327210@qq.com> Date: Sun, 26 May 2019 23:43:58 +0800 Subject: [PATCH 1/8] get subscriber list --- .../naming/controllers/ServiceController.java | 29 +++++- .../nacos/naming/core/SubscribeManager.java | 92 +++++++++++++++++++ .../alibaba/nacos/naming/pojo/Subscriber.java | 75 +++++++++++++++ .../nacos/naming/pojo/Subscribers.java | 17 ++++ .../nacos/naming/push/PushService.java | 16 +++- 5 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java create mode 100644 naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscriber.java create mode 100644 naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscribers.java diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ServiceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ServiceController.java index 47c7683ff06..410c50d3129 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ServiceController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ServiceController.java @@ -28,6 +28,7 @@ import com.alibaba.nacos.naming.exception.NacosException; import com.alibaba.nacos.naming.misc.Loggers; import com.alibaba.nacos.naming.misc.UtilsAndCommons; +import com.alibaba.nacos.naming.pojo.Subscriber; import com.alibaba.nacos.naming.selector.LabelSelector; import com.alibaba.nacos.naming.selector.NoneSelector; import com.alibaba.nacos.naming.selector.Selector; @@ -40,7 +41,6 @@ import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; -import java.io.IOException; import java.net.URLDecoder; import java.util.*; @@ -62,6 +62,9 @@ public class ServiceController { @Autowired private ServerListManager serverListManager; + @Autowired + private SubscribeManager subscribeManager; + @RequestMapping(value = "", method = RequestMethod.POST) public String create(HttpServletRequest request) throws Exception { @@ -365,6 +368,30 @@ public JSONObject checksum(HttpServletRequest request) throws Exception { return result; } + /** + * get subscriber list + * @param request + * @return + */ + @RequestMapping(value = "/subscribers", method = RequestMethod.GET) + public JSONObject subscribers(HttpServletRequest request){ + String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, + Constants.DEFAULT_NAMESPACE_ID); + String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); + boolean aggregation = Boolean.valueOf(WebUtils.required(request,"aggregation")); + + JSONObject result = new JSONObject(); + + try { + List subscribers = subscribeManager.getSubscribers(serviceName,namespaceId,aggregation); + result.put("subscribers",subscribers); + return result; + } catch (InterruptedException e) { + + } + return result; + } + private List filterInstanceMetadata(String namespaceId, List serviceNames, String key, String value) { List filteredServiceNames = new ArrayList<>(); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java new file mode 100644 index 00000000000..9c03276b39d --- /dev/null +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java @@ -0,0 +1,92 @@ +package com.alibaba.nacos.naming.core; + +import com.alibaba.fastjson.JSONObject; +import com.alibaba.nacos.naming.boot.RunningConfig; +import com.alibaba.nacos.naming.cluster.ServerListManager; +import com.alibaba.nacos.naming.cluster.servers.Server; +import com.alibaba.nacos.naming.misc.HttpClient; +import com.alibaba.nacos.naming.misc.Loggers; +import com.alibaba.nacos.naming.misc.NetUtils; +import com.alibaba.nacos.naming.misc.UtilsAndCommons; +import com.alibaba.nacos.naming.pojo.Subscriber; +import com.alibaba.nacos.naming.pojo.Subscribers; +import com.alibaba.nacos.naming.push.PushService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.net.HttpURLConnection; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +/** + * @author Nicholas + */ +@Service +public class SubscribeManager { + + private static final String DATA_ON_SYNC_URL = "/service/subscribers"; + + @Autowired + private PushService pushService; + + @Autowired + private ServerListManager serverListManager; + + + private List getSubscribers(String serviceName, String namespaceId){ + return pushService.getClients(serviceName,namespaceId); + } + + /** + * + * @param serviceName + * @param namespaceId + * @param aggregation + * @return + * @throws InterruptedException + */ + public List getSubscribers(String serviceName, String namespaceId, boolean aggregation) throws InterruptedException { + if (aggregation){ + // size = 1 means only myself in the list, we need at least one another server alive: + while (serverListManager.getHealthyServers().size() <= 1) { + Thread.sleep(1000L); + Loggers.EPHEMERAL.info("waiting server list init..."); + } + + List subscriberList = new ArrayList(); + // try sync data from remote server: + for (Server server : serverListManager.getHealthyServers()) { + + Map paramValues = new HashMap<>(128); + paramValues.put("serviceName",serviceName); + paramValues.put("namespaceId",namespaceId); + paramValues.put("aggregation",String.valueOf(!aggregation)); + if (NetUtils.localServer().equals(server.getKey())) { + subscriberList.addAll(getSubscribers(serviceName,namespaceId)); + } + + HttpClient.HttpResult result = HttpClient.httpGet("http://" + server.getKey() + RunningConfig.getContextPath() + + UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_ON_SYNC_URL, new ArrayList<>(),paramValues); + + if (HttpURLConnection.HTTP_OK == result.code) { + Subscribers subscribers = (Subscribers) JSONObject.parseObject(result.content, Subscribers.class); + subscriberList.addAll(subscribers.getSubscribers()); + } + return subscriberList.stream().filter(distinctByKey(Subscriber::toString)).collect(Collectors.toList()); + + } + } else { + // local server + return getSubscribers(serviceName,namespaceId); + } + return Collections.emptyList(); + } + + public static Predicate distinctByKey(Function keyExtractor) { + Map seen = new ConcurrentHashMap<>(128); + return object -> seen.putIfAbsent(keyExtractor.apply(object), Boolean.TRUE) == null; + } +} diff --git a/naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscriber.java b/naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscriber.java new file mode 100644 index 00000000000..4cd08cb3813 --- /dev/null +++ b/naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscriber.java @@ -0,0 +1,75 @@ +package com.alibaba.nacos.naming.pojo; + +import java.io.Serializable; + +public class Subscriber implements Serializable { + + private String addrStr; + + private String agent; + + private String app; + + private String ip; + + private String namespaceId; + + private String serviceName; + + public Subscriber(String addrStr, String agent, String app, String ip, String namespaceId, String serviceName) { + this.addrStr = addrStr; + this.agent = agent; + this.app = app; + this.ip = ip; + this.namespaceId = namespaceId; + this.serviceName = serviceName; + } + + public String getAddrStr() { + return addrStr; + } + + public void setAddrStr(String addrStr) { + this.addrStr = addrStr; + } + + public String getAgent() { + return agent; + } + + public void setAgent(String agent) { + this.agent = agent; + } + + public String getApp() { + return app; + } + + public void setApp(String app) { + this.app = app; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public String getNamespaceId() { + return namespaceId; + } + + public void setNamespaceId(String namespaceId) { + this.namespaceId = namespaceId; + } + + public String getServiceName() { + return serviceName; + } + + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } +} diff --git a/naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscribers.java b/naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscribers.java new file mode 100644 index 00000000000..4a9cd699b69 --- /dev/null +++ b/naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscribers.java @@ -0,0 +1,17 @@ +package com.alibaba.nacos.naming.pojo; + +import java.io.Serializable; +import java.util.List; + +public class Subscribers implements Serializable { + + private List subscribers; + + public List getSubscribers() { + return subscribers; + } + + public void setSubscribers(List subscribers) { + this.subscribers = subscribers; + } +} diff --git a/naming/src/main/java/com/alibaba/nacos/naming/push/PushService.java b/naming/src/main/java/com/alibaba/nacos/naming/push/PushService.java index 56e9ba34704..3eb0d08d650 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/push/PushService.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/push/PushService.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.naming.misc.Loggers; import com.alibaba.nacos.naming.misc.SwitchDomain; import com.alibaba.nacos.naming.misc.UtilsAndCommons; +import com.alibaba.nacos.naming.pojo.Subscriber; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; import org.codehaus.jackson.util.VersionUtil; @@ -137,7 +138,7 @@ public void addClient(String namespaceId, String tenant, String app) { - PushClient client = new PushService.PushClient(namespaceId, + PushClient client = new PushClient(namespaceId, serviceName, clusters, agent, @@ -170,6 +171,19 @@ public static void addClient(PushClient client) { } } + public List getClients(String serviceName, String namespaceId) { + String serviceKey = UtilsAndCommons.assembleFullServiceName(namespaceId, serviceName); + ConcurrentMap clientConcurrentMap = clientMap.get(serviceKey); + if (Objects.isNull(clientConcurrentMap)) { + return null; + } + List clients = new ArrayList(); + clientConcurrentMap.forEach((key, client) -> { + clients.add(new Subscriber(client.getAddrStr(),client.getAgent(),client.getApp(),client.getIp(),namespaceId,serviceName)); + }); + return clients; + } + public static void removeClientIfZombie() { int size = 0; From 20af90b821496f5b0e362901fb0b8bf7e34817f7 Mon Sep 17 00:00:00 2001 From: Nicholas2015 <799327210@qq.com> Date: Wed, 29 May 2019 22:54:35 +0800 Subject: [PATCH 2/8] add license --- .../nacos/naming/core/SubscribeManager.java | 15 +++++++++++++++ .../alibaba/nacos/naming/pojo/Subscriber.java | 19 +++++++++++++++++++ .../nacos/naming/pojo/Subscribers.java | 19 +++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java index 9c03276b39d..d2fa64517fc 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java @@ -1,3 +1,18 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.naming.core; import com.alibaba.fastjson.JSONObject; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscriber.java b/naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscriber.java index 4cd08cb3813..6cea0ee38f6 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscriber.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscriber.java @@ -1,7 +1,26 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.naming.pojo; import java.io.Serializable; +/** + * @author nicholas + * @version $Id: Subscriber.java, v 0.1 2019-05-28 下午10:47 nicholas Exp $$ + */ public class Subscriber implements Serializable { private String addrStr; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscribers.java b/naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscribers.java index 4a9cd699b69..72b4080b6c3 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscribers.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/pojo/Subscribers.java @@ -1,8 +1,27 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.naming.pojo; import java.io.Serializable; import java.util.List; +/** + * @author nicholas + * @version $Id: Subscribers.java, v 0.1 2019-05-28 下午10:47 nicholas Exp $$ + */ public class Subscribers implements Serializable { private List subscribers; From bbb2108fd2720d53f980bc252f3ac385656299bb Mon Sep 17 00:00:00 2001 From: Nicholas2015 <799327210@qq.com> Date: Tue, 4 Jun 2019 17:10:27 +0800 Subject: [PATCH 3/8] code format and some code detail --- .../nacos/naming/controllers/ServiceController.java | 9 +++++---- .../alibaba/nacos/naming/core/SubscribeManager.java | 10 +++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ServiceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ServiceController.java index 410c50d3129..5cb6c65a3df 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ServiceController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ServiceController.java @@ -370,21 +370,22 @@ public JSONObject checksum(HttpServletRequest request) throws Exception { /** * get subscriber list + * * @param request * @return */ @RequestMapping(value = "/subscribers", method = RequestMethod.GET) - public JSONObject subscribers(HttpServletRequest request){ + public JSONObject subscribers(HttpServletRequest request) { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); - boolean aggregation = Boolean.valueOf(WebUtils.required(request,"aggregation")); + boolean aggregation = Boolean.valueOf(WebUtils.optional(request, "aggregation", String.valueOf(Boolean.TRUE))); JSONObject result = new JSONObject(); try { - List subscribers = subscribeManager.getSubscribers(serviceName,namespaceId,aggregation); - result.put("subscribers",subscribers); + List subscribers = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation); + result.put("subscribers", subscribers); return result; } catch (InterruptedException e) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java index d2fa64517fc..90a70e375b0 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java @@ -38,11 +38,12 @@ /** * @author Nicholas + * @since 1.0.1 */ @Service public class SubscribeManager { - private static final String DATA_ON_SYNC_URL = "/service/subscribers"; + private static final String SUBSCRIBER_ON_SYNC_URL = "/service/subscribers"; @Autowired private PushService pushService; @@ -67,8 +68,7 @@ public List getSubscribers(String serviceName, String namespaceId, b if (aggregation){ // size = 1 means only myself in the list, we need at least one another server alive: while (serverListManager.getHealthyServers().size() <= 1) { - Thread.sleep(1000L); - Loggers.EPHEMERAL.info("waiting server list init..."); + return getSubscribers(serviceName,namespaceId); } List subscriberList = new ArrayList(); @@ -78,13 +78,13 @@ public List getSubscribers(String serviceName, String namespaceId, b Map paramValues = new HashMap<>(128); paramValues.put("serviceName",serviceName); paramValues.put("namespaceId",namespaceId); - paramValues.put("aggregation",String.valueOf(!aggregation)); + paramValues.put("aggregation",String.valueOf(Boolean.FALSE)); if (NetUtils.localServer().equals(server.getKey())) { subscriberList.addAll(getSubscribers(serviceName,namespaceId)); } HttpClient.HttpResult result = HttpClient.httpGet("http://" + server.getKey() + RunningConfig.getContextPath() - + UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_ON_SYNC_URL, new ArrayList<>(),paramValues); + + UtilsAndCommons.NACOS_NAMING_CONTEXT + SUBSCRIBER_ON_SYNC_URL, new ArrayList<>(),paramValues); if (HttpURLConnection.HTTP_OK == result.code) { Subscribers subscribers = (Subscribers) JSONObject.parseObject(result.content, Subscribers.class); From dc61375c299092212460ef432c814febb9d251de Mon Sep 17 00:00:00 2001 From: Nicholas2015 <799327210@qq.com> Date: Wed, 5 Jun 2019 21:48:25 +0800 Subject: [PATCH 4/8] common params to constant --- .../java/com/alibaba/nacos/naming/core/SubscribeManager.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java index 90a70e375b0..c19af6436bc 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.naming.core; import com.alibaba.fastjson.JSONObject; +import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.naming.boot.RunningConfig; import com.alibaba.nacos.naming.cluster.ServerListManager; import com.alibaba.nacos.naming.cluster.servers.Server; @@ -76,8 +77,8 @@ public List getSubscribers(String serviceName, String namespaceId, b for (Server server : serverListManager.getHealthyServers()) { Map paramValues = new HashMap<>(128); - paramValues.put("serviceName",serviceName); - paramValues.put("namespaceId",namespaceId); + paramValues.put(CommonParams.SERVICE_NAME,serviceName); + paramValues.put(CommonParams.NAMESPACE_ID,namespaceId); paramValues.put("aggregation",String.valueOf(Boolean.FALSE)); if (NetUtils.localServer().equals(server.getKey())) { subscriberList.addAll(getSubscribers(serviceName,namespaceId)); From 7d52d47f741359f9aa7ea6e3f009ab9113be6169 Mon Sep 17 00:00:00 2001 From: Nicholas2015 <799327210@qq.com> Date: Sat, 8 Jun 2019 20:52:10 +0800 Subject: [PATCH 5/8] add test case --- .../nacos/naming/core/SubscribeManager.java | 23 +++-- .../naming/core/SubscribeManagerTest.java | 90 +++++++++++++++++++ .../nacos/naming/pojo/SubscriberTest.java | 36 ++++++++ 3 files changed, 137 insertions(+), 12 deletions(-) create mode 100644 naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java create mode 100644 naming/src/test/java/com/alibaba/nacos/naming/pojo/SubscriberTest.java diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java index c19af6436bc..5eb5a686f04 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java @@ -53,12 +53,11 @@ public class SubscribeManager { private ServerListManager serverListManager; - private List getSubscribers(String serviceName, String namespaceId){ - return pushService.getClients(serviceName,namespaceId); + private List getSubscribers(String serviceName, String namespaceId) { + return pushService.getClients(serviceName, namespaceId); } /** - * * @param serviceName * @param namespaceId * @param aggregation @@ -66,10 +65,10 @@ private List getSubscribers(String serviceName, String namespaceId){ * @throws InterruptedException */ public List getSubscribers(String serviceName, String namespaceId, boolean aggregation) throws InterruptedException { - if (aggregation){ + if (aggregation) { // size = 1 means only myself in the list, we need at least one another server alive: - while (serverListManager.getHealthyServers().size() <= 1) { - return getSubscribers(serviceName,namespaceId); + if (serverListManager.getHealthyServers().size() <= 1) { + return getSubscribers(serviceName, namespaceId); } List subscriberList = new ArrayList(); @@ -77,15 +76,15 @@ public List getSubscribers(String serviceName, String namespaceId, b for (Server server : serverListManager.getHealthyServers()) { Map paramValues = new HashMap<>(128); - paramValues.put(CommonParams.SERVICE_NAME,serviceName); - paramValues.put(CommonParams.NAMESPACE_ID,namespaceId); - paramValues.put("aggregation",String.valueOf(Boolean.FALSE)); + paramValues.put(CommonParams.SERVICE_NAME, serviceName); + paramValues.put(CommonParams.NAMESPACE_ID, namespaceId); + paramValues.put("aggregation", String.valueOf(Boolean.FALSE)); if (NetUtils.localServer().equals(server.getKey())) { - subscriberList.addAll(getSubscribers(serviceName,namespaceId)); + subscriberList.addAll(getSubscribers(serviceName, namespaceId)); } HttpClient.HttpResult result = HttpClient.httpGet("http://" + server.getKey() + RunningConfig.getContextPath() - + UtilsAndCommons.NACOS_NAMING_CONTEXT + SUBSCRIBER_ON_SYNC_URL, new ArrayList<>(),paramValues); + + UtilsAndCommons.NACOS_NAMING_CONTEXT + SUBSCRIBER_ON_SYNC_URL, new ArrayList<>(), paramValues); if (HttpURLConnection.HTTP_OK == result.code) { Subscribers subscribers = (Subscribers) JSONObject.parseObject(result.content, Subscribers.class); @@ -96,7 +95,7 @@ public List getSubscribers(String serviceName, String namespaceId, b } } else { // local server - return getSubscribers(serviceName,namespaceId); + return getSubscribers(serviceName, namespaceId); } return Collections.emptyList(); } diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java new file mode 100644 index 00000000000..97960f06102 --- /dev/null +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java @@ -0,0 +1,90 @@ +package com.alibaba.nacos.naming.core; + +import com.alibaba.nacos.naming.BaseTest; +import com.alibaba.nacos.naming.cluster.ServerListManager; +import com.alibaba.nacos.naming.cluster.servers.Server; +import com.alibaba.nacos.naming.pojo.Subscriber; +import com.alibaba.nacos.naming.push.PushService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Nicholas + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class SubscribeManagerTest extends BaseTest { + + @Mock + private SubscribeManager subscribeManager; + + @Mock + private PushService pushService; + + @Mock + private ServerListManager serverListManager; + + @Before + public void before() { + super.before(); + subscribeManager = new SubscribeManager(); + } + + @Test + public void getSubscribersWithFalse() { + String serviceName = "test"; + String namespaceId = "public"; + boolean aggregation = Boolean.FALSE; + try { + List clients = new ArrayList(); + Subscriber subscriber = new Subscriber("127.0.0.1:8080", "test", "app", "127.0.0.1", namespaceId, serviceName); + clients.add(subscriber); + Mockito.when(pushService.getClients(Mockito.anyString(), Mockito.anyString())).thenReturn(clients); + subscribeManager.getSubscribers(serviceName, namespaceId, aggregation); + } catch (Exception e) { + + } + } + + @Test + public void getSubscribersWithTrue() { + String serviceName = "test"; + String namespaceId = "public"; + boolean aggregation = Boolean.TRUE; + try { + List clients = new ArrayList(); + Subscriber subscriber = new Subscriber("127.0.0.1:8080", "test", "app", "127.0.0.1", namespaceId, serviceName); + clients.add(subscriber); + + List healthyServers = new ArrayList<>(); + + for (int i = 0; i <= 2; i++) { + Server server = new Server(); + server.setIp("127.0.0.1"); + server.setServePort(8080 + i); + server.setAlive(Boolean.TRUE); + server.setAdWeight(10); + server.setLastRefTime(System.currentTimeMillis()); + server.setLastRefTimeStr(String.valueOf(System.currentTimeMillis())); + server.setSite("site"); + server.setWeight(1); + healthyServers.add(server); + } + + Mockito.when(serverListManager.getHealthyServers()).thenReturn(healthyServers); + //Mockito.doReturn(3).when(serverListManager.getHealthyServers().size()); + subscribeManager.getSubscribers(serviceName, namespaceId, aggregation); + } catch (Exception e) { + + } + } +} + diff --git a/naming/src/test/java/com/alibaba/nacos/naming/pojo/SubscriberTest.java b/naming/src/test/java/com/alibaba/nacos/naming/pojo/SubscriberTest.java new file mode 100644 index 00000000000..d41222ede6f --- /dev/null +++ b/naming/src/test/java/com/alibaba/nacos/naming/pojo/SubscriberTest.java @@ -0,0 +1,36 @@ +package com.alibaba.nacos.naming.pojo; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Nicholas + */ +public class SubscriberTest { + + @Test + public void subscriberBeanTest() { + Subscriber subscriber = new Subscriber("127.0.0.1:8080", "agent", "app", "127.0.0.1", "public", "test"); + subscriber.setAddrStr("127.0.0.1:8080"); + subscriber.setIp("127.0.0.1"); + subscriber.setApp("app"); + subscriber.setAgent("agent"); + subscriber.setNamespaceId("public"); + subscriber.setServiceName("test"); + + subscriber.getAddrStr(); + subscriber.getIp(); + subscriber.getAgent(); + subscriber.getApp(); + subscriber.getNamespaceId(); + subscriber.getServiceName(); + + Subscribers subscribers = new Subscribers(); + List subscriberList = new ArrayList<>(); + subscriberList.add(subscriber); + subscribers.setSubscribers(subscriberList); + subscribers.getSubscribers(); + } +} From e1c1b32ae701fbbf95e432dc23a11a7874a43cb5 Mon Sep 17 00:00:00 2001 From: Nicholas2015 <799327210@qq.com> Date: Sun, 9 Jun 2019 20:27:16 +0800 Subject: [PATCH 6/8] add assert --- .../nacos/naming/core/SubscribeManagerTest.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java index 97960f06102..ec12e61d16b 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java @@ -5,6 +5,7 @@ import com.alibaba.nacos.naming.cluster.servers.Server; import com.alibaba.nacos.naming.pojo.Subscriber; import com.alibaba.nacos.naming.push.PushService; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -48,7 +49,10 @@ public void getSubscribersWithFalse() { Subscriber subscriber = new Subscriber("127.0.0.1:8080", "test", "app", "127.0.0.1", namespaceId, serviceName); clients.add(subscriber); Mockito.when(pushService.getClients(Mockito.anyString(), Mockito.anyString())).thenReturn(clients); - subscribeManager.getSubscribers(serviceName, namespaceId, aggregation); + List list = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation); + Assert.assertNotNull(list); + Assert.assertEquals(1, list.size()); + Assert.assertEquals("public", list.get(0).getNamespaceId()); } catch (Exception e) { } @@ -81,7 +85,10 @@ public void getSubscribersWithTrue() { Mockito.when(serverListManager.getHealthyServers()).thenReturn(healthyServers); //Mockito.doReturn(3).when(serverListManager.getHealthyServers().size()); - subscribeManager.getSubscribers(serviceName, namespaceId, aggregation); + List list = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation); + Assert.assertNotNull(list); + Assert.assertEquals(1, list.size()); + Assert.assertEquals("public", list.get(0).getNamespaceId()); } catch (Exception e) { } From 6be91e9b4565b715766771b71d64947ae7309957 Mon Sep 17 00:00:00 2001 From: Nicholas2015 <799327210@qq.com> Date: Sun, 9 Jun 2019 21:33:56 +0800 Subject: [PATCH 7/8] add assert --- .../com/alibaba/nacos/naming/core/SubscribeManagerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java index ec12e61d16b..e6b16345d96 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/SubscribeManagerTest.java @@ -87,7 +87,7 @@ public void getSubscribersWithTrue() { //Mockito.doReturn(3).when(serverListManager.getHealthyServers().size()); List list = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation); Assert.assertNotNull(list); - Assert.assertEquals(1, list.size()); + Assert.assertEquals(2, list.size()); Assert.assertEquals("public", list.get(0).getNamespaceId()); } catch (Exception e) { From 13331c50d1c452fc0016bf2d5310179bfcdbbe31 Mon Sep 17 00:00:00 2001 From: Nicholas2015 <799327210@qq.com> Date: Thu, 13 Jun 2019 22:41:54 +0800 Subject: [PATCH 8/8] add assert --- .../com/alibaba/nacos/naming/pojo/SubscriberTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/naming/src/test/java/com/alibaba/nacos/naming/pojo/SubscriberTest.java b/naming/src/test/java/com/alibaba/nacos/naming/pojo/SubscriberTest.java index d41222ede6f..179230d1753 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/pojo/SubscriberTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/pojo/SubscriberTest.java @@ -1,5 +1,6 @@ package com.alibaba.nacos.naming.pojo; +import org.junit.Assert; import org.junit.Test; import java.util.ArrayList; @@ -32,5 +33,14 @@ public void subscriberBeanTest() { subscriberList.add(subscriber); subscribers.setSubscribers(subscriberList); subscribers.getSubscribers(); + + Assert.assertNotNull(subscriberList); + Assert.assertEquals(1, subscriberList.size()); + Assert.assertEquals("127.0.0.1:8080", subscriberList.get(0).getAddrStr()); + Assert.assertEquals("127.0.0.1", subscriberList.get(0).getIp()); + Assert.assertEquals("app", subscriberList.get(0).getApp()); + Assert.assertEquals("agent", subscriberList.get(0).getAgent()); + Assert.assertEquals("public", subscriberList.get(0).getNamespaceId()); + Assert.assertEquals("test", subscriberList.get(0).getServiceName()); } }