Skip to content

Commit

Permalink
增加核心策略Header开关
Browse files Browse the repository at this point in the history
  • Loading branch information
HaojunRen committed Aug 31, 2020
1 parent 7d9295d commit 78cb810
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.ConfigurableEnvironment;
Expand All @@ -28,6 +30,8 @@
import com.nepxion.discovery.plugin.strategy.service.context.ServiceStrategyContextHolder;

public abstract class AbstractStrategyInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(AbstractStrategyInterceptor.class);

@Autowired
protected ConfigurableEnvironment environment;

Expand All @@ -49,6 +53,10 @@ public AbstractStrategyInterceptor(String contextRequestHeaders, String business
if (StringUtils.isNotEmpty(businessRequestHeaders)) {
requestHeaderList.addAll(StringUtil.splitToList(businessRequestHeaders.toLowerCase(), DiscoveryConstant.SEPARATE));
}

LOG.info("------- " + getInterceptorName() + " Intercept Information -------");
LOG.info(getInterceptorName() + " desires to intercept customer headers are {}", requestHeaderList);
LOG.info("--------------------------------------------------");
}

protected void interceptInputHeader() {
Expand All @@ -67,7 +75,7 @@ protected void interceptInputHeader() {
return;
}

System.out.println("------- Intercept Input Header Information -------");
System.out.println("------- " + getInterceptorName() + " Intercept Input Header Information -------");
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
boolean isHeaderContains = isHeaderContains(headerName.toLowerCase());
Expand Down Expand Up @@ -96,4 +104,14 @@ protected boolean isHeaderContainsExcludeInner(String headerName) {
!StringUtils.equals(headerName, DiscoveryConstant.N_D_SERVICE_ENVIRONMENT);
// return isHeaderContains(headerName) && !headerName.startsWith(DiscoveryConstant.N_D_SERVICE_PREFIX);
}

protected boolean isCoreHeaderContains(String headerName) {
return StringUtils.equals(headerName, DiscoveryConstant.N_D_VERSION) ||
StringUtils.equals(headerName, DiscoveryConstant.N_D_REGION) ||
StringUtils.equals(headerName, DiscoveryConstant.N_D_ADDRESS) ||
StringUtils.equals(headerName, DiscoveryConstant.N_D_VERSION_WEIGHT) ||
StringUtils.equals(headerName, DiscoveryConstant.N_D_REGION_WEIGHT);
}

protected abstract String getInterceptorName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.context.request.ServletRequestAttributes;
Expand All @@ -31,8 +29,6 @@
import com.nepxion.discovery.plugin.strategy.service.filter.ServiceStrategyRouteFilter;

public class FeignStrategyInterceptor extends AbstractStrategyInterceptor implements RequestInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(FeignStrategyInterceptor.class);

@Autowired
private ServiceStrategyRouteFilter serviceStrategyRouteFilter;

Expand All @@ -48,10 +44,6 @@ public class FeignStrategyInterceptor extends AbstractStrategyInterceptor implem

public FeignStrategyInterceptor(String contextRequestHeaders, String businessRequestHeaders) {
super(contextRequestHeaders, businessRequestHeaders);

LOG.info("----------- Feign Intercept Information ----------");
LOG.info("Feign desires to intercept customer headers are {}", requestHeaderList);
LOG.info("--------------------------------------------------");
}

@Override
Expand Down Expand Up @@ -95,7 +87,14 @@ private void applyOuterHeader(RequestTemplate requestTemplate) {
String headerValue = previousRequest.getHeader(headerName);
boolean isHeaderContains = isHeaderContainsExcludeInner(headerName.toLowerCase());
if (isHeaderContains) {
requestTemplate.header(headerName, headerValue);
if (feignCoreHeaderTransmissionEnabled) {
requestTemplate.header(headerName, headerValue);
} else {
boolean isCoreHeaderContains = isCoreHeaderContains(headerName);
if (!isCoreHeaderContains) {
requestTemplate.header(headerName, headerValue);
}
}
}
}

Expand Down Expand Up @@ -139,7 +138,7 @@ private void interceptOutputHeader(RequestTemplate requestTemplate) {
return;
}

System.out.println("------- Intercept Output Header Information ------");
System.out.println("------- " + getInterceptorName() + " Intercept Output Header Information ------");
Map<String, Collection<String>> headers = requestTemplate.headers();
for (Map.Entry<String, Collection<String>> entry : headers.entrySet()) {
String headerName = entry.getKey();
Expand All @@ -152,4 +151,9 @@ private void interceptOutputHeader(RequestTemplate requestTemplate) {
}
System.out.println("--------------------------------------------------");
}

@Override
protected String getInterceptorName() {
return "Feign";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
Expand All @@ -35,8 +33,6 @@
import com.nepxion.discovery.plugin.strategy.service.filter.ServiceStrategyRouteFilter;

public class RestTemplateStrategyInterceptor extends AbstractStrategyInterceptor implements ClientHttpRequestInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(RestTemplateStrategyInterceptor.class);

@Autowired
private ServiceStrategyRouteFilter serviceStrategyRouteFilter;

Expand All @@ -52,10 +48,6 @@ public class RestTemplateStrategyInterceptor extends AbstractStrategyInterceptor

public RestTemplateStrategyInterceptor(String contextRequestHeaders, String businessRequestHeaders) {
super(contextRequestHeaders, businessRequestHeaders);

LOG.info("------- RestTemplate Intercept Information -------");
LOG.info("RestTemplate desires to intercept customer headers are {}", requestHeaderList);
LOG.info("--------------------------------------------------");
}

@Override
Expand Down Expand Up @@ -103,7 +95,14 @@ private void applyOuterHeader(HttpRequest request) {
String headerValue = previousRequest.getHeader(headerName);
boolean isHeaderContains = isHeaderContainsExcludeInner(headerName.toLowerCase());
if (isHeaderContains) {
headers.add(headerName, headerValue);
if (restTemplateCoreHeaderTransmissionEnabled) {
headers.add(headerName, headerValue);
} else {
boolean isCoreHeaderContains = isCoreHeaderContains(headerName);
if (!isCoreHeaderContains) {
headers.add(headerName, headerValue);
}
}
}
}

Expand Down Expand Up @@ -146,7 +145,7 @@ private void interceptOutputHeader(HttpRequest request) {
return;
}

System.out.println("------- Intercept Output Header Information ------");
System.out.println("------- " + getInterceptorName() + " Intercept Output Header Information ------");
HttpHeaders headers = request.getHeaders();
for (Iterator<Entry<String, List<String>>> iterator = headers.entrySet().iterator(); iterator.hasNext();) {
Entry<String, List<String>> header = iterator.next();
Expand All @@ -160,4 +159,9 @@ private void interceptOutputHeader(HttpRequest request) {
}
System.out.println("--------------------------------------------------");
}

@Override
protected String getInterceptorName() {
return "RestTemplate";
}
}

0 comments on commit 78cb810

Please sign in to comment.