第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

SpringCloud系列——Zuul 動(dòng)態(tài)路由

標(biāo)簽:
Java

 前言

  Zuul 是在Spring Cloud Netflix平台上提供动态路由,监控,弹性,安全等边缘服务的框架,是Netflix基于jvm的路由器和服务器端负载均衡器,相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门。本文基于上篇(SpringCloud系列——Ribbon 负载均衡)实现Zuul动态路由

  GitHub地址:https://github.com/Netflix/zuul

  官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.1.0.RC2/single/spring-cloud-netflix.html#_router_and_filter_zuul

 

  代码编写

  首先我们在springCloud下面新建一个springboot项目:zuul-server,pom继承parent,并且在Eureka上面注册(还不会服务注册与发现的,请戳:SpringCloud系列——Eureka 服务注册与发现

   maven引入Zuul

        <!-- Zuul -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

  配置文件

复制代码

server.port=10010spring.application.name=zuul-server
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/#健康检查(需要spring-boot-starter-actuator依赖)eureka.client.healthcheck.enabled=true# 续约更新时间间隔(默认30秒)eureka.instance.lease-renewal-interval-in-seconds=10# 续约到期时间(默认90秒)eureka.instance.lease-expiration-duration-in-seconds=10#zuul代理配置  zuul.routes.服务名.path,服务名要与注册的一致
#应用名映射zuul.routes.myspringboot.path=/myspringboot/**zuul.routes.myspringboot.service-id=myspringboot#URL映射
#zuul.routes.myspringboot.path=/myspringboot/**
#zuul.routes.myspringboot-url.url=http://localhost:10087/

复制代码

  自定义Zuul过滤器

  更多的检查规则后续慢慢健全

复制代码

/**
 * Zuul过滤器,实现了路由检查 */public class AccessFilter extends ZuulFilter {    /**
     * 通过int值来定义过滤器的执行顺序     */
    @Override    public int filterOrder() {        // PreDecoration之前运行
        return PRE_DECORATION_FILTER_ORDER - 1;
    }    /**
     * 过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型:
     *     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";     */
    @Override    public String filterType() {        return PRE_TYPE;
    }    /**
     * 过滤器的具体逻辑     */
    @Override    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        System.out.println(String.format("%s AccessFilter request to %s", request.getMethod(),request.getRequestURL().toString()));
        String accessToken = request.getParameter("accessToken");        //有权限令牌
        if (!StringUtils.isEmpty(accessToken)) {
            ctx.setSendZuulResponse(true);
            ctx.setResponseStatusCode(200);            //可以设置一些值
            ctx.set("isSuccess", true);            return null;
        } else {
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            ctx.setResponseBody("{\"result\":\"accessToken is not correct!\"}");            //可以设置一些值
            ctx.set("isSuccess", false);            return null;
        }
    }    /**
     * 返回一个boolean类型来判断该过滤器是否要执行     */
    @Override    public boolean shouldFilter() {        return true;
    }
}

复制代码

  启动类

  添加@EnableZuulProxy注解并使用自定义过滤器

复制代码

@EnableZuulProxy
@SpringBootApplicationpublic class ZuulServerApplication {    public static void main(String[] args) {
        SpringApplication.run(ZuulServerApplication.class, args);
    }

    @Bean    public AccessFilter accessFilter() {        return new AccessFilter();
    }
}

复制代码

 

   效果演示

  启动所有项目,我们在Eureka上注册了四个服务,相比上篇(SpringCloud系列——Ribbon 负载均衡)多了一个Zuul

 

  浏览器访问 http://localhost:10010/myspringboot/feign/ribbon、http://localhost:10010/myspringboot/feign/ribbon?accessToken=123456

  http://localhost:10010/ 这个端口对外暴露,相对于总入口,后面接不同的路径由,Zuul路由到对应的服务上

  1、没有accessToken是,无法通过检查

  2、携带accessToken时,可正常路由,并且Feign调用、Ribbon负载均衡

 

  后记

  我们为什么要使用Zuul呢?

  1、请求校验、路由转发,接口校验与业务逻辑分离

  2、隐藏诸多服务路径,只暴露统一入口,安全

  更多Zuul配置,请看官方文档

作者:huanzi-qch

出处:https://www.cnblogs.com/huanzi-qch/p/10142395.html  

若标题中有“转载”字样,则本文版权归原作者所有。若无转载字样,本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消