shouldFilter(): shouldFilter()方法检查请求并确定是否要执行过滤器。
run(): 如果!isFilterDisabled()和 shouldFilter()方法都返回,则run()方法将调用是。
filterType(): filterType()方法按类型对过滤器进行分类。 Zuul中有四种类型的标准过滤器: pre 用于路由前过滤,路由用于路由到原点,发布(用于路由后过滤器)和错误(用于错误处理)。 Zuul还支持静态响应的静态类型。可以通过调用方法 runFilters(type)来创建或添加和运行任何过滤器类型。
filterOrder(): 必须为过滤器定义过滤器顺序。如果优先级对于过滤器不重要,则过滤器可能具有相同的过滤器顺序。过滤器顺序不必是顺序的。
private Logger logger=LoggerFactory.getLogger(this.getClass());
public Object run() throws ZuulException { //getting the current HTTP request that is to be handle HttpServletRequest request=RequestContext.getCurrentContext().getRequest(); //printing the detail of the request logger.info("request -> {} request uri-> {}", request, request.getRequestURI()); return null; }
package com.lidihuo.microservices.netflixzuulapigatewayserver; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; @Component public class ZuulLoggingFilter extends ZuulFilter { //creating Logger object private Logger logger=LoggerFactory.getLogger(this.getClass()); @Override public booleanshouldFilter() { return true; //executing filter for every request } //log the content of the request @Override public Object run() throws ZuulException { //getting the current HTTP request that is to be handle HttpServletRequest request=RequestContext.getCurrentContext().getRequest(); //prints the detail of the requestin the log logger.info("request -> {} request uri-> {}", request, request.getRequestURI()); return null; } @Override public String filterType() { return "pre"; //intercept all the request before execution } @Override public intfilterOrder() { return 1; //setting filter order to 1 } }