#523 add switch
This commit is contained in:
parent
478cb1533e
commit
16794d15e5
@ -25,6 +25,7 @@ import com.alibaba.nacos.naming.core.IpAddress;
|
|||||||
import com.alibaba.nacos.naming.core.VirtualClusterDomain;
|
import com.alibaba.nacos.naming.core.VirtualClusterDomain;
|
||||||
import com.alibaba.nacos.naming.exception.NacosException;
|
import com.alibaba.nacos.naming.exception.NacosException;
|
||||||
import com.alibaba.nacos.naming.healthcheck.HealthCheckMode;
|
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.misc.UtilsAndCommons;
|
||||||
import com.alibaba.nacos.naming.selector.LabelSelector;
|
import com.alibaba.nacos.naming.selector.LabelSelector;
|
||||||
import com.alibaba.nacos.naming.selector.NoneSelector;
|
import com.alibaba.nacos.naming.selector.NoneSelector;
|
||||||
@ -62,7 +63,7 @@ public class ServiceController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
float protectThreshold = NumberUtils.toFloat(WebUtils.optional(request, "protectThreshold", "0"));
|
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 metadata = WebUtils.optional(request, "metadata", StringUtils.EMPTY);
|
||||||
String selector = WebUtils.optional(request, "selector", StringUtils.EMPTY);
|
String selector = WebUtils.optional(request, "selector", StringUtils.EMPTY);
|
||||||
Map<String, String> metadataMap = new HashMap<>(16);
|
Map<String, String> metadataMap = new HashMap<>(16);
|
||||||
|
@ -62,10 +62,8 @@ public class Switch {
|
|||||||
if (StringUtils.isEmpty(value)) {
|
if (StringUtils.isEmpty(value)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SwitchDomain switchDomain = JSON.parseObject(value, new TypeReference<SwitchDomain>() {
|
dom = JSON.parseObject(value, new TypeReference<SwitchDomain>() {
|
||||||
});
|
});
|
||||||
|
|
||||||
dom = switchDomain;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -77,7 +75,7 @@ public class Switch {
|
|||||||
|
|
||||||
public static long getPushCacheMillis(String dom) {
|
public static long getPushCacheMillis(String dom) {
|
||||||
if (Switch.dom.pushCacheMillisMap == null
|
if (Switch.dom.pushCacheMillisMap == null
|
||||||
|| !Switch.dom.pushCacheMillisMap.containsKey(dom)) {
|
|| !Switch.dom.pushCacheMillisMap.containsKey(dom)) {
|
||||||
return Switch.dom.defaultPushCacheMillis;
|
return Switch.dom.defaultPushCacheMillis;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +88,7 @@ public class Switch {
|
|||||||
|
|
||||||
public static long getCacheMillis(String dom) {
|
public static long getCacheMillis(String dom) {
|
||||||
if (Switch.dom.cacheMillisMap == null
|
if (Switch.dom.cacheMillisMap == null
|
||||||
|| !Switch.dom.cacheMillisMap.containsKey(dom)) {
|
|| !Switch.dom.cacheMillisMap.containsKey(dom)) {
|
||||||
return Switch.dom.defaultCacheMillis;
|
return Switch.dom.defaultCacheMillis;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,6 +163,14 @@ public class Switch {
|
|||||||
Switch.dom.healthCheckEnabled = enabled;
|
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() {
|
public static boolean isEnableAuthentication() {
|
||||||
return dom.isEnableAuthentication();
|
return dom.isEnableAuthentication();
|
||||||
}
|
}
|
||||||
@ -191,7 +197,7 @@ public class Switch {
|
|||||||
|
|
||||||
public static Integer getAdWeight(String ip) {
|
public static Integer getAdWeight(String ip) {
|
||||||
if (dom.adWeightMap == null
|
if (dom.adWeightMap == null
|
||||||
|| !dom.adWeightMap.containsKey(ip)) {
|
|| !dom.adWeightMap.containsKey(ip)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,6 +211,7 @@ public class Switch {
|
|||||||
public static String getPushJavaVersion() {
|
public static String getPushJavaVersion() {
|
||||||
return dom.pushJavaVersion;
|
return dom.pushJavaVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getPushGoVersion() {
|
public static String getPushGoVersion() {
|
||||||
return dom.pushGoVersion;
|
return dom.pushGoVersion;
|
||||||
}
|
}
|
||||||
@ -220,6 +227,7 @@ public class Switch {
|
|||||||
public static void setPushJavaVersion(String pushJavaVersion) {
|
public static void setPushJavaVersion(String pushJavaVersion) {
|
||||||
dom.pushJavaVersion = pushJavaVersion;
|
dom.pushJavaVersion = pushJavaVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setPushGoVersion(String pushGoVersion) {
|
public static void setPushGoVersion(String pushGoVersion) {
|
||||||
dom.pushGoVersion = pushGoVersion;
|
dom.pushGoVersion = pushGoVersion;
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import com.alibaba.fastjson.JSON;
|
|||||||
import com.alibaba.fastjson.annotation.JSONField;
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
import com.alibaba.nacos.naming.core.Domain;
|
import com.alibaba.nacos.naming.core.Domain;
|
||||||
import com.alibaba.nacos.naming.core.IpAddress;
|
import com.alibaba.nacos.naming.core.IpAddress;
|
||||||
|
import com.alibaba.nacos.naming.healthcheck.HealthCheckMode;
|
||||||
import com.alibaba.nacos.naming.raft.RaftListener;
|
import com.alibaba.nacos.naming.raft.RaftListener;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
|
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
|
||||||
@ -52,6 +53,8 @@ public class SwitchDomain implements Domain, RaftListener {
|
|||||||
|
|
||||||
public boolean healthCheckEnabled = true;
|
public boolean healthCheckEnabled = true;
|
||||||
|
|
||||||
|
public String defaultHealthCheckMode = HealthCheckMode.client.name();
|
||||||
|
|
||||||
public boolean distroEnabled = true;
|
public boolean distroEnabled = true;
|
||||||
|
|
||||||
public boolean enableStandalone = true;
|
public boolean enableStandalone = true;
|
||||||
|
@ -38,6 +38,7 @@ public class SwitchEntry {
|
|||||||
public static final String MASTERS = "masters";
|
public static final String MASTERS = "masters";
|
||||||
public static final String DISTRO = "distro";
|
public static final String DISTRO = "distro";
|
||||||
public static final String CHECK = "check";
|
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 DOM_STATUS_SYNC_PERIOD = "domStatusSynchronizationPeriodMillis";
|
||||||
public static final String SERVER_STATUS_SYNC_PERIOD = "serverStatusSynchronizationPeriodMillis";
|
public static final String SERVER_STATUS_SYNC_PERIOD = "serverStatusSynchronizationPeriodMillis";
|
||||||
public static final String HEALTH_CHECK_TIMES = "healthCheckTimes";
|
public static final String HEALTH_CHECK_TIMES = "healthCheckTimes";
|
||||||
|
@ -27,10 +27,7 @@ import com.alibaba.nacos.core.utils.WebUtils;
|
|||||||
import com.alibaba.nacos.naming.boot.RunningConfig;
|
import com.alibaba.nacos.naming.boot.RunningConfig;
|
||||||
import com.alibaba.nacos.naming.core.*;
|
import com.alibaba.nacos.naming.core.*;
|
||||||
import com.alibaba.nacos.naming.exception.NacosException;
|
import com.alibaba.nacos.naming.exception.NacosException;
|
||||||
import com.alibaba.nacos.naming.healthcheck.AbstractHealthCheckProcessor;
|
import com.alibaba.nacos.naming.healthcheck.*;
|
||||||
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.misc.*;
|
import com.alibaba.nacos.naming.misc.*;
|
||||||
import com.alibaba.nacos.naming.push.ClientInfo;
|
import com.alibaba.nacos.naming.push.ClientInfo;
|
||||||
import com.alibaba.nacos.naming.push.DataSource;
|
import com.alibaba.nacos.naming.push.DataSource;
|
||||||
@ -386,11 +383,16 @@ public class ApiCommands {
|
|||||||
boolean isUseSpecifiedURL = Boolean.parseBoolean(WebUtils.optional(request, "isUseSpecifiedURL", "false"));
|
boolean isUseSpecifiedURL = Boolean.parseBoolean(WebUtils.optional(request, "isUseSpecifiedURL", "false"));
|
||||||
String envAndSite = WebUtils.optional(request, "envAndSites", StringUtils.EMPTY);
|
String envAndSite = WebUtils.optional(request, "envAndSites", StringUtils.EMPTY);
|
||||||
boolean resetWeight = Boolean.parseBoolean(WebUtils.optional(request, "resetWeight", "false"));
|
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"));
|
boolean enable = Boolean.parseBoolean(WebUtils.optional(request, "serviceEnabled", "true"));
|
||||||
|
|
||||||
String disabledSites = WebUtils.optional(request, "disabledSites", StringUtils.EMPTY);
|
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 clusterName = WebUtils.optional(request, "clusterName", UtilsAndCommons.DEFAULT_CLUSTER_NAME);
|
||||||
|
|
||||||
String serviceMetadataJson = WebUtils.optional(request, "serviceMetadata", StringUtils.EMPTY);
|
String serviceMetadataJson = WebUtils.optional(request, "serviceMetadata", StringUtils.EMPTY);
|
||||||
@ -1536,6 +1538,16 @@ public class ApiCommands {
|
|||||||
return "ok";
|
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)) {
|
if (entry.equals(SwitchEntry.DOM_STATUS_SYNC_PERIOD)) {
|
||||||
Long millis = Long.parseLong(WebUtils.required(request, "millis"));
|
Long millis = Long.parseLong(WebUtils.required(request, "millis"));
|
||||||
|
|
||||||
|
@ -24,9 +24,9 @@ import static com.alibaba.nacos.test.naming.NamingBase.*;
|
|||||||
/**
|
/**
|
||||||
* @author <a href="mailto:zpf.073@gmail.com">nkorange</a>
|
* @author <a href="mailto:zpf.073@gmail.com">nkorange</a>
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
//@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"},
|
//@SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"},
|
||||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
// webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
public class MultipleTenantTest {
|
public class MultipleTenantTest {
|
||||||
|
|
||||||
private NamingService naming;
|
private NamingService naming;
|
||||||
@ -42,6 +42,8 @@ public class MultipleTenantTest {
|
|||||||
|
|
||||||
TimeUnit.SECONDS.sleep(10);
|
TimeUnit.SECONDS.sleep(10);
|
||||||
|
|
||||||
|
port = 8848;
|
||||||
|
|
||||||
naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port);
|
naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port);
|
||||||
|
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
@ -86,6 +88,8 @@ public class MultipleTenantTest {
|
|||||||
instances = naming.getAllInstances(serviceName);
|
instances = naming.getAllInstances(serviceName);
|
||||||
|
|
||||||
Assert.assertEquals(2, instances.size());
|
Assert.assertEquals(2, instances.size());
|
||||||
|
|
||||||
|
TimeUnit.SECONDS.sleep(10000000L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -42,9 +42,9 @@ import static com.alibaba.nacos.test.naming.NamingBase.*;
|
|||||||
* @author wangtong.wt
|
* @author wangtong.wt
|
||||||
* @date 2018/6/20
|
* @date 2018/6/20
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
//@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"},
|
//@SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"},
|
||||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
// webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
public class RegisterInstance_ITCase {
|
public class RegisterInstance_ITCase {
|
||||||
|
|
||||||
private NamingService naming;
|
private NamingService naming;
|
||||||
@ -54,23 +54,23 @@ public class RegisterInstance_ITCase {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() throws Exception {
|
public void init() throws Exception {
|
||||||
if (naming == null) {
|
// if (naming == null) {
|
||||||
//TimeUnit.SECONDS.sleep(10);
|
// TimeUnit.SECONDS.sleep(10);
|
||||||
naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port);
|
// naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port);
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
// @Ignore
|
||||||
public void regService() throws NacosException, InterruptedException {
|
public void regService() throws NacosException, InterruptedException {
|
||||||
|
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848");
|
properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848");
|
||||||
properties.put(PropertyKeyConst.NAMESPACE, "t3");
|
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.1", 80, "c1");
|
||||||
naming.registerInstance(serviceName, "127.0.0.2", 80, "c2");
|
naming.registerInstance(serviceName, "127.0.0.2", 80, "c2");
|
||||||
Thread.sleep(100000000L);
|
Thread.sleep(100000000L);
|
||||||
|
Loading…
Reference in New Issue
Block a user