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 01/18] 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 47c7683ff..410c50d31 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.core.*; 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.RequestMethod; 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 class ServiceController { 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 000000000..9c03276b3 --- /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 000000000..4cd08cb38 --- /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 000000000..4a9cd699b --- /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 56e9ba347..3eb0d08d6 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.fastjson.JSON; 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 class PushService { String tenant, String app) { - PushClient client = new PushService.PushClient(namespaceId, + PushClient client = new PushClient(namespaceId, serviceName, clusters, agent, @@ -170,6 +171,19 @@ public class PushService { } } + 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 02/18] 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 9c03276b3..d2fa64517 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 4cd08cb38..6cea0ee38 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 4a9cd699b..72b4080b6 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 03/18] 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 410c50d31..5cb6c65a3 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 class ServiceController { /** * 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 d2fa64517..90a70e375 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 @@ import java.util.stream.Collectors; /** * @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 class SubscribeManager { 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 class SubscribeManager { 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 04/18] 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 90a70e375..c19af6436 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 class SubscribeManager { 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 05/18] 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 c19af6436..5eb5a686f 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 @@ public class SubscribeManager { * @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 class SubscribeManager { 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 class SubscribeManager { } } 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 000000000..97960f061 --- /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 000000000..d41222ede --- /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 06/18] 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 97960f061..ec12e61d1 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.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.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -48,7 +49,10 @@ public class SubscribeManagerTest extends BaseTest { 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 class SubscribeManagerTest extends BaseTest { 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 07/18] 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 ec12e61d1..e6b16345d 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 class SubscribeManagerTest extends BaseTest { //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 266f81a01e0ec8c5667df6ac436b3874c0242626 Mon Sep 17 00:00:00 2001 From: IanCao Date: Thu, 13 Jun 2019 09:57:11 +0800 Subject: [PATCH 08/18] format code --- .../nacos/client/naming/backups/FailoverReactor.java | 2 +- .../com/alibaba/nacos/client/naming/cache/DiskCache.java | 7 +++---- .../com/alibaba/nacos/client/naming/core/Balancer.java | 9 ++------- .../alibaba/nacos/client/naming/core/HostReactor.java | 3 --- .../alibaba/nacos/client/naming/core/PushReceiver.java | 2 +- .../nacos/naming/healthcheck/TcpSuperSenseProcessor.java | 4 ++-- .../java/com/alibaba/nacos/naming/misc/HttpClient.java | 4 ++-- 7 files changed, 11 insertions(+), 20 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 88b0ff30c..bc10aa5d9 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -118,7 +118,7 @@ public class FailoverReactor { String failover = ConcurrentDiskUtil.getFileContent(failoverDir + UtilAndComs.FAILOVER_SWITCH, Charset.defaultCharset().toString()); if (!StringUtils.isEmpty(failover)) { - List lines = Arrays.asList(failover.split(DiskCache.getLineSeperator())); + List lines = Arrays.asList(failover.split(DiskCache.getLineSeparator())); for (String line : lines) { String line1 = line.trim(); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/cache/DiskCache.java b/client/src/main/java/com/alibaba/nacos/client/naming/cache/DiskCache.java index 2e7cad6c9..6fe5a4a72 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/cache/DiskCache.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/cache/DiskCache.java @@ -71,9 +71,8 @@ public class DiskCache { } } - public static String getLineSeperator() { - String lineSeparator = System.getProperty("line.separator"); - return lineSeparator; + public static String getLineSeparator() { + return System.getProperty("line.separator"); } public static Map read(String cacheDir) { @@ -82,7 +81,7 @@ public class DiskCache { BufferedReader reader = null; try { File[] files = makeSureCacheDirExists(cacheDir).listFiles(); - if (files == null) { + if (files == null || files.length == 0) { return domMap; } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/Balancer.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/Balancer.java index f598c1410..48109a918 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/Balancer.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/Balancer.java @@ -40,7 +40,7 @@ public class Balancer { public static class RandomByWeight { public static List selectAll(ServiceInfo serviceInfo) { - List hosts = nothing(serviceInfo); + List hosts = serviceInfo.getHosts(); if (CollectionUtils.isEmpty(hosts)) { throw new IllegalStateException("no host to srv for serviceInfo: " + serviceInfo.getName()); @@ -59,10 +59,6 @@ public class Balancer { return getHostByRandomWeight(hosts); } - - public static List nothing(ServiceInfo serviceInfo) { - return serviceInfo.getHosts(); - } } /** @@ -91,7 +87,6 @@ public class Balancer { NAMING_LOGGER.debug("for (Host host : hosts)"); vipChooser.refresh(hostsWithWeight); NAMING_LOGGER.debug("vipChooser.refresh"); - Instance host = vipChooser.randomWithWeight(); - return host; + return vipChooser.randomWithWeight(); } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java index e43dd6763..3e1e7e5a1 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java @@ -140,9 +140,7 @@ public class HostReactor { if (!oldHostMap.containsKey(key)) { newHosts.add(host); - continue; } - } for (Map.Entry entry : oldHostMap.entrySet()) { @@ -154,7 +152,6 @@ public class HostReactor { if (!newHostMap.containsKey(key)) { remvHosts.add(host); - continue; } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java index cbb53d924..b133f6574 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java @@ -35,7 +35,7 @@ public class PushReceiver implements Runnable { private ScheduledExecutorService executorService; - public static final int UDP_MSS = 64 * 1024; + private static final int UDP_MSS = 64 * 1024; private DatagramSocket udpSocket; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/TcpSuperSenseProcessor.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/TcpSuperSenseProcessor.java index a842208a4..812c7bcf0 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/TcpSuperSenseProcessor.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/TcpSuperSenseProcessor.java @@ -59,13 +59,13 @@ public class TcpSuperSenseProcessor implements HealthCheckProcessor, Runnable { /** * this value has been carefully tuned, do not modify unless you're confident */ - public static final int NIO_THREAD_COUNT = Runtime.getRuntime().availableProcessors() <= 1 ? + private static final int NIO_THREAD_COUNT = Runtime.getRuntime().availableProcessors() <= 1 ? 1 : Runtime.getRuntime().availableProcessors() / 2; /** * because some hosts doesn't support keep-alive connections, disabled temporarily */ - public static final long TCP_KEEP_ALIVE_MILLIS = 0; + private static final long TCP_KEEP_ALIVE_MILLIS = 0; private static ScheduledExecutorService TCP_CHECK_EXECUTOR = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java index 1a356661d..ce8b7a8d9 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java @@ -54,8 +54,8 @@ import java.util.zip.GZIPInputStream; * @author nacos */ public class HttpClient { - public static final int TIME_OUT_MILLIS = 10000; - public static final int CON_TIME_OUT_MILLIS = 5000; + private static final int TIME_OUT_MILLIS = 10000; + private static final int CON_TIME_OUT_MILLIS = 5000; private static AsyncHttpClient asyncHttpClient; From a98db447fec829302e5a4a948d669d321460ddc7 Mon Sep 17 00:00:00 2001 From: IanCao Date: Thu, 13 Jun 2019 12:26:22 +0800 Subject: [PATCH 09/18] format code --- .../client/naming/utils/UtilAndComs.java | 8 ++++---- .../nacos/cmdb/memory/CmdbProvider.java | 2 -- .../alibaba/nacos/common/util/IoUtils.java | 20 +++++++++++-------- .../config/server/monitor/MemoryMonitor.java | 2 -- .../server/service/DiskServiceUnitTest.java | 2 -- .../alibaba/nacos/core/utils/InetUtils.java | 3 +-- .../alibaba/nacos/core/utils/SystemUtils.java | 4 ++-- .../naming/cluster/ServerListManager.java | 2 +- .../cluster/transport/FastJsonSerializer.java | 8 +++++--- .../alibaba/nacos/naming/core/Instance.java | 12 +++++------ .../healthcheck/HttpHealthCheckProcessor.java | 2 +- .../healthcheck/TcpSuperSenseProcessor.java | 1 - .../alibaba/nacos/naming/misc/HttpClient.java | 2 -- .../monitor/PerformanceLoggerThread.java | 1 - .../nacos/naming/push/PushService.java | 2 +- 15 files changed, 33 insertions(+), 38 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java index 9736560dc..0d2e868d5 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java @@ -22,13 +22,13 @@ public class UtilAndComs { public static final String VERSION = "Nacos-Java-Client:v1.0.1"; - public static String WEB_CONTEXT = "/nacos"; + public static final String WEB_CONTEXT = "/nacos"; - public static String NACOS_URL_BASE = WEB_CONTEXT + "/v1/ns"; + public static final String NACOS_URL_BASE = WEB_CONTEXT + "/v1/ns"; - public static String NACOS_URL_INSTANCE = NACOS_URL_BASE + "/instance"; + public static final String NACOS_URL_INSTANCE = NACOS_URL_BASE + "/instance"; - public static String NACOS_URL_SERVICE = NACOS_URL_BASE + "/service"; + public static final String NACOS_URL_SERVICE = NACOS_URL_BASE + "/service"; public static final String ENCODING = "UTF-8"; diff --git a/cmdb/src/main/java/com/alibaba/nacos/cmdb/memory/CmdbProvider.java b/cmdb/src/main/java/com/alibaba/nacos/cmdb/memory/CmdbProvider.java index 7ad078c0f..c0a6cd3ea 100644 --- a/cmdb/src/main/java/com/alibaba/nacos/cmdb/memory/CmdbProvider.java +++ b/cmdb/src/main/java/com/alibaba/nacos/cmdb/memory/CmdbProvider.java @@ -54,8 +54,6 @@ public class CmdbProvider implements CmdbReader, CmdbWriter { private Set entityTypeSet = new HashSet<>(); - private List eventList = new ArrayList<>(); - private long eventTimestamp = System.currentTimeMillis(); public CmdbProvider() throws NacosException { diff --git a/common/src/main/java/com/alibaba/nacos/common/util/IoUtils.java b/common/src/main/java/com/alibaba/nacos/common/util/IoUtils.java index 09beef788..9d445a66e 100644 --- a/common/src/main/java/com/alibaba/nacos/common/util/IoUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/util/IoUtils.java @@ -31,25 +31,29 @@ import java.util.zip.GZIPInputStream; public class IoUtils { public static byte[] tryDecompress(InputStream raw) throws Exception { - + GZIPInputStream gis = null; + ByteArrayOutputStream out = null; try { - GZIPInputStream gis - = new GZIPInputStream(raw); - ByteArrayOutputStream out - = new ByteArrayOutputStream(); - + gis = new GZIPInputStream(raw); + out = new ByteArrayOutputStream(); IOUtils.copy(gis, out); - return out.toByteArray(); } catch (Exception e) { e.printStackTrace(); + } finally { + if (out != null) { + out.close(); + } + if (gis != null) { + gis.close(); + } } return null; } static private BufferedReader toBufferedReader(Reader reader) { - return reader instanceof BufferedReader ? (BufferedReader)reader : new BufferedReader( + return reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader( reader); } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/monitor/MemoryMonitor.java b/config/src/main/java/com/alibaba/nacos/config/server/monitor/MemoryMonitor.java index f96f6a706..b347caf41 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/monitor/MemoryMonitor.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/monitor/MemoryMonitor.java @@ -82,8 +82,6 @@ class PrintMemoryTask implements Runnable { class NotifyTaskQueueMonitorTask implements Runnable { final private AsyncNotifyService notifySingleService; - private AtomicInteger notifyTask = new AtomicInteger(); - NotifyTaskQueueMonitorTask(AsyncNotifyService notifySingleService) { this.notifySingleService = notifySingleService; diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/DiskServiceUnitTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/DiskServiceUnitTest.java index 6863ed5e9..96e79c2af 100755 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/DiskServiceUnitTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/DiskServiceUnitTest.java @@ -34,8 +34,6 @@ public class DiskServiceUnitTest { private DiskUtil diskService; - private ServletContext servletContext; - private File tempFile; private String path; diff --git a/core/src/main/java/com/alibaba/nacos/core/utils/InetUtils.java b/core/src/main/java/com/alibaba/nacos/core/utils/InetUtils.java index 6705799cd..e1543c1ed 100644 --- a/core/src/main/java/com/alibaba/nacos/core/utils/InetUtils.java +++ b/core/src/main/java/com/alibaba/nacos/core/utils/InetUtils.java @@ -114,8 +114,7 @@ public class InetUtils { log.info("Testing interface: " + ifc.getDisplayName()); if (ifc.getIndex() < lowest || result == null) { lowest = ifc.getIndex(); - } - else if (result != null) { + } else if (result != null) { continue; } diff --git a/core/src/main/java/com/alibaba/nacos/core/utils/SystemUtils.java b/core/src/main/java/com/alibaba/nacos/core/utils/SystemUtils.java index 6eec1e88e..53ad39752 100644 --- a/core/src/main/java/com/alibaba/nacos/core/utils/SystemUtils.java +++ b/core/src/main/java/com/alibaba/nacos/core/utils/SystemUtils.java @@ -42,7 +42,7 @@ public class SystemUtils { /** * Standalone mode or not */ - public static boolean STANDALONE_MODE = Boolean.getBoolean(STANDALONE_MODE_PROPERTY_NAME); + public static final boolean STANDALONE_MODE = Boolean.getBoolean(STANDALONE_MODE_PROPERTY_NAME); public static final String STANDALONE_MODE_ALONE = "standalone"; public static final String STANDALONE_MODE_CLUSTER = "cluster"; @@ -50,7 +50,7 @@ public class SystemUtils { /** * server */ - public static String FUNCTION_MODE = System.getProperty(FUNCTION_MODE_PROPERTY_NAME); + public static final String FUNCTION_MODE = System.getProperty(FUNCTION_MODE_PROPERTY_NAME); public static final String FUNCTION_MODE_CONFIG = "config"; public static final String FUNCTION_MODE_NAMING = "naming"; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/cluster/ServerListManager.java b/naming/src/main/java/com/alibaba/nacos/naming/cluster/ServerListManager.java index 639ea7203..b00231adc 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/cluster/ServerListManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/cluster/ServerListManager.java @@ -56,7 +56,7 @@ public class ServerListManager { private Set liveSites = new HashSet<>(); - public final String LOCALHOST_SITE = UtilsAndCommons.UNKNOWN_SITE; + private final static String LOCALHOST_SITE = UtilsAndCommons.UNKNOWN_SITE; private long lastHealthServerMillis = 0L; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/cluster/transport/FastJsonSerializer.java b/naming/src/main/java/com/alibaba/nacos/naming/cluster/transport/FastJsonSerializer.java index f0f0965cf..26c869cc1 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/cluster/transport/FastJsonSerializer.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/cluster/transport/FastJsonSerializer.java @@ -27,6 +27,8 @@ import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; +import static org.apache.commons.lang3.CharEncoding.UTF_8; + /** * Use FastJSON to serialize data * @@ -44,7 +46,7 @@ public class FastJsonSerializer implements Serializer { @Override public T deserialize(byte[] data, Class clazz) { try { - return JSON.parseObject(new String(data, "UTF-8"), clazz); + return JSON.parseObject(new String(data, UTF_8), clazz); } catch (UnsupportedEncodingException e) { return null; } @@ -53,7 +55,7 @@ public class FastJsonSerializer implements Serializer { @Override public T deserialize(byte[] data, TypeReference clazz) { try { - String dataString = new String(data, "UTF-8"); + String dataString = new String(data, UTF_8); return JSON.parseObject(dataString, clazz); } catch (Exception e) { Loggers.SRV_LOG.error("deserialize data failed.", e); @@ -64,7 +66,7 @@ public class FastJsonSerializer implements Serializer { @Override public Map> deserializeMap(byte[] data, Class clazz) { try { - String dataString = new String(data, "UTF-8"); + String dataString = new String(data, UTF_8); Map dataMap = JSON.parseObject(dataString, new TypeReference>() { }); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java b/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java index c87294fdf..c4612a66d 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java @@ -34,7 +34,7 @@ import java.util.regex.Pattern; public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance implements Comparable { private static final double MAX_WEIGHT_VALUE = 10000.0D; - private static final double MIN_POSTIVE_WEIGHT_VALUE = 0.01D; + private static final double MIN_POSITIVE_WEIGHT_VALUE = 0.01D; private static final double MIN_WEIGHT_VALUE = 0.00D; private volatile long lastBeat = System.currentTimeMillis(); @@ -48,13 +48,13 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme private String app; - public static final Pattern IP_PATTERN + private static final Pattern IP_PATTERN = Pattern.compile("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):?(\\d{1,5})?"); - public static final Pattern ONLY_DIGIT_AND_DOT + private static final Pattern ONLY_DIGIT_AND_DOT = Pattern.compile("(\\d|\\.)+"); - public static final String SPLITER = "_"; + private static final String SPLITER = "_"; public Instance() { } @@ -191,8 +191,8 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme ip.setWeight(MAX_WEIGHT_VALUE); } - if (ip.getWeight() < MIN_POSTIVE_WEIGHT_VALUE && ip.getWeight() > MIN_WEIGHT_VALUE) { - ip.setWeight(MIN_POSTIVE_WEIGHT_VALUE); + if (ip.getWeight() < MIN_POSITIVE_WEIGHT_VALUE && ip.getWeight() > MIN_WEIGHT_VALUE) { + ip.setWeight(MIN_POSITIVE_WEIGHT_VALUE); } else if (ip.getWeight() < MIN_WEIGHT_VALUE) { ip.setWeight(0.0D); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HttpHealthCheckProcessor.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HttpHealthCheckProcessor.java index 3795a3453..064de1e12 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HttpHealthCheckProcessor.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HttpHealthCheckProcessor.java @@ -55,7 +55,7 @@ public class HttpHealthCheckProcessor implements HealthCheckProcessor { private static AsyncHttpClient asyncHttpClient; - public static final int CONNECT_TIMEOUT_MS = 500; + private static final int CONNECT_TIMEOUT_MS = 500; static { try { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/TcpSuperSenseProcessor.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/TcpSuperSenseProcessor.java index 812c7bcf0..f33859ea0 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/TcpSuperSenseProcessor.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/TcpSuperSenseProcessor.java @@ -111,7 +111,6 @@ public class TcpSuperSenseProcessor implements HealthCheckProcessor, Runnable { if (CollectionUtils.isEmpty(ips)) { return; } - Service service = task.getCluster().getService(); for (Instance ip : ips) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java index ce8b7a8d9..856518dbc 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java @@ -61,8 +61,6 @@ public class HttpClient { private static CloseableHttpClient postClient; - private static PoolingHttpClientConnectionManager connectionManager; - static { AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder(); builder.setMaximumConnectionsTotal(-1); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/monitor/PerformanceLoggerThread.java b/naming/src/main/java/com/alibaba/nacos/naming/monitor/PerformanceLoggerThread.java index 9d4e62e34..b33c0138f 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/monitor/PerformanceLoggerThread.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/monitor/PerformanceLoggerThread.java @@ -134,7 +134,6 @@ public class PerformanceLoggerThread { try { int serviceCount = serviceManager.getServiceCount(); int ipCount = serviceManager.getInstanceCount(); - long maxPushMaxCost = getMaxPushCost(); long maxPushCost = getMaxPushCost(); long avgPushCost = getAvgPushCost(); 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 e052af559..86adf7ba3 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 @@ -46,7 +46,7 @@ public class PushService { @Autowired private SwitchDomain switchDomain; - public static final long ACK_TIMEOUT_NANOS = TimeUnit.SECONDS.toNanos(10L); + private static final long ACK_TIMEOUT_NANOS = TimeUnit.SECONDS.toNanos(10L); private static final int MAX_RETRY_TIMES = 1; From aae3b72aa05eda0460a5fdb48b7961d6ee778255 Mon Sep 17 00:00:00 2001 From: IanCao Date: Thu, 13 Jun 2019 14:38:00 +0800 Subject: [PATCH 10/18] fix bug --- .../alibaba/nacos/client/naming/utils/UtilAndComs.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java index 0d2e868d5..9736560dc 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java @@ -22,13 +22,13 @@ public class UtilAndComs { public static final String VERSION = "Nacos-Java-Client:v1.0.1"; - public static final String WEB_CONTEXT = "/nacos"; + public static String WEB_CONTEXT = "/nacos"; - public static final String NACOS_URL_BASE = WEB_CONTEXT + "/v1/ns"; + public static String NACOS_URL_BASE = WEB_CONTEXT + "/v1/ns"; - public static final String NACOS_URL_INSTANCE = NACOS_URL_BASE + "/instance"; + public static String NACOS_URL_INSTANCE = NACOS_URL_BASE + "/instance"; - public static final String NACOS_URL_SERVICE = NACOS_URL_BASE + "/service"; + public static String NACOS_URL_SERVICE = NACOS_URL_BASE + "/service"; public static final String ENCODING = "UTF-8"; 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 11/18] 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 d41222ede..179230d17 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 class SubscriberTest { 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()); } } From 10aa620315c1d6a61224b8b4efeeba86eac49b22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=A6=B9=E5=85=89?= Date: Fri, 14 Jun 2019 18:54:04 +0800 Subject: [PATCH 12/18] All overriding methods must be preceded by @Override annotations. --- .../naming/pojo/AbstractHealthChecker.java | 1 + .../client/config/http/ServerHttpAgent.java | 1 + .../nacos/client/config/impl/CacheData.java | 1 + .../client/config/impl/TimerService.java | 1 + .../client/identify/CredentialService.java | 1 + .../nacos/client/identify/Credentials.java | 2 ++ .../naming/backups/FailoverReactor.java | 1 + .../client/naming/utils/GenericPoller.java | 2 ++ .../nacos/client/naming/utils/JvmRandom.java | 9 +++++++++ .../naming/utils/ThreadLocalRandom.java | 3 +++ .../config/server/manager/TaskManager.java | 2 ++ .../config/server/model/ConfigInfoBase.java | 1 + .../config/server/model/app/GroupKey.java | 1 + .../server/service/DynamicDataSource.java | 1 + .../server/service/LongPollingService.java | 1 + .../config/server/service/PersistService.java | 19 +++++++++++++++++++ .../service/notify/AsyncNotifyService.java | 2 ++ .../config/server/utils/SimpleFlowData.java | 2 ++ .../config/server/utils/SimpleIPFlowData.java | 2 ++ .../alibaba/nacos/naming/core/Service.java | 1 + .../nacos/naming/healthcheck/RsInfo.java | 1 + .../web/OverrideParameterRequestWrapper.java | 1 + 22 files changed, 56 insertions(+) diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/AbstractHealthChecker.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/AbstractHealthChecker.java index 182c229e9..f719c7d76 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/AbstractHealthChecker.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/AbstractHealthChecker.java @@ -193,6 +193,7 @@ public abstract class AbstractHealthChecker implements Cloneable { } + @Override public Tcp clone() throws CloneNotSupportedException { Tcp config = new Tcp(); config.setType(this.type); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java index aa2bf35ef..094393edd 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java @@ -426,6 +426,7 @@ public class ServerHttpAgent implements HttpAgent { return code; } + @Override public String toString() { return "STSCredential{" + "accessKeyId='" + accessKeyId + '\'' + diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java index 47c1d262d..f59462753 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java @@ -167,6 +167,7 @@ public class CacheData { final Listener listener = listenerWrap.listener; Runnable job = new Runnable() { + @Override public void run() { ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader appClassLoader = listener.getClass().getClassLoader(); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/TimerService.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/TimerService.java index 88e828ae0..5b0ceaf00 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/TimerService.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/TimerService.java @@ -36,6 +36,7 @@ public class TimerService { @SuppressWarnings("PMD.ThreadPoolCreationRule") static ScheduledExecutorService scheduledExecutor = Executors .newSingleThreadScheduledExecutor(new ThreadFactory() { + @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setName("com.alibaba.nacos.client.Timer"); diff --git a/client/src/main/java/com/alibaba/nacos/client/identify/CredentialService.java b/client/src/main/java/com/alibaba/nacos/client/identify/CredentialService.java index cb1c39e85..4ce6edb67 100644 --- a/client/src/main/java/com/alibaba/nacos/client/identify/CredentialService.java +++ b/client/src/main/java/com/alibaba/nacos/client/identify/CredentialService.java @@ -85,6 +85,7 @@ public final class CredentialService implements SpasCredentialLoader { LOGGER.info("[{}] {} is freed", appName, this.getClass().getSimpleName()); } + @Override public Credentials getCredential() { Credentials localCredential = credentials; if (localCredential.valid()) { diff --git a/client/src/main/java/com/alibaba/nacos/client/identify/Credentials.java b/client/src/main/java/com/alibaba/nacos/client/identify/Credentials.java index 36f698c95..5f865cc5e 100644 --- a/client/src/main/java/com/alibaba/nacos/client/identify/Credentials.java +++ b/client/src/main/java/com/alibaba/nacos/client/identify/Credentials.java @@ -38,6 +38,7 @@ public class Credentials implements SpasCredential { this(null, null, null); } + @Override public String getAccessKey() { return accessKey; } @@ -46,6 +47,7 @@ public class Credentials implements SpasCredential { this.accessKey = accessKey; } + @Override public String getSecretKey() { return secretKey; } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 88b0ff30c..052a0e8fa 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -212,6 +212,7 @@ public class FailoverReactor { } class DiskFileWriter extends TimerTask { + @Override public void run() { Map map = hostReactor.getServiceInfoMap(); for (Map.Entry entry : map.entrySet()) { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/GenericPoller.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/GenericPoller.java index 08e302b00..e5e40930d 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/GenericPoller.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/GenericPoller.java @@ -31,10 +31,12 @@ public class GenericPoller implements Poller { this.items = items; } + @Override public T next() { return items.get(Math.abs(index.getAndIncrement() % items.size())); } + @Override public Poller refresh(List items) { return new GenericPoller(items); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/JvmRandom.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/JvmRandom.java index b9c9092a1..362c1747c 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/JvmRandom.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/JvmRandom.java @@ -63,6 +63,7 @@ public final class JvmRandom extends Random { * @param seed ignored * @throws UnsupportedOperationException */ + @Override public synchronized void setSeed(long seed) { if (this.constructed) { throw new UnsupportedOperationException(); @@ -75,6 +76,7 @@ public final class JvmRandom extends Random { * @return Nothing, this method always throws an UnsupportedOperationException. * @throws UnsupportedOperationException */ + @Override public synchronized double nextGaussian() { throw new UnsupportedOperationException(); } @@ -85,6 +87,7 @@ public final class JvmRandom extends Random { * @param byteArray ignored * @throws UnsupportedOperationException */ + @Override public void nextBytes(byte[] byteArray) { throw new UnsupportedOperationException(); } @@ -95,6 +98,7 @@ public final class JvmRandom extends Random { * * @return the random int */ + @Override public int nextInt() { return nextInt(Integer.MAX_VALUE); } @@ -107,6 +111,7 @@ public final class JvmRandom extends Random { * @return the random int * @throws IllegalArgumentException when n <= 0 */ + @Override public int nextInt(int n) { return SHARED_RANDOM.nextInt(n); } @@ -117,6 +122,7 @@ public final class JvmRandom extends Random { * * @return the random long */ + @Override public long nextLong() { return nextLong(Long.MAX_VALUE); } @@ -158,6 +164,7 @@ public final class JvmRandom extends Random { * * @return the random boolean */ + @Override public boolean nextBoolean() { return SHARED_RANDOM.nextBoolean(); } @@ -168,6 +175,7 @@ public final class JvmRandom extends Random { * * @return the random float */ + @Override public float nextFloat() { return SHARED_RANDOM.nextFloat(); } @@ -177,6 +185,7 @@ public final class JvmRandom extends Random { * * @return the random double */ + @Override public double nextDouble() { return SHARED_RANDOM.nextDouble(); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/ThreadLocalRandom.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/ThreadLocalRandom.java index aec6d42a0..e141b6fff 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/ThreadLocalRandom.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/ThreadLocalRandom.java @@ -146,6 +146,7 @@ public class ThreadLocalRandom extends Random { * The actual ThreadLocal */ private static final ThreadLocal localRandom = new ThreadLocal() { + @Override protected ThreadLocalRandom initialValue() { return new ThreadLocalRandom(); } @@ -165,6 +166,7 @@ public class ThreadLocalRandom extends Random { * * @throws UnsupportedOperationException always */ + @Override public void setSeed(long seed) { if (initialized) { throw new UnsupportedOperationException(); @@ -172,6 +174,7 @@ public class ThreadLocalRandom extends Random { rnd = (seed ^ multiplier) & mask; } + @Override protected int next(int bits) { rnd = (rnd * multiplier + addend) & mask; return (int)(rnd >>> (48 - bits)); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/manager/TaskManager.java b/config/src/main/java/com/alibaba/nacos/config/server/manager/TaskManager.java index d1311c02b..5bd05d58c 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/manager/TaskManager.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/manager/TaskManager.java @@ -55,6 +55,7 @@ public final class TaskManager implements TaskManagerMBean { class ProcessRunnable implements Runnable { + @Override public void run() { while (!TaskManager.this.closed.get()) { try { @@ -248,6 +249,7 @@ public final class TaskManager implements TaskManagerMBean { } } + @Override public String getTaskInfos() { StringBuilder sb = new StringBuilder(); for (String taskType : this.taskProcessors.keySet()) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/model/ConfigInfoBase.java b/config/src/main/java/com/alibaba/nacos/config/server/model/ConfigInfoBase.java index 94cb5ef32..55151910e 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/model/ConfigInfoBase.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/model/ConfigInfoBase.java @@ -94,6 +94,7 @@ public class ConfigInfoBase implements Serializable, Comparable writer.write(this.content); } + @Override public int compareTo(ConfigInfoBase o) { if (o == null) { return 1; diff --git a/config/src/main/java/com/alibaba/nacos/config/server/model/app/GroupKey.java b/config/src/main/java/com/alibaba/nacos/config/server/model/app/GroupKey.java index 287d175a5..f14e505d3 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/model/app/GroupKey.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/model/app/GroupKey.java @@ -54,6 +54,7 @@ public class GroupKey extends GroupKey2 { this.group = group; } + @Override public String toString() { return dataId + "+" + group; } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java b/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java index 90f9cd2cd..ac8fe8bb4 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java @@ -36,6 +36,7 @@ public class DynamicDataSource implements ApplicationContextAware { private ApplicationContext applicationContext; + @Override public void setApplicationContext(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/LongPollingService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/LongPollingService.java index ec8ae8fff..b24c873e0 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/LongPollingService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/LongPollingService.java @@ -371,6 +371,7 @@ public class LongPollingService extends AbstractEventListener { @Override public void run() { asyncTimeoutFuture = scheduler.schedule(new Runnable() { + @Override public void run() { try { getRetainIps().put(ClientLongPolling.this.ip, System.currentTimeMillis()); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java index 7e1429d67..60ea7ce41 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java @@ -94,6 +94,7 @@ public class PersistService { static final class ConfigInfoWrapperRowMapper implements RowMapper { + @Override public ConfigInfoWrapper mapRow(ResultSet rs, int rowNum) throws SQLException { ConfigInfoWrapper info = new ConfigInfoWrapper(); @@ -128,6 +129,7 @@ public class PersistService { static final class ConfigInfoBetaWrapperRowMapper implements RowMapper { + @Override public ConfigInfoBetaWrapper mapRow(ResultSet rs, int rowNum) throws SQLException { ConfigInfoBetaWrapper info = new ConfigInfoBetaWrapper(); @@ -163,6 +165,7 @@ public class PersistService { static final class ConfigInfoTagWrapperRowMapper implements RowMapper { + @Override public ConfigInfoTagWrapper mapRow(ResultSet rs, int rowNum) throws SQLException { ConfigInfoTagWrapper info = new ConfigInfoTagWrapper(); @@ -198,6 +201,7 @@ public class PersistService { static final class ConfigInfoRowMapper implements RowMapper { + @Override public ConfigInfo mapRow(ResultSet rs, int rowNum) throws SQLException { ConfigInfo info = new ConfigInfo(); @@ -227,6 +231,7 @@ public class PersistService { static final class ConfigKeyRowMapper implements RowMapper { + @Override public ConfigKey mapRow(ResultSet rs, int rowNum) throws SQLException { ConfigKey info = new ConfigKey(); @@ -239,6 +244,7 @@ public class PersistService { } static final class ConfigAdvanceInfoRowMapper implements RowMapper { + @Override public ConfigAdvanceInfo mapRow(ResultSet rs, int rowNum) throws SQLException { ConfigAdvanceInfo info = new ConfigAdvanceInfo(); info.setCreateTime(rs.getTimestamp("gmt_modified").getTime()); @@ -255,6 +261,7 @@ public class PersistService { } static final class ConfigAllInfoRowMapper implements RowMapper { + @Override public ConfigAllInfo mapRow(ResultSet rs, int rowNum) throws SQLException { ConfigAllInfo info = new ConfigAllInfo(); info.setDataId(rs.getString("data_id")); @@ -291,6 +298,7 @@ public class PersistService { static final class ConfigInfo4BetaRowMapper implements RowMapper { + @Override public ConfigInfo4Beta mapRow(ResultSet rs, int rowNum) throws SQLException { ConfigInfo4Beta info = new ConfigInfo4Beta(); @@ -320,6 +328,7 @@ public class PersistService { static final class ConfigInfo4TagRowMapper implements RowMapper { + @Override public ConfigInfo4Tag mapRow(ResultSet rs, int rowNum) throws SQLException { ConfigInfo4Tag info = new ConfigInfo4Tag(); @@ -349,6 +358,7 @@ public class PersistService { static final class ConfigInfoBaseRowMapper implements RowMapper { + @Override public ConfigInfoBase mapRow(ResultSet rs, int rowNum) throws SQLException { ConfigInfoBase info = new ConfigInfoBase(); @@ -371,6 +381,7 @@ public class PersistService { static final class ConfigInfoAggrRowMapper implements RowMapper { + @Override public ConfigInfoAggr mapRow(ResultSet rs, int rowNum) throws SQLException { ConfigInfoAggr info = new ConfigInfoAggr(); @@ -385,6 +396,7 @@ public class PersistService { } static final class ConfigInfoChangedRowMapper implements RowMapper { + @Override public ConfigInfoChanged mapRow(ResultSet rs, int rowNum) throws SQLException { ConfigInfoChanged info = new ConfigInfoChanged(); info.setDataId(rs.getString("data_id")); @@ -395,6 +407,7 @@ public class PersistService { } static final class ConfigHistoryRowMapper implements RowMapper { + @Override public ConfigHistoryInfo mapRow(ResultSet rs, int rowNum) throws SQLException { ConfigHistoryInfo configHistoryInfo = new ConfigHistoryInfo(); configHistoryInfo.setId(rs.getLong("nid")); @@ -411,6 +424,7 @@ public class PersistService { } static final class ConfigHistoryDetailRowMapper implements RowMapper { + @Override public ConfigHistoryInfo mapRow(ResultSet rs, int rowNum) throws SQLException { ConfigHistoryInfo configHistoryInfo = new ConfigHistoryInfo(); configHistoryInfo.setId(rs.getLong("nid")); @@ -432,6 +446,7 @@ public class PersistService { ; static final class TenantInfoRowMapper implements RowMapper { + @Override public TenantInfo mapRow(ResultSet rs, int rowNum) throws SQLException { TenantInfo info = new TenantInfo(); info.setTenantId(rs.getString("tenant_id")); @@ -442,6 +457,7 @@ public class PersistService { } static final class UserRowMapper implements RowMapper { + @Override public User mapRow(ResultSet rs, int rowNum) throws SQLException { User user = new User(); user.setUsername(rs.getString("username")); @@ -799,6 +815,7 @@ public class PersistService { try { this.jt.update(sql, new PreparedStatementSetter() { + @Override public void setValues(PreparedStatement ps) throws SQLException { int index = 1; ps.setString(index++, dataId); @@ -822,6 +839,7 @@ public class PersistService { try { this.jt.update(sql, new PreparedStatementSetter() { + @Override public void setValues(PreparedStatement ps) throws SQLException { int index = 1; ps.setString(index++, dataId); @@ -2637,6 +2655,7 @@ public class PersistService { try { jt.update(new PreparedStatementCreator() { + @Override public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setString(1, configInfo.getDataId()); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/notify/AsyncNotifyService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/notify/AsyncNotifyService.java index ee3fe7a45..d48f411d0 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/notify/AsyncNotifyService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/notify/AsyncNotifyService.java @@ -319,10 +319,12 @@ public class AsyncNotifyService extends AbstractEventListener { // this.executor = executor; } + @Override public void setFailCount(int count) { this.failCount = count; } + @Override public int getFailCount() { return failCount; } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/utils/SimpleFlowData.java b/config/src/main/java/com/alibaba/nacos/config/server/utils/SimpleFlowData.java index d795a7876..76827c1d6 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/utils/SimpleFlowData.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/utils/SimpleFlowData.java @@ -35,6 +35,7 @@ public class SimpleFlowData { @SuppressWarnings("PMD.ThreadPoolCreationRule") private ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { + @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setName("nacos flow control thread"); @@ -52,6 +53,7 @@ public class SimpleFlowData { } timer.scheduleAtFixedRate(new Runnable() { + @Override public void run() { rotateSlot(); } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/utils/SimpleIPFlowData.java b/config/src/main/java/com/alibaba/nacos/config/server/utils/SimpleIPFlowData.java index 88e0491f1..d29a73bb6 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/utils/SimpleIPFlowData.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/utils/SimpleIPFlowData.java @@ -38,6 +38,7 @@ public class SimpleIPFlowData { @SuppressWarnings("PMD.ThreadPoolCreationRule") private ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { + @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setName("nacos ip flow control thread"); @@ -49,6 +50,7 @@ public class SimpleIPFlowData { class DefaultIPFlowDataManagerTask implements Runnable { + @Override public void run() { rotateSlot(); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/Service.java b/naming/src/main/java/com/alibaba/nacos/naming/core/Service.java index f8512b4a8..0fcc029f7 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/Service.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/Service.java @@ -427,6 +427,7 @@ public class Service extends com.alibaba.nacos.api.naming.pojo.Service implement recalculateChecksum(); } + @Override public String getChecksum() { if (StringUtils.isEmpty(checksum)) { recalculateChecksum(); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/RsInfo.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/RsInfo.java index d64a0fbd7..95913e6a5 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/RsInfo.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/RsInfo.java @@ -143,6 +143,7 @@ public class RsInfo { this.metadata = metadata; } + @Override public String toString() { return JSON.toJSONString(this); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/OverrideParameterRequestWrapper.java b/naming/src/main/java/com/alibaba/nacos/naming/web/OverrideParameterRequestWrapper.java index 3f419d9dd..65b4a1529 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/OverrideParameterRequestWrapper.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/OverrideParameterRequestWrapper.java @@ -73,6 +73,7 @@ public class OverrideParameterRequestWrapper extends HttpServletRequestWrapper { return params; } + @Override public String[] getParameterValues(String name) { return params.get(name); } From ebeacb0e311c52055491748b8abf29af2198973b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=A6=B9=E5=85=89?= Date: Sat, 15 Jun 2019 00:16:51 +0800 Subject: [PATCH 13/18] Avoid accessing static variables or static methods of this class through object references of a class. It increases the cost of compiler parsing and can be accessed directly by class name. --- .../alibaba/nacos/config/server/service/DynamicDataSource.java | 2 +- .../nacos/config/server/service/LocalDataSourceServiceImpl.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java b/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java index 90f9cd2cd..78f038d22 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java @@ -47,7 +47,7 @@ public class DynamicDataSource implements ApplicationContextAware { public DataSourceService getDataSource() { DataSourceService dataSourceService = null; - if (STANDALONE_MODE && !propertyUtil.isStandaloneUseMysql()) { + if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) { dataSourceService = (DataSourceService)applicationContext.getBean("localDataSourceService"); } else { dataSourceService = (DataSourceService)applicationContext.getBean("basicDataSourceService"); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java index a45f08618..c930a3f1d 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java @@ -90,7 +90,7 @@ public class LocalDataSourceServiceImpl implements DataSourceService { tm.setDataSource(ds); tjt.setTimeout(5000); - if (STANDALONE_MODE && !propertyUtil.isStandaloneUseMysql()) { + if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) { reload(); } } From 170a0d7d0a772ef6b8498b5b31dfeafe80f2fc4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=A6=B9=E5=85=89?= Date: Sat, 15 Jun 2019 12:52:54 +0800 Subject: [PATCH 14/18] better --- .../nacos/config/server/service/DynamicDataSource.java | 4 ---- .../config/server/service/LocalDataSourceServiceImpl.java | 4 ---- 2 files changed, 8 deletions(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java b/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java index 78f038d22..58109402e 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/DynamicDataSource.java @@ -16,7 +16,6 @@ package com.alibaba.nacos.config.server.service; import com.alibaba.nacos.config.server.utils.PropertyUtil; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @@ -31,9 +30,6 @@ import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE; @Component public class DynamicDataSource implements ApplicationContextAware { - @Autowired - private PropertyUtil propertyUtil; - private ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java index c930a3f1d..31d6dca77 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/LocalDataSourceServiceImpl.java @@ -22,7 +22,6 @@ import com.alibaba.nacos.config.server.utils.StringUtils; import org.apache.commons.dbcp.BasicDataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Service; @@ -62,9 +61,6 @@ public class LocalDataSourceServiceImpl implements DataSourceService { private JdbcTemplate jt; private TransactionTemplate tjt; - @Autowired - private PropertyUtil propertyUtil; - @PostConstruct public void init() { BasicDataSource ds = new BasicDataSource(); From 240ee50aa2efd577a9dc691ca01fa5378b2d7537 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Sat, 15 Jun 2019 19:12:54 +0800 Subject: [PATCH 15/18] fix(nacos-client:config): fix issue 1317 --- .../nacos/api/config/ConfigService.java | 12 ++++++ .../client/config/NacosConfigService.java | 14 +++++++ .../nacos/client/config/impl/CacheData.java | 6 ++- .../client/config/impl/ClientWorker.java | 10 +++++ .../nacos/test/config/ConfigAPI_ITCase.java | 38 +++++++++++++++++++ 5 files changed, 79 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java index 3e79f950c..13a8eac25 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java @@ -36,6 +36,18 @@ public interface ConfigService { */ String getConfig(String dataId, String group, long timeoutMs) throws NacosException; + /** + * Get config + * + * @param dataId dataId + * @param group group + * @param timeoutMs read timeout + * @param listener {@link Listener} + * @return config value + * @throws NacosException NacosException + */ + String getConfigAndSignListener(String dataId, String group, long timeoutMs, Listener listener) throws NacosException; + /** * Add a listener to the configuration, after the server modified the * configuration, the client will use the incoming listener callback. diff --git a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java index 9d2be89bd..85d82c37a 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java @@ -27,6 +27,7 @@ import com.alibaba.nacos.client.config.filter.impl.ConfigResponse; import com.alibaba.nacos.client.config.http.HttpAgent; import com.alibaba.nacos.client.config.http.MetricsHttpAgent; import com.alibaba.nacos.client.config.http.ServerHttpAgent; +import com.alibaba.nacos.client.config.impl.CacheData; import com.alibaba.nacos.client.config.impl.ClientWorker; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.config.impl.LocalConfigInfoProcessor; @@ -122,6 +123,13 @@ public class NacosConfigService implements ConfigService { return getConfigInner(namespace, dataId, group, timeoutMs); } + @Override + public String getConfigAndSignListener(String dataId, String group, long timeoutMs, Listener listener) throws NacosException { + String content = getConfig(dataId, group, timeoutMs); + worker.addTenantListenersWithContent(dataId, group, content, Arrays.asList(listener)); + return content; + } + @Override public void addListener(String dataId, String group, Listener listener) throws NacosException { worker.addTenantListeners(dataId, group, Arrays.asList(listener)); @@ -166,6 +174,12 @@ public class NacosConfigService implements ConfigService { content = worker.getServerConfig(dataId, group, tenant, timeoutMs); cr.setContent(content); + + CacheData cacheData = worker.getCache(dataId, group, tenant); + if (cacheData != null) { + cacheData.setContent(content); + } + configFilterChainManager.doFilter(null, cr); content = cr.getContent(); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java index 47c1d262d..67cbb3fd2 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java @@ -66,6 +66,7 @@ public class CacheData { /** * Add listener + * if CacheData already set new content, Listener should init lastCallMd5 by CacheData.md5 * * @param listener listener */ @@ -74,6 +75,7 @@ public class CacheData { throw new IllegalArgumentException("listener is null"); } ManagerListenerWrap wrap = new ManagerListenerWrap(listener); + wrap.lastCallMd5 = md5; if (listeners.addIfAbsent(wrap)) { LOGGER.info("[{}] [add-listener] ok, tenant={}, dataId={}, group={}, cnt={}", name, tenant, dataId, group, listeners.size()); @@ -167,6 +169,8 @@ public class CacheData { final Listener listener = listenerWrap.listener; Runnable job = new Runnable() { + + @Override public void run() { ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader appClassLoader = listener.getClass().getClassLoader(); @@ -284,7 +288,7 @@ public class CacheData { class ManagerListenerWrap { final Listener listener; - String lastCallMd5 = CacheData.getMd5String(null); + volatile String lastCallMd5 = CacheData.getMd5String(null); ManagerListenerWrap(Listener listener) { this.listener = listener; diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index a7665f051..93022337d 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -91,6 +91,16 @@ public class ClientWorker { } } + public void addTenantListenersWithContent(String dataId, String group, String content, List listeners) { + group = null2defaultGroup(group); + String tenant = agent.getTenant(); + CacheData cache = addCacheDataIfAbsent(dataId, group, tenant); + cache.setContent(content); + for (Listener listener : listeners) { + cache.addListener(listener); + } + } + public void removeTenantListener(String dataId, String group, Listener listener) { group = null2defaultGroup(group); String tenant = agent.getTenant(); diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java index 103fce8dd..5fc386106 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java @@ -435,6 +435,44 @@ public class ConfigAPI_ITCase { iconfig.removeListener(dataId, group, ml); } + /** + * @TCDescription : nacos_在主动拉取配置后并注册Listener,在更新配置后才触发Listener监听事件 + * @TestStep : TODO Test steps + * @ExpectResult : TODO expect results + * @author xiaochun.xxc + * @since 3.6.8 + */ + @Test + public void nacos_addListener_5() throws InterruptedException, NacosException { + final AtomicInteger count = new AtomicInteger(0); + final String content = "test-abc"; + final String newContent = "new-test-def"; + boolean result = iconfig.publishConfig(dataId, group, content); + Assert.assertTrue(result); + + Thread.sleep(3000); + + Listener ml = new AbstractListener() { + @Override + public void receiveConfigInfo(String configInfo) { + count.incrementAndGet(); + System.out.println("Listener receive : [" + configInfo + "]"); + Assert.assertEquals(content, newContent); + } + }; + String receiveContent = iconfig.getConfigAndSignListener(dataId, group, 1000, ml); + System.out.println(receiveContent); + + result = iconfig.publishConfig(dataId, group, newContent); + Assert.assertTrue(result); + + Assert.assertEquals(content, receiveContent); + Thread.sleep(3000); + + Assert.assertEquals(1, count.get()); + iconfig.removeListener(dataId, group, ml); + } + /** * @TCDescription : nacos_正常移除监听器 * @TestStep : TODO Test steps From ade510f31dfa989c047a6616d400a48e4fa0ed51 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Sat, 15 Jun 2019 23:37:12 +0800 Subject: [PATCH 16/18] feat(nacos-client:config): fix issue #1317 --- .../alibaba/nacos/api/PropertyKeyConst.java | 2 + .../nacos/api/config/ConfigService.java | 2 +- .../client/config/NacosConfigService.java | 5 -- .../nacos/client/config/impl/CacheData.java | 25 +++++++- .../client/config/impl/ClientWorker.java | 16 ++++- .../nacos/test/config/ConfigAPI_ITCase.java | 58 ++++++++++++++++++- .../test/config/ConfigLongPoll_ITCase.java | 2 +- 7 files changed, 97 insertions(+), 13 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java b/api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java index 984591bdf..de42befe5 100644 --- a/api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java +++ b/api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java @@ -52,6 +52,8 @@ public class PropertyKeyConst { public final static String MAX_RETRY = "maxRetry"; + public final static String ENABLE_REMOTE_SYNC_CONFIG = "enableRemoteSyncConfig"; + public final static String NAMING_LOAD_CACHE_AT_START = "namingLoadCacheAtStart"; public final static String NAMING_CLIENT_BEAT_THREAD_COUNT = "namingClientBeatThreadCount"; diff --git a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java index 13a8eac25..7da577f1d 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java @@ -37,7 +37,7 @@ public interface ConfigService { String getConfig(String dataId, String group, long timeoutMs) throws NacosException; /** - * Get config + * Get config and register Listener * * @param dataId dataId * @param group group diff --git a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java index 85d82c37a..a6ce0c79c 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java @@ -175,11 +175,6 @@ public class NacosConfigService implements ConfigService { cr.setContent(content); - CacheData cacheData = worker.getCache(dataId, group, tenant); - if (cacheData != null) { - cacheData.setContent(content); - } - configFilterChainManager.doFilter(null, cr); content = cr.getContent(); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java index 67cbb3fd2..aa05cac49 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java @@ -23,6 +23,7 @@ import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager; import com.alibaba.nacos.client.config.filter.impl.ConfigResponse; import com.alibaba.nacos.client.config.utils.MD5; import com.alibaba.nacos.client.utils.LogUtils; +import com.alibaba.nacos.client.utils.StringUtils; import com.alibaba.nacos.client.utils.TenantUtil; import org.slf4j.Logger; @@ -74,8 +75,7 @@ public class CacheData { if (null == listener) { throw new IllegalArgumentException("listener is null"); } - ManagerListenerWrap wrap = new ManagerListenerWrap(listener); - wrap.lastCallMd5 = md5; + ManagerListenerWrap wrap = new ManagerListenerWrap(listener, md5); if (listeners.addIfAbsent(wrap)) { LOGGER.info("[{}] [add-listener] ok, tenant={}, dataId={}, group={}, cnt={}", name, tenant, dataId, group, listeners.size()); @@ -263,6 +263,22 @@ public class CacheData { this.md5 = getMd5String(content); } + public CacheData(ConfigFilterChainManager configFilterChainManager, String name, String dataId, String group, + String tenant, String content) { + if (null == dataId || null == group) { + throw new IllegalArgumentException("dataId=" + dataId + ", group=" + group); + } + this.name = name; + this.configFilterChainManager = configFilterChainManager; + this.dataId = dataId; + this.group = group; + this.tenant = tenant; + listeners = new CopyOnWriteArrayList(); + this.isInitializing = true; + this.content = StringUtils.isEmpty(content) ? loadCacheContentFromDiskLocal(name, dataId, group, tenant) : content; + this.md5 = getMd5String(content); + } + // ================== private final String name; @@ -294,6 +310,11 @@ class ManagerListenerWrap { this.listener = listener; } + ManagerListenerWrap(Listener listener, String md5) { + this.listener = listener; + this.lastCallMd5 = md5; + } + @Override public boolean equals(Object obj) { if (null == obj || obj.getClass() != getClass()) { diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index 93022337d..221b0d2ce 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -177,7 +177,6 @@ public class ClientWorker { return cache; } String key = GroupKey.getKeyTenant(dataId, group, tenant); - cache = new CacheData(configFilterChainManager, agent.getName(), dataId, group, tenant); synchronized (cacheMap) { CacheData cacheFromMap = getCache(dataId, group, tenant); // multiple listeners on the same dataid+group and race condition,so @@ -187,6 +186,18 @@ public class ClientWorker { cache = cacheFromMap; // reset so that server not hang this check cache.setInitializing(true); + } else { + cache = new CacheData(configFilterChainManager, agent.getName(), dataId, group, tenant); + String content; + // fix issue # 1317 + if (enableRemoteSyncConfig) { + try { + content = getServerConfig(dataId, group, tenant, 3000L); + } catch (NacosException ignore) { + content = null; + } + cache.setContent(content); + } } Map copy = new HashMap(cacheMap.get()); @@ -471,6 +482,8 @@ public class ClientWorker { Constants.CONFIG_LONG_POLL_TIMEOUT), Constants.MIN_CONFIG_LONG_POLL_TIMEOUT); taskPenaltyTime = NumberUtils.toInt(String.valueOf(properties.get(PropertyKeyConst.CONFIG_RETRY_TIME)), Constants.CONFIG_RETRY_TIME); + + enableRemoteSyncConfig = Boolean.parseBoolean(properties.getProperty(PropertyKeyConst.ENABLE_REMOTE_SYNC_CONFIG)); } class LongPollingRunnable implements Runnable { @@ -569,4 +582,5 @@ public class ClientWorker { private long timeout; private double currentLongingTaskCount = 0; private int taskPenaltyTime; + private boolean enableRemoteSyncConfig = false; } diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java index 5fc386106..837a6cce9 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java @@ -436,7 +436,7 @@ public class ConfigAPI_ITCase { } /** - * @TCDescription : nacos_在主动拉取配置后并注册Listener,在更新配置后才触发Listener监听事件 + * @TCDescription : nacos_在主动拉取配置后并注册Listener,在更新配置后才触发Listener监听事件(使用特定接口) * @TestStep : TODO Test steps * @ExpectResult : TODO expect results * @author xiaochun.xxc @@ -450,7 +450,7 @@ public class ConfigAPI_ITCase { boolean result = iconfig.publishConfig(dataId, group, content); Assert.assertTrue(result); - Thread.sleep(3000); + Thread.sleep(2000); Listener ml = new AbstractListener() { @Override @@ -460,6 +460,7 @@ public class ConfigAPI_ITCase { Assert.assertEquals(content, newContent); } }; + String receiveContent = iconfig.getConfigAndSignListener(dataId, group, 1000, ml); System.out.println(receiveContent); @@ -467,7 +468,58 @@ public class ConfigAPI_ITCase { Assert.assertTrue(result); Assert.assertEquals(content, receiveContent); - Thread.sleep(3000); + Thread.sleep(2000); + + Assert.assertEquals(1, count.get()); + iconfig.removeListener(dataId, group, ml); + } + + /** + * @TCDescription : nacos_在主动拉取配置后并注册Listener,在更新配置后才触发Listener监听事件(进行配置参数设置) + * @TestStep : TODO Test steps + * @ExpectResult : TODO expect results + * @author xiaochun.xxc + * @since 3.6.8 + */ + @Test + public void nacos_addListener_6() throws InterruptedException, NacosException { + + Properties properties = new Properties(); + properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1"+":"+port); + properties.put(PropertyKeyConst.ENABLE_REMOTE_SYNC_CONFIG, "true"); + ConfigService iconfig = NacosFactory.createConfigService(properties); + + final AtomicInteger count = new AtomicInteger(0); + final String content = "test-abc"; + final String newContent = "new-test-def"; + boolean result = iconfig.publishConfig(dataId, group, content); + Assert.assertTrue(result); + + Thread.sleep(2000); + + Listener ml = new AbstractListener() { + @Override + public void receiveConfigInfo(String configInfo) { + count.incrementAndGet(); + System.out.println("Listener receive : [" + configInfo + "]"); + Assert.assertEquals(content, newContent); + } + }; + + iconfig.addListener(dataId, group, ml); + + String receiveContent = iconfig.getConfig(dataId, group, 1000); + + System.out.println(receiveContent); + + result = iconfig.publishConfig(dataId, group, newContent); + Assert.assertTrue(result); + + Thread.sleep(2000); + + receiveContent = iconfig.getConfig(dataId, group, 1000); + + Assert.assertEquals(newContent, receiveContent); Assert.assertEquals(1, count.get()); iconfig.removeListener(dataId, group, ml); diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java index c50fd92da..fb45d8830 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java @@ -82,7 +82,7 @@ public class ConfigLongPoll_ITCase { } }); - TimeUnit.SECONDS.sleep(30); + TimeUnit.SECONDS.sleep(10); } From 21ff07563d88a12eb9032aa1db3ff59d0e2c1d90 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Sun, 16 Jun 2019 08:24:17 +0800 Subject: [PATCH 17/18] refactor(nacos-client:config): rmove un use construct of CacheData --- .../nacos/api/config/ConfigService.java | 5 +++++ .../nacos/client/config/impl/CacheData.java | 19 +------------------ .../client/config/impl/ClientWorker.java | 7 +++---- 3 files changed, 9 insertions(+), 22 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java index 7da577f1d..87234f160 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java @@ -39,6 +39,11 @@ public interface ConfigService { /** * Get config and register Listener * + * If you want to pull it yourself when the program starts to get the configuration for the first time, + * and the registered Listener is used for future configuration updates, you can keep the original + * code unchanged, just add the system parameter: enableRemoteSyncConfig = "true" ( But there is network overhead); + * therefore we recommend that you use this interface directly + * * @param dataId dataId * @param group group * @param timeoutMs read timeout diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java index aa05cac49..6e3f289da 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java @@ -23,7 +23,6 @@ import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager; import com.alibaba.nacos.client.config.filter.impl.ConfigResponse; import com.alibaba.nacos.client.config.utils.MD5; import com.alibaba.nacos.client.utils.LogUtils; -import com.alibaba.nacos.client.utils.StringUtils; import com.alibaba.nacos.client.utils.TenantUtil; import org.slf4j.Logger; @@ -263,22 +262,6 @@ public class CacheData { this.md5 = getMd5String(content); } - public CacheData(ConfigFilterChainManager configFilterChainManager, String name, String dataId, String group, - String tenant, String content) { - if (null == dataId || null == group) { - throw new IllegalArgumentException("dataId=" + dataId + ", group=" + group); - } - this.name = name; - this.configFilterChainManager = configFilterChainManager; - this.dataId = dataId; - this.group = group; - this.tenant = tenant; - listeners = new CopyOnWriteArrayList(); - this.isInitializing = true; - this.content = StringUtils.isEmpty(content) ? loadCacheContentFromDiskLocal(name, dataId, group, tenant) : content; - this.md5 = getMd5String(content); - } - // ================== private final String name; @@ -304,7 +287,7 @@ public class CacheData { class ManagerListenerWrap { final Listener listener; - volatile String lastCallMd5 = CacheData.getMd5String(null); + String lastCallMd5 = CacheData.getMd5String(null); ManagerListenerWrap(Listener listener) { this.listener = listener; diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index 221b0d2ce..97e6afe3b 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -188,15 +188,14 @@ public class ClientWorker { cache.setInitializing(true); } else { cache = new CacheData(configFilterChainManager, agent.getName(), dataId, group, tenant); - String content; // fix issue # 1317 if (enableRemoteSyncConfig) { try { - content = getServerConfig(dataId, group, tenant, 3000L); + String content = getServerConfig(dataId, group, tenant, 3000L); + cache.setContent(content); } catch (NacosException ignore) { - content = null; + // If the remote pull fails, the local snapshot should not be overwritten } - cache.setContent(content); } } From 2a67382a7aa585eb04b7995303d274714a2915d5 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Mon, 17 Jun 2019 21:11:25 +0800 Subject: [PATCH 18/18] refactor(nacos-client:config): Modify exception handling --- .../nacos/client/config/impl/ClientWorker.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index 97e6afe3b..79c01dd5f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -82,7 +82,7 @@ public class ClientWorker { } } - public void addTenantListeners(String dataId, String group, List listeners) { + public void addTenantListeners(String dataId, String group, List listeners) throws NacosException { group = null2defaultGroup(group); String tenant = agent.getTenant(); CacheData cache = addCacheDataIfAbsent(dataId, group, tenant); @@ -91,7 +91,7 @@ public class ClientWorker { } } - public void addTenantListenersWithContent(String dataId, String group, String content, List listeners) { + public void addTenantListenersWithContent(String dataId, String group, String content, List listeners) throws NacosException { group = null2defaultGroup(group); String tenant = agent.getTenant(); CacheData cache = addCacheDataIfAbsent(dataId, group, tenant); @@ -171,7 +171,7 @@ public class ClientWorker { return cache; } - public CacheData addCacheDataIfAbsent(String dataId, String group, String tenant) { + public CacheData addCacheDataIfAbsent(String dataId, String group, String tenant) throws NacosException { CacheData cache = getCache(dataId, group, tenant); if (null != cache) { return cache; @@ -190,12 +190,8 @@ public class ClientWorker { cache = new CacheData(configFilterChainManager, agent.getName(), dataId, group, tenant); // fix issue # 1317 if (enableRemoteSyncConfig) { - try { - String content = getServerConfig(dataId, group, tenant, 3000L); - cache.setContent(content); - } catch (NacosException ignore) { - // If the remote pull fails, the local snapshot should not be overwritten - } + String content = getServerConfig(dataId, group, tenant, 3000L); + cache.setContent(content); } }