文档章节

ZuulFilter的order小结

go4it
 go4it
发布于 2017/05/31 00:12
字数 785
阅读 218
收藏 0

##filters org/springframework/cloud/netflix/zuul/filters/

##pre |filter|type|order|desc| |:---|:---| :----|:----| |ServletDetectionFilter|pre| -3 |Detects whether a request is ran through the {@link DispatcherServlet} or {@link ZuulServlet}.The purpose was to detect this up-front at the very beginning of Zuul filter processing and rely on this information in all filters.RequestContext is used such that the information is accessible to classes which do not have a request reference.| |Servlet30WrapperFilter|pre| -2 |zuul适配servlet request| |FormBodyWrapperFilter| pre | -1 | 将上下文中ZuulRequestHeaders的content-type设置成了带文件类型+boundary的形式.| |DebugFilter| pre | 1 |debug路由标识| |PreDecorationFilter|pre| 5 |补充头信息,比如是有路由配置的,则添加proxy标识为路由id,对于非http,https以及不是forward的标识serviceId|

##post |filter|type|order|desc| |:---|:---| :----|:----| |SendErrorFilter|post| 0 |当上下文内含error.status_code且没经过本filter时生效,补充上下文内error信息| |SendResponseFilter|post| 1000 |返回内容|

##route |filter|type|order|desc| |:---|:---| :----|:----| |RibbonRoutingFilter|route| 10 |在上下文中没有routeHost且sendZuulResponse为true,而且serviceId值不为空时生效.| |SimpleHostRoutingFilter|route| 100 |在上下文中存在routeHost且sendZuulResponse为true时生效| |SendForwardFilter|route| 500 |存在forward.to以及sendForwardFilter.ran为false时生效|

##PreDecorationFilter填充上下文变量 spring-cloud-netflix-core-1.2.6.RELEASE-sources.jar!/org/springframework/cloud/netflix/zuul/filters/pre/PreDecorationFilter.java

public class PreDecorationFilter extends ZuulFilter {

	public static final int FILTER_ORDER = 5;

	private RouteLocator routeLocator;

	private String dispatcherServletPath;

	private ZuulProperties properties;

	private UrlPathHelper urlPathHelper = new UrlPathHelper();

	private ProxyRequestHelper proxyRequestHelper;

	public PreDecorationFilter(RouteLocator routeLocator, String dispatcherServletPath, ZuulProperties properties,
			ProxyRequestHelper proxyRequestHelper) {
		this.routeLocator = routeLocator;
		this.properties = properties;
		this.urlPathHelper.setRemoveSemicolonContent(properties.isRemoveSemicolonContent());
		this.dispatcherServletPath = dispatcherServletPath;
		this.proxyRequestHelper = proxyRequestHelper;
	}

	@Override
	public int filterOrder() {
		return FILTER_ORDER;
	}

	@Override
	public String filterType() {
		return "pre";
	}

	@Override
	public boolean shouldFilter() {
		RequestContext ctx = RequestContext.getCurrentContext();
		return !ctx.containsKey("forward.to") // a filter has already forwarded
				&& !ctx.containsKey("serviceId"); // a filter has already determined
													// serviceId
	}

	@Override
	public Object run() {
		RequestContext ctx = RequestContext.getCurrentContext();
		final String requestURI = this.urlPathHelper.getPathWithinApplication(ctx.getRequest());
		Route route = this.routeLocator.getMatchingRoute(requestURI);
		if (route != null) {
			String location = route.getLocation();
			if (location != null) {
				ctx.put("requestURI", route.getPath());
				ctx.put("proxy", route.getId());
				if (!route.isCustomSensitiveHeaders()) {
					this.proxyRequestHelper
							.addIgnoredHeaders(this.properties.getSensitiveHeaders().toArray(new String[0]));
				}
				else {
					this.proxyRequestHelper.addIgnoredHeaders(route.getSensitiveHeaders().toArray(new String[0]));
				}

				if (route.getRetryable() != null) {
					ctx.put("retryable", route.getRetryable());
				}

				if (location.startsWith("http:") || location.startsWith("https:")) {
					ctx.setRouteHost(getUrl(location));
					ctx.addOriginResponseHeader("X-Zuul-Service", location);
				}
				else if (location.startsWith("forward:")) {
					ctx.set("forward.to",
							StringUtils.cleanPath(location.substring("forward:".length()) + route.getPath()));
					ctx.setRouteHost(null);
					return null;
				}
				else {
					// set serviceId for use in filters.route.RibbonRequest
					ctx.set("serviceId", location);
					ctx.setRouteHost(null);
					ctx.addOriginResponseHeader("X-Zuul-ServiceId", location);
				}
				if (this.properties.isAddProxyHeaders()) {
					addProxyHeaders(ctx, route);
					String xforwardedfor = ctx.getRequest().getHeader("X-Forwarded-For");
					String remoteAddr = ctx.getRequest().getRemoteAddr();
					if (xforwardedfor == null) {
						xforwardedfor = remoteAddr;
					}
					else if (!xforwardedfor.contains(remoteAddr)) { // Prevent duplicates
						xforwardedfor += ", " + remoteAddr;
					}
					ctx.addZuulRequestHeader("X-Forwarded-For", xforwardedfor);
				}
				if (this.properties.isAddHostHeader()) {
					ctx.addZuulRequestHeader("Host", toHostHeader(ctx.getRequest()));
				}
			}
		}
		else {
			log.warn("No route found for uri: " + requestURI);

			String fallBackUri = requestURI;
			String fallbackPrefix = this.dispatcherServletPath; // default fallback
																// servlet is
																// DispatcherServlet

			if (RequestUtils.isZuulServletRequest()) {
				// remove the Zuul servletPath from the requestUri
				log.debug("zuulServletPath=" + this.properties.getServletPath());
				fallBackUri = fallBackUri.replaceFirst(this.properties.getServletPath(), "");
				log.debug("Replaced Zuul servlet path:" + fallBackUri);
			}
			else {
				// remove the DispatcherServlet servletPath from the requestUri
				log.debug("dispatcherServletPath=" + this.dispatcherServletPath);
				fallBackUri = fallBackUri.replaceFirst(this.dispatcherServletPath, "");
				log.debug("Replaced DispatcherServlet servlet path:" + fallBackUri);
			}
			if (!fallBackUri.startsWith("/")) {
				fallBackUri = "/" + fallBackUri;
			}
			String forwardURI = fallbackPrefix + fallBackUri;
			forwardURI = forwardURI.replaceAll("//", "/");
			ctx.set("forward.to", forwardURI);
		}
		return null;
	}

	private void addProxyHeaders(RequestContext ctx, Route route) {
		HttpServletRequest request = ctx.getRequest();
		String host = toHostHeader(request);
		String port = String.valueOf(request.getServerPort());
		String proto = request.getScheme();
		if (hasHeader(request, "X-Forwarded-Host")) {
			host = request.getHeader("X-Forwarded-Host") + "," + host;
			if (!hasHeader(request, "X-Forwarded-Port")) {
				if (hasHeader(request, "X-Forwarded-Proto")) {
					StringBuilder builder = new StringBuilder();
					for (String previous : StringUtils.commaDelimitedListToStringArray(request.getHeader("X-Forwarded-Proto"))) {
						if (builder.length()>0) {
							builder.append(",");
						}
						builder.append("https".equals(previous) ? "443" : "80");
					}
					builder.append(",").append(port);
					port = builder.toString();
				}
			} else {
				port = request.getHeader("X-Forwarded-Port") + "," + port;
			}
			proto = request.getHeader("X-Forwarded-Proto") + "," + proto;
		}
		ctx.addZuulRequestHeader("X-Forwarded-Host", host);
		ctx.addZuulRequestHeader("X-Forwarded-Port", port);
		ctx.addZuulRequestHeader(ZuulHeaders.X_FORWARDED_PROTO, proto);
		addProxyPrefix(ctx, route);
	}

	private boolean hasHeader(HttpServletRequest request, String name) {
		return StringUtils.hasLength(request.getHeader(name));
	}

	private void addProxyPrefix(RequestContext ctx, Route route) {
		String forwardedPrefix = ctx.getRequest().getHeader("X-Forwarded-Prefix");
		String contextPath = ctx.getRequest().getContextPath();
		String prefix = StringUtils.hasLength(forwardedPrefix) ? forwardedPrefix
				: (StringUtils.hasLength(contextPath) ? contextPath : null);
		if (StringUtils.hasText(route.getPrefix())) {
			StringBuilder newPrefixBuilder = new StringBuilder();
			if (prefix != null) {
				if (prefix.endsWith("/") && route.getPrefix().startsWith("/")) {
					newPrefixBuilder.append(prefix, 0, prefix.length() - 1);
				}
				else {
					newPrefixBuilder.append(prefix);
				}
			}
			newPrefixBuilder.append(route.getPrefix());
			prefix = newPrefixBuilder.toString();
		}
		if (prefix != null) {
			ctx.addZuulRequestHeader("X-Forwarded-Prefix", prefix);
		}
	}

	private String toHostHeader(HttpServletRequest request) {
		int port = request.getServerPort();
		if ((port == 80 && "http".equals(request.getScheme()))
				|| (port == 443 && "https".equals(request.getScheme()))) {
			return request.getServerName();
		}
		else {
			return request.getServerName() + ":" + port;
		}
	}

	private URL getUrl(String target) {
		try {
			return new URL(target);
		}
		catch (MalformedURLException ex) {
			throw new IllegalStateException("Target URL is malformed", ex);
		}
	}
}

##doc


想获取最新资讯,请关注微信公众号

输入图片说明

© 著作权归作者所有

共有 人打赏支持
go4it
粉丝 64
博文 741
码字总数 535026
作品 0
深圳
深入理解Zuul之源码解析

转载请标明出处: http://blog.csdn.net/forezp/article/details/76211680 本文出自方志朋的博客 Zuul 架构图 在zuul中, 整个请求的过程是这样的,首先将请求给zuulservlet处理,zuulservl...

forezp
2017/07/27
0
0
springCloud(15):使用Zuul构建微服务网关-Header与文件上传和过滤器

一、Header 1.1、敏感header的设置 一般来说,可在同一个系统中的服务之间共享Header,不过应尽量防止让一些敏感的Header外泄。 zuul: routes: provide-user: sensitive-headers: Cookie,Se...

我爱大金子
2017/08/23
0
0
基于docker部署的微服务架构(三): 服务网关

前言 在微服务架构中,后端服务往往不直接开放给调用端,而是通过一个公共网关根据请求的url,路由到相应的服务。在网关中可以做一些服务调用的前置处理,比如权限验证。也可以通过动态路由,...

月冷X心寒
2016/11/18
709
0
微服务架构网关接口设计

近年来微服务架构盛行,我司也利用spring cloud实践微服务。使用的也是spring cloud很经典的架构,zuul充当网关路由,configserver作为静态配置中心,feign作为远程调用http client 等,反正...

数齐
01/01
0
0
【微服务】之七:轻松搞定SpringCloud微服务-API权限控制

【微服务】之七:轻松搞定SpringCloud微服务-API权限控制 Harries Blog™2017-12-210 阅读 httpSpringAppcatbeanAPIhttps 【微服务】轻松搞定SpringCloud微服务 目录 本系列为连载 文章 ,阅...

Harries Blog™
2017/12/21
0
0

没有更多内容

加载失败,请刷新页面

加载更多

DES/3DES(TripleDES)加密、解密测试数据

以下结果经 PHP+openssl及VB.NET验证,ECB模式。 PHP 7.0.10 (cli) (built: Aug 18 2016 09:48:53) ( ZTS ) OpenSSL Library Version: OpenSSL 1.0.1t 3 May 2016 VB.net 2003 ****** DES(S......

SamXIAO
35分钟前
1
3
Java11的新特性

Java语言特性系列 Java5的新特性 Java6的新特性 Java7的新特性 Java8的新特性 Java9的新特性 Java10的新特性 Java11的新特性 Java12的新特性 序 本文主要讲述一下Java11的新特性 版本号 java...

go4it
36分钟前
3
0
Maven常用命令及相关笔记

Maven常用命令 dos指令 4. 编译源代码: mvn compile 6. 运行测试: mvn test 8. 打包: mvn package 9. 在本地Repository中安装jar: mvn install 10. 清除产生的项目: mvn clean 4. 运行项...

颖伙虫
43分钟前
1
0
swagger2.2.2 与 spring cloud feign冲突 导致服务请求报空

swagger2.2.2 与 spring cloud feign冲突 Java代码 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.choosefine.web.console.ar......

泉天下
46分钟前
1
0
设计模式之 明确责任 观察者模式 状态模式 责任链模式

观察者模式是任务分发的一种模式。 如果认为我们设计的系统的各个模块(或子系统)的最终目的是完成共同任务,那么这个任务如何分配到多个模块的就是我们遇到的第一个问题。简单设计场合我们...

backbye
51分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部