springcloud(二):zuul网关过滤

zuul的过滤我们需要继承zuul的一个抽象类  ZuulFilter。

继承了ZuulFilter这个抽象类之后,我们需要实现它的4个抽象方法:

filterType: filter的类型,分为pre,route,post,error。pre是在路由之前执行,route是在路由时候执行,post是在路由完成之后执行,error,是在发生错误时候执行。

filterOrder:根据filterType类型,再根据filterOrder的值正序执行。

shouldFilter:返回布尔值,判断此过滤器执行不执行,可以做一些逻辑判断,如果前面的filter不想让后面的filter执行,可以在这边做判断。

run:执行的具体逻辑。

接下来简单写一些过滤的逻辑和方法:

接之前的zuul写一些filter。

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class Filter1 extends ZuulFilter {


    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

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

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("filter1 is running");
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request =  requestContext.getRequest();
        String header1 = request.getHeader("headerParam");
        if (header1 == null) {
            // 设置SendZuulResponse,如果是false,则不会向路由的后端服务发送
            requestContext.setSendZuulResponse(false);
            requestContext.set("flag", false);
            // 直接返回resp结果
            requestContext.setResponseBody("headerParam is not exist");
            return null;
        }
        requestContext.set("flag", true);
        return null;
    }
}



import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

@Component
public class Filter2 extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

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

    @Override
    public boolean shouldFilter() {
        return (boolean) RequestContext.getCurrentContext().get("flag");
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("filter2 is running");
        RequestContext requestContext = RequestContext.getCurrentContext();
        // 增加header
        requestContext.addZuulRequestHeader("zuulHeader", "zuulHeader");
        return null;
    }
}


import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

@Component
public class PostFilter1 extends ZuulFilter {
    @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 {
        System.out.println("postFilter1 is running");
        return null;
    }
}

以上就是 通过zuul的一些功能实现简单的过滤,还有设置不向后路由,以及直接返回等。当然zuul的功能还有很多,后续再好好完善!

你可能感兴趣的:(springcloud,zuul,java)