From 16794d15e55fc9003959dbf7f269748038535088 Mon Sep 17 00:00:00 2001 From: nkorange Date: Tue, 25 Dec 2018 20:59:08 +0800 Subject: [PATCH] #523 add switch --- .../naming/controllers/ServiceController.java | 3 ++- .../com/alibaba/nacos/naming/misc/Switch.java | 20 +++++++++++----- .../nacos/naming/misc/SwitchDomain.java | 3 +++ .../nacos/naming/misc/SwitchEntry.java | 1 + .../alibaba/nacos/naming/web/ApiCommands.java | 24 ++++++++++++++----- .../nacos/test/naming/MultipleTenantTest.java | 10 +++++--- .../test/naming/RegisterInstance_ITCase.java | 20 ++++++++-------- 7 files changed, 55 insertions(+), 26 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 e77a85d5e..3f2db7260 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 @@ -25,6 +25,7 @@ import com.alibaba.nacos.naming.core.IpAddress; import com.alibaba.nacos.naming.core.VirtualClusterDomain; import com.alibaba.nacos.naming.exception.NacosException; import com.alibaba.nacos.naming.healthcheck.HealthCheckMode; +import com.alibaba.nacos.naming.misc.Switch; import com.alibaba.nacos.naming.misc.UtilsAndCommons; import com.alibaba.nacos.naming.selector.LabelSelector; import com.alibaba.nacos.naming.selector.NoneSelector; @@ -62,7 +63,7 @@ public class ServiceController { } float protectThreshold = NumberUtils.toFloat(WebUtils.optional(request, "protectThreshold", "0")); - String healthCheckMode = WebUtils.optional(request, "healthCheckMode", "client"); + String healthCheckMode = WebUtils.optional(request, "healthCheckMode", Switch.getDefaultHealthCheckMode()); String metadata = WebUtils.optional(request, "metadata", StringUtils.EMPTY); String selector = WebUtils.optional(request, "selector", StringUtils.EMPTY); Map metadataMap = new HashMap<>(16); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/Switch.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/Switch.java index 8386f7afe..fa5ac4993 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/Switch.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/Switch.java @@ -62,10 +62,8 @@ public class Switch { if (StringUtils.isEmpty(value)) { return; } - SwitchDomain switchDomain = JSON.parseObject(value, new TypeReference() { + dom = JSON.parseObject(value, new TypeReference() { }); - - dom = switchDomain; } @Override @@ -77,7 +75,7 @@ public class Switch { public static long getPushCacheMillis(String dom) { if (Switch.dom.pushCacheMillisMap == null - || !Switch.dom.pushCacheMillisMap.containsKey(dom)) { + || !Switch.dom.pushCacheMillisMap.containsKey(dom)) { return Switch.dom.defaultPushCacheMillis; } @@ -90,7 +88,7 @@ public class Switch { public static long getCacheMillis(String dom) { if (Switch.dom.cacheMillisMap == null - || !Switch.dom.cacheMillisMap.containsKey(dom)) { + || !Switch.dom.cacheMillisMap.containsKey(dom)) { return Switch.dom.defaultCacheMillis; } @@ -165,6 +163,14 @@ public class Switch { Switch.dom.healthCheckEnabled = enabled; } + public static String getDefaultHealthCheckMode() { + return Switch.dom.defaultHealthCheckMode; + } + + public static void setDefaultHealthCheckMode(String healthCheckMode) { + Switch.dom.defaultHealthCheckMode = healthCheckMode; + } + public static boolean isEnableAuthentication() { return dom.isEnableAuthentication(); } @@ -191,7 +197,7 @@ public class Switch { public static Integer getAdWeight(String ip) { if (dom.adWeightMap == null - || !dom.adWeightMap.containsKey(ip)) { + || !dom.adWeightMap.containsKey(ip)) { return 0; } @@ -205,6 +211,7 @@ public class Switch { public static String getPushJavaVersion() { return dom.pushJavaVersion; } + public static String getPushGoVersion() { return dom.pushGoVersion; } @@ -220,6 +227,7 @@ public class Switch { public static void setPushJavaVersion(String pushJavaVersion) { dom.pushJavaVersion = pushJavaVersion; } + public static void setPushGoVersion(String pushGoVersion) { dom.pushGoVersion = pushGoVersion; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchDomain.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchDomain.java index 8be8038c5..39f97721e 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchDomain.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchDomain.java @@ -19,6 +19,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.nacos.naming.core.Domain; import com.alibaba.nacos.naming.core.IpAddress; +import com.alibaba.nacos.naming.healthcheck.HealthCheckMode; import com.alibaba.nacos.naming.raft.RaftListener; import org.apache.commons.lang3.StringUtils; import sun.reflect.generics.reflectiveObjects.NotImplementedException; @@ -52,6 +53,8 @@ public class SwitchDomain implements Domain, RaftListener { public boolean healthCheckEnabled = true; + public String defaultHealthCheckMode = HealthCheckMode.client.name(); + public boolean distroEnabled = true; public boolean enableStandalone = true; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchEntry.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchEntry.java index eaee45c95..91afde295 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchEntry.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchEntry.java @@ -38,6 +38,7 @@ public class SwitchEntry { public static final String MASTERS = "masters"; public static final String DISTRO = "distro"; public static final String CHECK = "check"; + public static final String DEFAULT_HEALTH_CHECK_MODE = "defaultHealthCheckMode"; public static final String DOM_STATUS_SYNC_PERIOD = "domStatusSynchronizationPeriodMillis"; public static final String SERVER_STATUS_SYNC_PERIOD = "serverStatusSynchronizationPeriodMillis"; public static final String HEALTH_CHECK_TIMES = "healthCheckTimes"; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java b/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java index cb30867ab..042653c08 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java @@ -27,10 +27,7 @@ import com.alibaba.nacos.core.utils.WebUtils; import com.alibaba.nacos.naming.boot.RunningConfig; import com.alibaba.nacos.naming.core.*; import com.alibaba.nacos.naming.exception.NacosException; -import com.alibaba.nacos.naming.healthcheck.AbstractHealthCheckProcessor; -import com.alibaba.nacos.naming.healthcheck.HealthCheckTask; -import com.alibaba.nacos.naming.healthcheck.HealthCheckType; -import com.alibaba.nacos.naming.healthcheck.RsInfo; +import com.alibaba.nacos.naming.healthcheck.*; import com.alibaba.nacos.naming.misc.*; import com.alibaba.nacos.naming.push.ClientInfo; import com.alibaba.nacos.naming.push.DataSource; @@ -386,11 +383,16 @@ public class ApiCommands { boolean isUseSpecifiedURL = Boolean.parseBoolean(WebUtils.optional(request, "isUseSpecifiedURL", "false")); String envAndSite = WebUtils.optional(request, "envAndSites", StringUtils.EMPTY); boolean resetWeight = Boolean.parseBoolean(WebUtils.optional(request, "resetWeight", "false")); - boolean enableHealthCheck = Boolean.parseBoolean(WebUtils.optional(request, "enableHealthCheck", "true")); + + boolean enableHealthCheck = Boolean.parseBoolean(WebUtils.optional(request, "enableHealthCheck", + String.valueOf(Switch.getDefaultHealthCheckMode().equals(HealthCheckMode.server.name())))); + boolean enable = Boolean.parseBoolean(WebUtils.optional(request, "serviceEnabled", "true")); String disabledSites = WebUtils.optional(request, "disabledSites", StringUtils.EMPTY); - boolean eanbleClientBeat = Boolean.parseBoolean(WebUtils.optional(request, "enableClientBeat", "true")); + boolean eanbleClientBeat = Boolean.parseBoolean(WebUtils.optional(request, "enableClientBeat", + String.valueOf(Switch.getDefaultHealthCheckMode().equals(HealthCheckMode.client.name())))); + String clusterName = WebUtils.optional(request, "clusterName", UtilsAndCommons.DEFAULT_CLUSTER_NAME); String serviceMetadataJson = WebUtils.optional(request, "serviceMetadata", StringUtils.EMPTY); @@ -1536,6 +1538,16 @@ public class ApiCommands { return "ok"; } + if (entry.equals(SwitchEntry.DEFAULT_HEALTH_CHECK_MODE)) { + String defaultHealthCheckMode = WebUtils.required(request, "mode"); + + Switch.setDefaultHealthCheckMode(defaultHealthCheckMode); + if (!debug) { + Switch.save(); + } + return "ok"; + } + if (entry.equals(SwitchEntry.DOM_STATUS_SYNC_PERIOD)) { Long millis = Long.parseLong(WebUtils.required(request, "millis")); diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/MultipleTenantTest.java b/test/src/test/java/com/alibaba/nacos/test/naming/MultipleTenantTest.java index ed3cb38ce..4f91ec561 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/MultipleTenantTest.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/MultipleTenantTest.java @@ -24,9 +24,9 @@ import static com.alibaba.nacos.test.naming.NamingBase.*; /** * @author nkorange */ -@RunWith(SpringRunner.class) -@SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +//@RunWith(SpringRunner.class) +//@SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"}, +// webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class MultipleTenantTest { private NamingService naming; @@ -42,6 +42,8 @@ public class MultipleTenantTest { TimeUnit.SECONDS.sleep(10); + port = 8848; + naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); Properties properties = new Properties(); @@ -86,6 +88,8 @@ public class MultipleTenantTest { instances = naming.getAllInstances(serviceName); Assert.assertEquals(2, instances.size()); + + TimeUnit.SECONDS.sleep(10000000L); } @Test diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java index 201245181..512435953 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java @@ -42,9 +42,9 @@ import static com.alibaba.nacos.test.naming.NamingBase.*; * @author wangtong.wt * @date 2018/6/20 */ -@RunWith(SpringRunner.class) -@SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +//@RunWith(SpringRunner.class) +//@SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"}, +// webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class RegisterInstance_ITCase { private NamingService naming; @@ -54,23 +54,23 @@ public class RegisterInstance_ITCase { @Before public void init() throws Exception { - if (naming == null) { - //TimeUnit.SECONDS.sleep(10); - naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); - } +// if (naming == null) { +// TimeUnit.SECONDS.sleep(10); +// naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); +// } } @Test - @Ignore +// @Ignore public void regService() throws NacosException, InterruptedException { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848"); properties.put(PropertyKeyConst.NAMESPACE, "t3"); - naming = NacosFactory.createNamingService(properties); + naming = NamingFactory.createNamingService(properties); - String serviceName = "dungu.test.8"; + String serviceName = "dungu.test.10"; naming.registerInstance(serviceName, "127.0.0.1", 80, "c1"); naming.registerInstance(serviceName, "127.0.0.2", 80, "c2"); Thread.sleep(100000000L);