Skip to content

Commit

Permalink
#523 add switch
Browse files Browse the repository at this point in the history
  • Loading branch information
nkorange committed Dec 25, 2018
1 parent 478cb15 commit 16794d1
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -62,7 +63,7 @@ public String create(HttpServletRequest request) throws Exception {
}

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<String, String> metadataMap = new HashMap<>(16);
Expand Down
20 changes: 14 additions & 6 deletions naming/src/main/java/com/alibaba/nacos/naming/misc/Switch.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ public void onChange(String key, String value) throws Exception {
if (StringUtils.isEmpty(value)) {
return;
}
SwitchDomain switchDomain = JSON.parseObject(value, new TypeReference<SwitchDomain>() {
dom = JSON.parseObject(value, new TypeReference<SwitchDomain>() {
});

dom = switchDomain;
}

@Override
Expand All @@ -77,7 +75,7 @@ public void onDelete(String key, String value) throws Exception {

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;
}

Expand All @@ -90,7 +88,7 @@ public static long getPushCacheMillis() {

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;
}

Expand Down Expand Up @@ -165,6 +163,14 @@ public static void setHeathCheckEnabled(boolean 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() {
return dom.isEnableAuthentication();
}
Expand All @@ -191,7 +197,7 @@ public static float getDistroThreshold() {

public static Integer getAdWeight(String ip) {
if (dom.adWeightMap == null
|| !dom.adWeightMap.containsKey(ip)) {
|| !dom.adWeightMap.containsKey(ip)) {
return 0;
}

Expand All @@ -205,6 +211,7 @@ public static void setAdWeight(String ip, int weight) {
public static String getPushJavaVersion() {
return dom.pushJavaVersion;
}

public static String getPushGoVersion() {
return dom.pushGoVersion;
}
Expand All @@ -220,6 +227,7 @@ public static String getPushCVersion() {
public static void setPushJavaVersion(String pushJavaVersion) {
dom.pushJavaVersion = pushJavaVersion;
}

public static void setPushGoVersion(String pushGoVersion) {
dom.pushGoVersion = pushGoVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
24 changes: 18 additions & 6 deletions naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
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;
Expand Down Expand Up @@ -386,11 +383,16 @@ private String addOrReplaceDom(HttpServletRequest request) throws Exception {
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);
Expand Down Expand Up @@ -1536,6 +1538,16 @@ public String updateSwitch(HttpServletRequest request) throws Exception {
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"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
/**
* @author <a href="mailto:[email protected]">nkorange</a>
*/
@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;
Expand All @@ -42,6 +42,8 @@ public void init() throws Exception {

TimeUnit.SECONDS.sleep(10);

port = 8848;

naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port);

Properties properties = new Properties();
Expand Down Expand Up @@ -86,6 +88,8 @@ public void registerInstance() throws Exception {
instances = naming.getAllInstances(serviceName);

Assert.assertEquals(2, instances.size());

TimeUnit.SECONDS.sleep(10000000L);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
* @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;
Expand All @@ -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);
Expand Down

0 comments on commit 16794d1

Please sign in to comment.