Skip to content

Commit

Permalink
Use HTTP redirect instead of resend request
Browse files Browse the repository at this point in the history
  • Loading branch information
nkorange committed Nov 5, 2018
1 parent a020bcd commit bbc3c24
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
9 changes: 6 additions & 3 deletions distribution/conf/nacos-logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -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>

Expand All @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,8 +58,7 @@ public String create(HttpServletRequest request) throws Exception {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,15 +24,19 @@
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;
Expand All @@ -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);
Expand All @@ -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()
Expand Down
14 changes: 4 additions & 10 deletions naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,10 @@ private String addOrReplaceDom(HttpServletRequest request) throws Exception {
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);
Expand Down Expand Up @@ -886,8 +886,6 @@ public String onAddIP4Dom(HttpServletRequest request) throws Exception {

private String doAddIP4Dom(HttpServletRequest request) throws Exception {

long start = System.currentTimeMillis();

if (Switch.getDisableAddIP()) {
throw new AccessControlException("Adding IP for dom is forbidden now.");
}
Expand Down Expand Up @@ -963,11 +961,9 @@ private String doAddIP4Dom(HttpServletRequest request) throws Exception {

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");

Expand Down Expand Up @@ -1010,12 +1006,10 @@ public Integer onCompleted(Response response) throws Exception {
+ 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";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
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;
Expand Down Expand Up @@ -56,6 +58,16 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
}
}

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;
Expand Down

0 comments on commit bbc3c24

Please sign in to comment.