zuul中的Filter的配置,zuul中提供了三种类型的Filter,preFilter,routeFilter和postFilter,分别对应请求中的不同的阶段,针对同一个请求,有一个RequestContext对象,在三个阶段的Filter中进行共享
假设我们要开发一个统计请求时间的功能,需要在preFilter里边记录开始时间,并将整个开始时间放在RequestContext中,在postFilter里边拿到开始时间,用当前的时间减去开始时间,就是请求执行的时间
定义一个preFilter:
package com.jiaoyiping.springcloud.zuul.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
/**
* Created with Intellij IDEA
*
* @author:
* Mail:
* Date:
* Time:
* To change this template use File | Settings | Editor | File and Code Templates
*/
public class TimeCostPreFilter extends ZuulFilter {
public static final String START_TIME_KEY = "start_time";
private Logger logger = LoggerFactory.getLogger(TimeCostPreFilter.class);
@Override
public String filterType() {
return FilterConstants.PRE_TYPE;
}
@Override
public int filterOrder() {
return 0;
}
/**
* 判断是否要拦截的逻辑
*
* @return
*/
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
long startTime = System.currentTimeMillis();
RequestContext.getCurrentContext().set(START_TIME_KEY, startTime);
return null;
}
}
定义以postFilter:
package com.jiaoyiping.springcloud.zuul.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
/**
* Created with Intellij IDEA
*
* @author:
* Mail:
* Date:
* Time:
* To change this template use File | Settings | Editor | File and Code Templates
*/
public class TimeCostPostFilter extends ZuulFilter {
private static final String START_TIME_KE = "start_time";
private Logger logger = LoggerFactory.getLogger(TimeCostPostFilter.class);
@Override
public String filterType() {
return FilterConstants.POST_TYPE;
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
long startTime = (long) RequestContext.getCurrentContext().get(START_TIME_KE);
logger.info("请求完成,耗时{}秒", (System.currentTimeMillis() - startTime) / 1000);
return null;
}
}
在一个配置类中将这两个Filter注入:
package com.jiaoyiping.springcloud.zuul.config;
import com.jiaoyiping.springcloud.zuul.filter.PDSFilter;
import com.jiaoyiping.springcloud.zuul.filter.TimeCostPostFilter;
import com.jiaoyiping.springcloud.zuul.filter.TimeCostPreFilter;
import com.netflix.zuul.ZuulFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created with Intellij IDEA
*
* @author:
* Mail:
* Date:
* Time:
* To change this template use File | Settings | Editor | File and Code Templates
*/
@Configuration
public class FilterConfig {
@Bean
public ZuulFilter timeCostPreFilter() {
return new TimeCostPreFilter();
}
@Bean
public ZuulFilter timeCostPostFilter() {
return new TimeCostPostFilter();
}
@Bean
public ZuulFilter pdsFilter() {
return new PDSFilter();
}
}
启动项目,可以发现,zuul网关已经注册到了eureka上:
请求provide对应的地址,发现,zuul可以成功地调用eureka上对应的服务,并将结果正确返回: