From 92bbf9ef229b669099eee3625487f3d0db85115a Mon Sep 17 00:00:00 2001 From: nkorange Date: Thu, 31 Oct 2019 20:51:06 +0800 Subject: [PATCH 1/2] fix #1947 --- .../naming/controllers/InstanceController.java | 3 ++- .../alibaba/nacos/naming/push/PushService.java | 17 +++++++---------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java index f46681643..a652b6fe0 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java @@ -384,6 +384,7 @@ public class InstanceController { // now try to enable the push try { if (udpPort > 0 && pushService.canEnablePush(agent)) { + pushService.addClient(namespaceId, serviceName, clusters, agent, @@ -394,7 +395,7 @@ public class InstanceController { cacheMillis = switchDomain.getPushCacheMillis(serviceName); } } catch (Exception e) { - Loggers.SRV_LOG.error("[NACOS-API] failed to added push client", e); + Loggers.SRV_LOG.error("[NACOS-API] failed to added push client {}, {}:{}", clientInfo, clientIP, udpPort, e); cacheMillis = switchDomain.getDefaultCacheMillis(); } 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 e2923afab..2d04c0ce7 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 @@ -225,7 +225,7 @@ public class PushService implements ApplicationContextAware, ApplicationListener addClient(client); } - public static void addClient(PushClient client) { + public void addClient(PushClient client) { // client is stored by key 'serviceName' because notify event is driven by serviceName change String serviceKey = UtilsAndCommons.assembleFullServiceName(client.getNamespaceId(), client.getServiceName()); ConcurrentMap clients = @@ -403,21 +403,18 @@ public class PushService implements ApplicationContextAware, ApplicationListener return dataSource; } - public PushClient(InetSocketAddress socketAddr) { - this.socketAddr = socketAddr; - } - public boolean zombie() { return System.currentTimeMillis() - lastRefTime > switchDomain.getPushCacheMillis(serviceName); } @Override public String toString() { - return "serviceName: " + serviceName - + ", clusters: " + clusters - + ", ip: " + socketAddr.getAddress().getHostAddress() - + ", port: " + socketAddr.getPort() - + ", agent: " + agent; + StringBuilder sb = new StringBuilder(); + sb.append("serviceName: ").append(serviceName) + .append(", clusters: ").append(clusters) + .append(", address: ").append(socketAddr) + .append(", agent: ").append(agent); + return sb.toString(); } public String getAgent() { From f57a3d8781f87d2ee10bcafd44115eb78db10ee5 Mon Sep 17 00:00:00 2001 From: nkorange Date: Mon, 4 Nov 2019 10:30:08 +0800 Subject: [PATCH 2/2] #1947 add test cases. --- .../nacos/naming/push/PushClientTest.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 naming/src/test/java/com/alibaba/nacos/naming/push/PushClientTest.java diff --git a/naming/src/test/java/com/alibaba/nacos/naming/push/PushClientTest.java b/naming/src/test/java/com/alibaba/nacos/naming/push/PushClientTest.java new file mode 100644 index 000000000..37df8e6c4 --- /dev/null +++ b/naming/src/test/java/com/alibaba/nacos/naming/push/PushClientTest.java @@ -0,0 +1,50 @@ +/* + * 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.push; + + +import com.alibaba.nacos.naming.BaseTest; +import org.junit.Test; + +import java.net.InetSocketAddress; + +/** + * @author nkorange + */ +public class PushClientTest extends BaseTest { + + @Test + public void testAddClient() { + + String namespaceId = "public"; + String serviceName = "test.1"; + String clusters = "DEFAULT"; + String agent = "Nacos-Java-Client:v1.1.4"; + String clientIp = "xxxxx"; + String app = "nacos"; + int udpPort = 10000; + + + pushService.addClient(namespaceId + , serviceName + , clusters + , agent + , new InetSocketAddress(clientIp, udpPort) + , null + , namespaceId + , app); + } +}