#209 Disable service and cluster level customization in client

This commit is contained in:
nkorange 2018-11-08 16:34:18 +08:00
parent 3c608ab2e5
commit d3751a99af
12 changed files with 43 additions and 86 deletions

View File

@ -17,7 +17,6 @@ package com.alibaba.nacos.api.naming.pojo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.nacos.api.common.Constants;
import java.util.HashMap;
import java.util.Map;
@ -58,14 +57,12 @@ public class Instance {
/**
* Cluster information of instance
*/
@JSONField(serialize = false)
private Cluster cluster = new Cluster();
private String clusterName;
/**
* Service information of instance
* Service name of instance
*/
@JSONField(serialize = false)
private Service service;
private String serviceName;
/**
* User extended attributes
@ -80,14 +77,6 @@ public class Instance {
this.instanceId = instanceId;
}
public String serviceName() {
String[] infos = instanceId.split(Constants.NAMING_INSTANCE_ID_SPLITTER);
if (infos.length < Constants.NAMING_INSTANCE_ID_SEG_COUNT) {
return null;
}
return infos[Constants.NAMING_INSTANCE_ID_SEG_COUNT - 1];
}
public String getIp() {
return ip;
}
@ -120,20 +109,20 @@ public class Instance {
this.healthy = healthy;
}
public Cluster getCluster() {
return cluster;
public String getClusterName() {
return clusterName;
}
public void setCluster(Cluster cluster) {
this.cluster = cluster;
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
public Service getService() {
return service;
public String getServiceName() {
return serviceName;
}
public void setService(Service service) {
this.service = service;
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
public Map<String, String> getMetadata() {

View File

@ -148,7 +148,7 @@ public class NacosNamingService implements NamingService {
instance.setIp(ip);
instance.setPort(port);
instance.setWeight(1.0);
instance.setCluster(new Cluster(clusterName));
instance.setClusterName(clusterName);
registerInstance(serviceName, instance);
}
@ -160,7 +160,7 @@ public class NacosNamingService implements NamingService {
beatInfo.setDom(serviceName);
beatInfo.setIp(instance.getIp());
beatInfo.setPort(instance.getPort());
beatInfo.setCluster(instance.getCluster().getName());
beatInfo.setCluster(instance.getClusterName());
beatInfo.setWeight(instance.getWeight());
beatInfo.setMetadata(instance.getMetadata());

View File

@ -161,12 +161,8 @@ public class NamingProxy {
params.put("enable", String.valueOf(instance.isEnabled()));
params.put("healthy", String.valueOf(instance.isHealthy()));
params.put("metadata", JSON.toJSONString(instance.getMetadata()));
if (instance.getService() == null) {
params.put("serviceName", serviceName);
} else {
params.put("service", JSON.toJSONString(instance.getService()));
}
params.put("cluster", JSON.toJSONString(instance.getCluster()));
params.put("serviceName", serviceName);
params.put("clusterName", instance.getClusterName());
reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, "PUT");
}

View File

@ -38,8 +38,6 @@ public class IpAddress extends Instance implements Comparable {
private static final double MIN_POSTIVE_WEIGHT_VALUE = 0.01D;
private static final double MIN_WEIGHT_VALUE = 0.00D;
private String clusterName = UtilsAndCommons.DEFAULT_CLUSTER_NAME;
private volatile long lastBeat = System.currentTimeMillis();
@JSONField(serialize = false)
@ -78,19 +76,19 @@ public class IpAddress extends Instance implements Comparable {
public IpAddress(String ip, int port) {
this.setIp(ip);
this.setPort(port);
this.clusterName = UtilsAndCommons.DEFAULT_CLUSTER_NAME;
this.setClusterName(UtilsAndCommons.DEFAULT_CLUSTER_NAME);
}
public IpAddress(String ip, int port, String clusterName) {
this.setIp(ip.trim());
this.setPort(port);
this.clusterName = clusterName;
this.setClusterName(clusterName);
}
public IpAddress(String ip, int port, String clusterName, String tenant, String app) {
this.setIp(ip.trim());
this.setPort(port);
this.clusterName = clusterName;
this.setClusterName(clusterName);
this.tenant = tenant;
this.app = app;
}
@ -166,7 +164,7 @@ public class IpAddress extends Instance implements Comparable {
@Override
public String toString() {
return getDatumKey() + SPLITER + getWeight() + SPLITER + isHealthy() + SPLITER + marked + SPLITER + clusterName;
return getDatumKey() + SPLITER + getWeight() + SPLITER + isHealthy() + SPLITER + marked + SPLITER + getClusterName();
}
public String toJSON() {
@ -264,14 +262,6 @@ public class IpAddress extends Instance implements Comparable {
HealthCheckStatus.get(this).checkRT = checkRT;
}
public String getClusterName() {
return clusterName;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
public synchronized void setValid(boolean valid) {
setHealthy(valid);
}
@ -305,7 +295,7 @@ public class IpAddress extends Instance implements Comparable {
}
public String generateInstanceId() {
return getIp() + "#" + getPort() + "#" + getClusterName() + "#" + getService().getName();
return getIp() + "#" + getPort() + "#" + getClusterName() + "#" + getServiceName();
}
@Override

View File

@ -232,10 +232,6 @@ public class VirtualClusterDomain implements Domain, RaftListener {
for (Map.Entry<String, List<IpAddress>> entry : ipMap.entrySet()) {
//make every ip mine
List<IpAddress> entryIPs = entry.getValue();
for (IpAddress ip : entryIPs) {
ip.setCluster(clusterMap.get(ip.getClusterName()));
}
clusterMap.get(entry.getKey()).updateIPs(entryIPs);
}
setLastModifiedMillis(System.currentTimeMillis());

View File

@ -57,8 +57,8 @@ public class HealthCheckStatus {
private static String buildKey(IpAddress ip) {
try {
String clusterName = ip.getCluster().getName();
String dom = ip.serviceName();
String clusterName = ip.getClusterName();
String dom = ip.getServiceName();
String datumKey = ip.getDatumKey();
return dom + ":"
+ clusterName + ":"

View File

@ -78,6 +78,8 @@ public class UtilsAndCommons {
public static final String DEFAULT_CLUSTER_NAME = "DEFAULT";
public static final int RAFT_PUBLISH_TIMEOUT = 5000;
static public final String RAFT_DOM_PRE = "meta";
static public final String RAFT_IPLIST_PRE = "iplist.";
static public final String RAFT_TAG_DOM_PRE = "tag.meta";

View File

@ -290,7 +290,7 @@ public class RaftCore {
}
if (!latch.await(5000, TimeUnit.MILLISECONDS)) {
if (!latch.await(UtilsAndCommons.RAFT_PUBLISH_TIMEOUT, TimeUnit.MILLISECONDS)) {
// only majority servers return success can we consider this update success
Loggers.RAFT.info("data publish failed, caused failed to notify majority, key=" + key);
throw new IllegalStateException("data publish failed, caused failed to notify majority, key=" + key);

View File

@ -20,7 +20,6 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.nacos.api.naming.pojo.AbstractHealthChecker;
import com.alibaba.nacos.api.naming.pojo.Service;
import com.alibaba.nacos.common.util.IoUtils;
import com.alibaba.nacos.common.util.Md5Utils;
import com.alibaba.nacos.common.util.SystemUtils;
@ -543,9 +542,8 @@ public class ApiCommands {
VirtualClusterDomain virtualClusterDomain = (VirtualClusterDomain) domainsManager.getDomain(dom);
IpAddress ipAddress = getIPAddress(request);
Service service = new Service(dom);
ipAddress.setApp(app);
ipAddress.setService(service);
ipAddress.setServiceName(dom);
ipAddress.setInstanceId(ipAddress.generateInstanceId());
ipAddress.setLastBeat(System.currentTimeMillis());
if (StringUtils.isNotEmpty(metadata)) {

View File

@ -58,14 +58,17 @@ public class DistroFilter implements Filter {
}
}
if (HttpMethod.PUT.name().equals(req.getMethod()) && req.getRequestURI().contains("instance") && !RaftCore.isLeader()) {
String url = "http://" + RaftCore.getLeader().ip + req.getRequestURI() + "?" + req.getQueryString();
try {
resp.sendRedirect(url);
} catch (Exception ignore) {
Loggers.SRV_LOG.warn("DISTRO-FILTER", "request failed: " + url);
if (req.getRequestURI().contains(UtilsAndCommons.NACOS_NAMING_INSTANCE_CONTEXT) && !RaftCore.isLeader()) {
if (HttpMethod.PUT.name().equals(req.getMethod()) && HttpMethod.DELETE.name().equals(req.getMethod())) {
String url = "http://" + RaftCore.getLeader().ip + req.getRequestURI() + "?" + req.getQueryString();
try {
resp.sendRedirect(url);
} catch (Exception ignore) {
Loggers.SRV_LOG.warn("DISTRO-FILTER", "request failed: " + url);
}
return;
}
return;
}
if (!Switch.isDistroEnabled()) {

View File

@ -31,6 +31,7 @@ import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -134,9 +135,11 @@ public class InstanceControllerTest extends BaseTest {
Mockito.when(domainsManager.getDomain("nacos.test.1")).thenReturn(domain);
MockHttpServletRequestBuilder builder =
MockMvcRequestBuilders.get("/naming/instances")
MockMvcRequestBuilders.get("/v1/ns/instances")
.param("serviceName", "nacos.test.1");
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
MockHttpServletResponse response = mockmvc.perform(builder).andReturn().getResponse();
String actualValue = response.getContentAsString();
JSONObject result = JSON.parseObject(actualValue);
Assert.assertEquals("nacos.test.1", result.getString("dom"));

View File

@ -70,28 +70,8 @@ public class NamingBase {
instanceMeta.put("site", "et2");
instance.setMetadata(instanceMeta);
Service service = new Service(serviceName);
service.setApp("nacos-naming");
service.setHealthCheckMode("server");
service.setProtectThreshold(0.8F);
service.setGroup("CNCF");
Map<String, String> serviceMeta = new HashMap<String, String>();
serviceMeta.put("symmetricCall", "true");
service.setMetadata(serviceMeta);
instance.setService(service);
Cluster cluster = new Cluster();
cluster.setName("c1");
AbstractHealthChecker.Http healthChecker = new AbstractHealthChecker.Http();
healthChecker.setExpectedResponseCode(400);
healthChecker.setHeaders("Client-Version|Nacos");
healthChecker.setPath("/xxx.html");
cluster.setHealthChecker(healthChecker);
Map<String, String> clusterMeta = new HashMap<String, String>();
clusterMeta.put("xxx", "yyyy");
cluster.setMetadata(clusterMeta);
instance.setCluster(cluster);
instance.setServiceName(serviceName);
instance.setClusterName("c1");
return instance;
}