Use HTTP redirect instead of resend request

This commit is contained in:
nkorange 2018-11-05 17:09:00 +08:00
parent a020bcd783
commit bbc3c24226
7 changed files with 37 additions and 22 deletions

View File

@ -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<String, String> metadata = new HashMap<String, String>();
public Service(String name) {
this.name = name;
}
private Map<String, String> metadata = new HashMap<String, String>();
public String getName() {
return name;
}

View File

@ -20,7 +20,8 @@
<appender name="async-naming-server" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<queueSize>512</queueSize>
<queueSize>1024</queueSize>
<neverBlock>true</neverBlock>
<appender-ref ref="naming-server"/>
</appender>
@ -43,7 +44,8 @@
<appender name="async-naming-raft" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<queueSize>512</queueSize>
<queueSize>1024</queueSize>
<neverBlock>true</neverBlock>
<appender-ref ref="naming-raft"/>
</appender>
@ -66,7 +68,8 @@
<appender name="async-naming-event" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<queueSize>512</queueSize>
<queueSize>1024</queueSize>
<neverBlock>true</neverBlock>
<appender-ref ref="naming-event"/>
</appender>

View File

@ -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<String, String> metadataMap = new HashMap<>(16);
if (StringUtils.isNotBlank(metadata)) {
metadataMap = JSON.parseObject(metadata, new TypeReference<Map<String, String>>() {
});
metadataMap = UtilsAndCommons.parseMetadata(metadata);
}
VirtualClusterDomain domObj = new VirtualClusterDomain();

View File

@ -62,6 +62,10 @@ public class DomainsManager {
private Map<String, Lock> dom2LockMap = new ConcurrentHashMap<>();
public Map<String, Lock> getDom2LockMap() {
return dom2LockMap;
}
/**
* thread pool that processes getting domain detail from other server asynchronously
*/

View File

@ -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()

View File

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

View File

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