#177 Finish backend.

This commit is contained in:
nkorange 2018-10-31 21:16:55 +08:00
parent 1a9a282682
commit a020bcd783
3 changed files with 91 additions and 19 deletions

View File

@ -22,6 +22,7 @@ import com.alibaba.nacos.naming.core.Cluster;
import com.alibaba.nacos.naming.core.DomainsManager;
import com.alibaba.nacos.naming.core.VirtualClusterDomain;
import com.alibaba.nacos.naming.exception.NacosException;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.web.BaseServlet;
import org.apache.commons.lang3.BooleanUtils;
@ -44,7 +45,7 @@ public class ClusterController {
@Autowired
protected DomainsManager domainsManager;
@RequestMapping(value = "/update", method = RequestMethod.POST)
@RequestMapping(value = {"/update", "/add"}, method = RequestMethod.POST)
public String update(HttpServletRequest request) throws Exception {
String clusterName = BaseServlet.required(request, "clusterName");
@ -61,7 +62,11 @@ public class ClusterController {
Cluster cluster = domain.getClusterMap().get(clusterName);
if (cluster == null) {
throw new NacosException(NacosException.INVALID_PARAM, "cluster not found:"+ clusterName + ", " + serviceName);
Loggers.SRV_LOG.warn("UPDATE-CLUSTER", "cluster not exist, will create it: " + clusterName + ", service:" + serviceName);
cluster = new Cluster();
cluster.setName(clusterName);
// throw new NacosException(NacosException.INVALID_PARAM, "cluster not found:"+ clusterName + ", " + serviceName);
}
cluster.setDefCkport(NumberUtils.toInt(checkPort));

View File

@ -15,11 +15,10 @@
*/
package com.alibaba.nacos.naming.controllers;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.nacos.api.naming.pojo.Service;
import com.alibaba.nacos.naming.core.DomainsManager;
import com.alibaba.nacos.naming.core.VirtualClusterDomain;
import com.alibaba.nacos.naming.exception.NacosException;
@ -33,6 +32,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@ -45,6 +47,84 @@ public class ServiceController {
@Autowired
protected DomainsManager domainsManager;
@RequestMapping(value = "/create", method = RequestMethod.PUT)
public String create(HttpServletRequest request) throws Exception {
String serviceName = BaseServlet.required(request, "serviceName");
if (domainsManager.getDomain(serviceName) != null) {
throw new IllegalArgumentException("specified service already exists, serviceName : " + serviceName);
}
float protectThreshold = NumberUtils.toFloat(BaseServlet.optional(request, "protectThreshold", "0"));
String healthCheckMode = BaseServlet.optional(request, "healthCheckMode", "client");
String metadata = BaseServlet.optional(request, "metadata", StringUtils.EMPTY);
Map<String, String> metadataMap = new HashMap<>(16);
if (StringUtils.isNotBlank(metadata)) {
metadataMap = JSON.parseObject(metadata, new TypeReference<Map<String, String>>() {
});
}
VirtualClusterDomain domObj = new VirtualClusterDomain();
domObj.setName(serviceName);
domObj.setProtectThreshold(protectThreshold);
domObj.setEnableHealthCheck(HealthCheckMode.server.name().equals(healthCheckMode.toLowerCase()));
domObj.setEnabled(true);
domObj.setEnableClientBeat(HealthCheckMode.client.name().equals(healthCheckMode.toLowerCase()));
domObj.setMetadata(metadataMap);
// now valid the dom. if failed, exception will be thrown
domObj.setLastModifiedMillis(System.currentTimeMillis());
domObj.recalculateChecksum();
domObj.valid();
domainsManager.easyAddOrReplaceDom(domObj);
return "ok";
}
@RequestMapping(value = "/remove", method = RequestMethod.DELETE)
public String remove(HttpServletRequest request) throws Exception {
String serviceName = BaseServlet.required(request, "serviceName");
VirtualClusterDomain service = (VirtualClusterDomain) domainsManager.getDomain(serviceName);
if (service == null) {
throw new IllegalArgumentException("specified service not exist, serviceName : " + serviceName);
}
if (!service.allIPs().isEmpty()) {
throw new IllegalArgumentException("specified service has instances, serviceName : " + serviceName);
}
domainsManager.easyRemoveDom(serviceName);
return "ok";
}
@RequestMapping(value = "/detail")
public Service detail(HttpServletRequest request) throws Exception {
String serviceName = BaseServlet.required(request, "serviceName");
VirtualClusterDomain domain = (VirtualClusterDomain) domainsManager.getDomain(serviceName);
if (domain == null) {
throw new NacosException(NacosException.NOT_FOUND, "serivce " + serviceName + " is not found!");
}
Service service = new Service(serviceName);
service.setName(serviceName);
service.setProtectThreshold(domain.getProtectThreshold());
service.setHealthCheckMode(HealthCheckMode.none.name());
if (domain.getEnableHealthCheck()) {
service.setHealthCheckMode(HealthCheckMode.server.name());
}
if (domain.getEnableClientBeat()) {
service.setHealthCheckMode(HealthCheckMode.client.name());
}
service.setMetadata(domain.getMetadata());
return service;
}
@RequestMapping(value = "/list", method = RequestMethod.GET)
public JSONObject list(HttpServletRequest request) throws Exception {
@ -115,16 +195,4 @@ public class ServiceController {
return "ok";
}
@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
public String removeService(HttpServletRequest request) throws Exception {
String serviceName = BaseServlet.required(request, "serviceName");
if (domainsManager.getDomain(serviceName) == null) {
throw new IllegalStateException("service doesn't exists.");
}
domainsManager.easyRemoveDom(serviceName);
return "ok";
}
}

View File

@ -257,7 +257,6 @@ public class ApiCommands {
@RequestMapping("/regDom")
public String regDom(HttpServletRequest request) throws Exception {
String dom = BaseServlet.required(request, "dom");
if (domainsManager.getDomain(dom) != null) {
throw new IllegalArgumentException("specified dom already exists, dom : " + dom);