From a020bcd783d90506c715f1219e1a4371b2470fc0 Mon Sep 17 00:00:00 2001 From: nkorange Date: Wed, 31 Oct 2018 21:16:55 +0800 Subject: [PATCH] #177 Finish backend. --- .../naming/controllers/ClusterController.java | 9 +- .../naming/controllers/ServiceController.java | 100 +++++++++++++++--- .../alibaba/nacos/naming/web/ApiCommands.java | 1 - 3 files changed, 91 insertions(+), 19 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ClusterController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ClusterController.java index f73931707..c340984c1 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ClusterController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ClusterController.java @@ -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)); 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 7cceae0c9..61ce14adc 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 @@ -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 metadataMap = new HashMap<>(16); + if (StringUtils.isNotBlank(metadata)) { + metadataMap = JSON.parseObject(metadata, new TypeReference>() { + }); + } + + 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"; - } - } 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 a2a3eb533..f4b9cbd6c 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 @@ -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);