zuul网关

1、先创建一个springboot项目

2、添加zuul的依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>

3、在启动类上添加网关的注解

@EnableZuulProxy
public class GatewayApplication {

4、在主配置文件中配置网关的路由规则:

    #实现路由规则
    zuul.routes.book.url=http://127.0.0.1:8070
    zuul.routes.sale.url=http://127.0.0.1:8090
        ps:book、sale是其它微服务的名称
            spring.application.name=book
            spring.application.name=sale

    #网关的访问端口号
    server.port=8000
    ps:
        #127.0.0.1:8080 相当于访问网关
        #127.0.0.1:8080/book 相当于访问book微服务

5、创建过滤器,拦截请求

 一:创建前置过滤器:pre

    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.stereotype.Component;

    import javax.servlet.http.HttpServletRequest;

    /**
     * 自定义过滤器
     * 1、继承ZuulFilter
     * 2、重写4个方法
     * 3、添加@Component注解
     */
    @Component
    public class PreFilter extends ZuulFilter {

        Logger log = LoggerFactory.getLogger(PreFilter.class);

        /**
         * filterType:过滤器的类型(pre,post,routing,error)
         * pre:前置过滤器
         */
        @Override
        public String filterType() {
            return "pre";
        }

        /**
         * filterOrder:过滤器的执行顺序。整数,数值越小,优先级越高
         */
        @Override
        public int filterOrder() {
            return 1;
        }

        /**
         * shouldFilter:是否开启过滤器
         * true:开启 false:关闭
         */
        @Override
        public boolean shouldFilter() {
            return true;
        }

        /**
         * 自定义过滤器的业务逻辑
         */
        @Override
        public Object run() throws ZuulException {
            //--获取请求信息--
            //获取全局对象RequestContext,从该对象中获取请求信息
            RequestContext context = RequestContext.getCurrentContext();
            //获取HttpServletRequest对象
            HttpServletRequest request = context.getRequest();

            //记录日志
            log.info(String.format("请求的uri地址:%s----请求方式:%s", request.getRequestURI(), request.getMethod()));

            // 使用pre过滤器验证请求的数据,如果验证失败则不进行路由
            String token = request.getParameter("token");
            if(token == null){
                //请求到这里被截断
                context.setSendZuulResponse(false);
                context.setResponseStatusCode(500);
            }

            return null;
        }
    }


    二:创建后置过滤器:post

    import com.netflix.zuul.ZuulFilter;
    import com.netflix.zuul.exception.ZuulException;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;

    //创建后置过滤器:post
    @Component
    public class PostFilter extends ZuulFilter {

        Logger log = LoggerFactory.getLogger(PostFilter.class);

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

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

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

        @Override
        public Object run() throws ZuulException {
            log.info("=========PostFilter=========");
            return null;
        }
    }

    三:创建路由过滤器:route

    import com.netflix.zuul.ZuulFilter;
    import com.netflix.zuul.exception.ZuulException;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;

    //创建路由过滤器:route
    //执行时间:在请求发送给controller之前
    @Component
    public class RouteFilter extends ZuulFilter {

        Logger log = LoggerFactory.getLogger(RouteFilter.class);

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

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

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

        @Override
        public Object run() throws ZuulException {
            log.info("=======routeFilter=====");
            return null;
        }
    }

    四:创建error过滤器

    import com.netflix.zuul.ZuulFilter;
    import com.netflix.zuul.exception.ZuulException;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;

    //创建error过滤器:当其他过滤器发生异常时,执行该过滤器
    @Component
    public class ErrorFilter extends ZuulFilter {

        Logger log = LoggerFactory.getLogger(ErrorFilter.class);

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

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

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

        @Override
        public Object run() throws ZuulException {
            log.info("==========error==========");
            return null;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值