0 课程地址
https://coding.imooc.com/lesson/380.html#mid=28557
1 浓缩精华
1.1 4.1
2 个人关注
2.1 4.1
过滤器传参取参的方法
过滤器order最大值为999
3 课程内容
3.1
4 代码演练
4.1 自定义访问日志过滤器(计算所有过滤器执行时间)
PreRequestFilter (起始过滤器最先执行的)
package com.imooc.coupon.filter; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; /** * PreRequestFilter * 自定义日志访问过滤器,统计日志执行时间,放在最前边 * @author 魏豆豆 * @date 2021/6/10 */ @Slf4j @Component public class PreRequestFilter extends AbstractPreZuulFilter{ @Override public Object continueRun() { context.set("startTime", System.currentTimeMillis()); log.info("开始时间为:"+ System.currentTimeMillis()); return null; } @Override public int filterOrder() { return 0; } }
AccessLogFilter (末尾过滤器最后执行的)
package com.imooc.coupon.filter; import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants; import org.springframework.stereotype.Component; /** * AccessLogFilter * * @author 魏豆豆 * @date 2021/6/10 */ @Slf4j @Component public class AccessLogFilter extends AbstractPostZullFilter { @Override public Object continueRun() { long startTime = (long)context.get("startTime"); long spentTime = System.currentTimeMillis()-startTime; log.info("花费时间"+spentTime); return null; } @Override public int filterOrder() { //第1000条将不再执行 return FilterConstants.SEND_RESPONSE_FILTER_ORDER-1; } }
FilterConstants(引用的常量)
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.cloud.netflix.zuul.filters.support; public class FilterConstants { public static final String IS_DISPATCHER_SERVLET_REQUEST_KEY = "isDispatcherServletRequest"; public static final String FORWARD_TO_KEY = "forward.to"; public static final String PROXY_KEY = "proxy"; public static final String REQUEST_ENTITY_KEY = "requestEntity"; public static final String REQUEST_URI_KEY = "requestURI"; public static final String RETRYABLE_KEY = "retryable"; public static final String ROUTING_DEBUG_KEY = "routingDebug"; public static final String SERVICE_ID_KEY = "serviceId"; public static final String LOAD_BALANCER_KEY = "loadBalancerKey"; public static final int DEBUG_FILTER_ORDER = 1; public static final int FORM_BODY_WRAPPER_FILTER_ORDER = -1; public static final int PRE_DECORATION_FILTER_ORDER = 5; public static final int RIBBON_ROUTING_FILTER_ORDER = 10; public static final int SEND_ERROR_FILTER_ORDER = 0; public static final int SEND_FORWARD_FILTER_ORDER = 500; public static final int SEND_RESPONSE_FILTER_ORDER = 1000; public static final int SIMPLE_HOST_ROUTING_FILTER_ORDER = 100; public static final int SERVLET_30_WRAPPER_FILTER_ORDER = -2; public static final int SERVLET_DETECTION_FILTER_ORDER = -3; public static final String ERROR_TYPE = "error"; public static final String POST_TYPE = "post"; public static final String PRE_TYPE = "pre"; public static final String ROUTE_TYPE = "route"; public static final String FORWARD_LOCATION_PREFIX = "forward:"; public static final int HTTP_PORT = 80; public static final int HTTPS_PORT = 443; public static final String HTTP_SCHEME = "http"; public static final String HTTPS_SCHEME = "https"; public static final String SERVICE_HEADER = "X-Zuul-Service"; public static final String SERVICE_ID_HEADER = "X-Zuul-ServiceId"; public static final String X_FORWARDED_FOR_HEADER = "X-Forwarded-For"; public static final String X_FORWARDED_HOST_HEADER = "X-Forwarded-Host"; public static final String X_FORWARDED_PREFIX_HEADER = "X-Forwarded-Prefix"; public static final String X_FORWARDED_PORT_HEADER = "X-Forwarded-Port"; public static final String X_FORWARDED_PROTO_HEADER = "X-Forwarded-Proto"; public static final String X_ZUUL_DEBUG_HEADER = "X-Zuul-Debug-Header"; private FilterConstants() { throw new AssertionError("Must not instantiate constant utility class"); } }
诸葛