From bbc3c242266665537d26b6b6371a6ff571d36996 Mon Sep 17 00:00:00 2001 From: nkorange Date: Mon, 5 Nov 2018 17:09:00 +0800 Subject: [PATCH] Use HTTP redirect instead of resend request --- .../com/alibaba/nacos/api/naming/pojo/Service.java | 6 +++--- distribution/conf/nacos-logback.xml | 9 ++++++--- .../naming/controllers/ServiceController.java | 5 +---- .../alibaba/nacos/naming/core/DomainsManager.java | 4 ++++ .../com/alibaba/nacos/naming/misc/HttpClient.java | 9 +++++++-- .../com/alibaba/nacos/naming/web/ApiCommands.java | 14 ++++---------- .../com/alibaba/nacos/naming/web/DistroFilter.java | 12 ++++++++++++ 7 files changed, 37 insertions(+), 22 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Service.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Service.java index 2c952c6fc..cc270b084 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Service.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Service.java @@ -39,7 +39,7 @@ public class Service { private String app; /** - * Service group which is meant to classify services into different sets. + * Service group is meant to classify services into different sets. */ private String group; @@ -48,12 +48,12 @@ public class Service { */ private String healthCheckMode; + private Map metadata = new HashMap(); + public Service(String name) { this.name = name; } - private Map metadata = new HashMap(); - public String getName() { return name; } diff --git a/distribution/conf/nacos-logback.xml b/distribution/conf/nacos-logback.xml index 09fa2b525..c4f031b8e 100644 --- a/distribution/conf/nacos-logback.xml +++ b/distribution/conf/nacos-logback.xml @@ -20,7 +20,8 @@ 0 - 512 + 1024 + true @@ -43,7 +44,8 @@ 0 - 512 + 1024 + true @@ -66,7 +68,8 @@ 0 - 512 + 1024 + true 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 61ce14adc..a6810e0fd 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,9 +15,7 @@ */ package com.alibaba.nacos.naming.controllers; -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; @@ -60,8 +58,7 @@ public class ServiceController { String metadata = BaseServlet.optional(request, "metadata", StringUtils.EMPTY); Map metadataMap = new HashMap<>(16); if (StringUtils.isNotBlank(metadata)) { - metadataMap = JSON.parseObject(metadata, new TypeReference>() { - }); + metadataMap = UtilsAndCommons.parseMetadata(metadata); } VirtualClusterDomain domObj = new VirtualClusterDomain(); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/DomainsManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/DomainsManager.java index 690410c4c..98f5a7ab8 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/DomainsManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/DomainsManager.java @@ -62,6 +62,10 @@ public class DomainsManager { private Map dom2LockMap = new ConcurrentHashMap<>(); + public Map getDom2LockMap() { + return dom2LockMap; + } + /** * thread pool that processes getting domain detail from other server asynchronously */ diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java index 718b7d081..58ca5d52f 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java @@ -15,7 +15,6 @@ */ package com.alibaba.nacos.naming.misc; -import com.alibaba.nacos.common.util.IoUtils; import com.ning.http.client.AsyncCompletionHandler; import com.ning.http.client.AsyncHttpClient; import com.ning.http.client.AsyncHttpClientConfig; @@ -25,15 +24,19 @@ import org.apache.commons.collections.MapUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.*; +import org.apache.http.client.RedirectStrategy; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicNameValuePair; +import org.apache.http.protocol.HttpContext; import java.io.IOException; import java.io.InputStream; @@ -56,6 +59,8 @@ public class HttpClient { private static CloseableHttpClient postClient; + private static PoolingHttpClientConnectionManager connectionManager; + static { AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder(); builder.setMaximumConnectionsTotal(-1); @@ -74,7 +79,7 @@ public class HttpClient { HttpClientBuilder builder2 = HttpClients.custom(); builder2.setUserAgent(UtilsAndCommons.SERVER_VERSION); builder2.setConnectionTimeToLive(CON_TIME_OUT_MILLIS, TimeUnit.MILLISECONDS); - builder2.setMaxConnPerRoute(256); + builder2.setMaxConnPerRoute(-1); builder2.setMaxConnTotal(-1); builder2.disableAutomaticRetries(); // builder2.disableConnectionState() 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 f4b9cbd6c..bcc963295 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 @@ -366,10 +366,10 @@ public class ApiCommands { boolean isUseSpecifiedURL = Boolean.parseBoolean(BaseServlet.optional(request, "isUseSpecifiedURL", "false")); String envAndSite = BaseServlet.optional(request, "envAndSites", StringUtils.EMPTY); boolean resetWeight = Boolean.parseBoolean(BaseServlet.optional(request, "resetWeight", "false")); - boolean enableHealthCheck = Boolean.parseBoolean(BaseServlet.optional(request, "enableHealthCheck", "true")); + boolean enableHealthCheck = Boolean.parseBoolean(BaseServlet.optional(request, "enableHealthCheck", "false")); boolean enable = Boolean.parseBoolean(BaseServlet.optional(request, "enable", "true")); String disabledSites = BaseServlet.optional(request, "disabledSites", StringUtils.EMPTY); - boolean eanbleClientBeat = Boolean.parseBoolean(BaseServlet.optional(request, "enableClientBeat", "false")); + boolean eanbleClientBeat = Boolean.parseBoolean(BaseServlet.optional(request, "enableClientBeat", "true")); String clusterName = BaseServlet.optional(request, "clusterName", UtilsAndCommons.DEFAULT_CLUSTER_NAME); String serviceMetadataJson = BaseServlet.optional(request, "serviceMetadata", StringUtils.EMPTY); @@ -886,8 +886,6 @@ public class ApiCommands { private String doAddIP4Dom(HttpServletRequest request) throws Exception { - long start = System.currentTimeMillis(); - if (Switch.getDisableAddIP()) { throw new AccessControlException("Adding IP for dom is forbidden now."); } @@ -963,11 +961,9 @@ public class ApiCommands { long timestamp = System.currentTimeMillis(); - Loggers.EVT_LOG.info("[ADD IP] before raft:" + (System.currentTimeMillis() - start)); - if (RaftCore.isLeader()) { try { - RaftCore.OPERATE_LOCK.lock(); + domainsManager.getDom2LockMap().get(dom).lock(); proxyParams.put("clientIP", NetUtils.localIP()); proxyParams.put("notify", "true"); @@ -1010,12 +1006,10 @@ public class ApiCommands { + Arrays.toString(ipList.toArray()) + " operatorIP: " + BaseServlet.optional(request, "clientIP", "unknown")); } finally { - RaftCore.OPERATE_LOCK.unlock(); + domainsManager.getDom2LockMap().get(dom).unlock(); } } - Loggers.EVT_LOG.info("[ADD IP] after raft:" + (System.currentTimeMillis() - start)); - return "ok"; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java b/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java index 421e2405b..994128bf3 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java @@ -19,8 +19,10 @@ import com.alibaba.nacos.naming.core.DistroMapper; import com.alibaba.nacos.naming.misc.Loggers; import com.alibaba.nacos.naming.misc.Switch; import com.alibaba.nacos.naming.misc.UtilsAndCommons; +import com.alibaba.nacos.naming.raft.RaftCore; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import org.apache.commons.lang3.StringUtils; +import org.springframework.http.HttpMethod; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; @@ -56,6 +58,16 @@ 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); + } + return; + } + if (!Switch.isDistroEnabled()) { filterChain.doFilter(req, resp); return;