zoukankan      html  css  js  c++  java
  • Zuul路由转发规则

    定制的路由规则的主要功能:

     1、路由表中包含源路径,微服务名称,目标路径

     2、Endpoint粒度配置支持

     3、路由支持1对1精确路由

     4、源路径可以前缀/**格式来模糊路由

     5、目标路径可以使用前缀/**格式来装配目标路径

     6、保留默认动态路由规则:服务名称/** --> 是否截去前缀 --> 目标路径

     7、保留默认动态路由规则是否支持截去前缀的配置参数stripPrefix特性

     8、路由规则可以在不重启服务动态更新,这个功能通过外化配置来支持

     9、匹配股则采取谁先匹配路由谁,也就是说在路由表中有2个或以上的路由规则可能被匹配到时,匹配最先查询到的规则

    路由规则格式采用properties格式:

    源路径 = 微服务名称, 目标路径

    启动时读取配置并解析,放入路由表。请求时通过查询匹配到合适的路由转发。

    例如:

    /api/v1/trade=trade,/v1/trade
    /api/customer/**=customer,/api/v1/**
    /api/user/**=user

    在上面的例子中:

    • /api/v1/trade会精确的路由到trade微服务的/v1/trade;
    • /api/customer/开头的api会路由转发到customer微服务的/api/v1/**,其中后面的**会被前面的**部分替换,比如/api/customer/card->/api/v1/card的转换
    • /api/user/开头的api会路由转发到user微服务的/api/user/**,endppoint不变

    一、直观显示路径到微服务的映射

    #localhost:8888/routes(Zuul对应的IP及端口)
    management:
      security:
        enabled: false
    

      

     二、路由配置

        1、静态路由

    #除了“users”服务,其他的服务都会被忽略
    zuul:
      ignoredServices: '*'
      routes:
        users: /myusers/**
    
    #前端通过/myusers的http访问,将会被后端“users”服务处理(例如:/myusers/101将会转发的/101)
    zuul:
      routes:
        users: /myusers/**
    
    #将xxx/books后面的所有请求,添加到url后面去
    示例:http://localhost:8888/books/xxx->http://localhost:5000/books/avaiable/xxx
    zuul:
      routes:
        books: http://localhost:5000/books/available
    
    #books/xxx=>转化为http://localhost:5000/books/available/xxx
    示例:http://localhost:8888/books/xxx==>http://localhost:50000/books/available/xxx
    server:
      port: 8888
    spring:
      application:
        name: zuul-gateway
    zuul:
      routes:
        books:
          url: http://localhost:5000/books/available
    
    #/baidu后的所有直接添加到http://localhost:8080后
    示例:http://localhost:8888/baidu/**=>http://localhost:8080/**
    zuul:
      routes:
        baidu:
          path: /baidu/**
          url: http://localhost:8080
    

     2、静态路由+ribbon负载均衡/故障切换

    zuul:
      routes:
        myroutes1:
          path: /mypath/**
          serviceId: myserverId
    myserverId:
      ribbon:
        listOfServers: localhost:8080, localhost:8081
    ribbon:
      eureka:
        enabled: false
    

     3、动态路由+ribbon负载均衡/故障切换

    zuul:
      routes:
        myroutes1:
          path: /mypath/**
          serviceId: myserviceId
    eureka:
      client:
        serviceUrl:
          defaultZne:xxx
    

     4、路由匹配配置

    stripPrefix=true,转发会过滤掉前缀
    path: /myusers/**,默认时转发到服务的请求是/**,如果stripPrefix=false,转发的请求是/myusers/**
    zuul.prefix=/api	会对所有的path增加一个/api前缀
    ignoredPatterns: /**/admin/**	过滤掉匹配的url
    route:
      users: /myusers/**	
      会匹配所有/myusers/**的url,但由于ignoredPatterns, /myusers/**/admin/**的请求不会被转发,而是直接由zuul里的接口接收
    

    5、匹配顺序

    path:/myusers/**
    path:/**	
    如果是在application.yml中配置的,那么会优先匹配/myusers/**
    但如果是applicaiton.properties配置的,那么可能导致/myusers/**被/**覆盖
    ignored-Services: ‘*‘	对于自动发现的services,除了route中明确指定的,其他都会被忽略
    

    6、请求头过滤

    route.sensitiveHeaders: Cookie,Set-Cookie,Authorization	
    默认就有这三个请求头,意思是不向下游转发请求这几个头
    zuul.ignoredHeaders 是一个全局设置,而route.sensitiveHeaders是局部设置
    

    参见:http://1754966750.blog.51cto.com/7455444/1958422

  • 相关阅读:
    mysql 游标使用
    springboot 集成swagger2
    springboot + thymeleaf静态资源访问404
    Win10系列:VC++绘制几何图形1
    Win10系列:VC++媒体播放控制4
    Win10系列:VC++媒体播放控制3
    Win10系列:VC++媒体播放控制2
    Win10系列:VC++媒体播放控制1
    Win10系列:VC++媒体播放
    Win10系列:VC++文件选取
  • 原文地址:https://www.cnblogs.com/moonandstar08/p/7523305.html
Copyright © 2011-2022 走看看