Spring Cloud之Zuul(三):路由配置详解

版权声明:本文为博主原创文章,可自由转载、引用,但需注明文章出处。 https://blog.csdn.net/StarskyBoy/article/details/85012075

主题

路由配置详解

前言

现实中可能只想让Zuul代理部分微服务,又或者需要对URL进行更加精确的控制。Zuul的路由配置非常灵活、简单,本博客通过几个实例,详细讲解Zuul的路由配置。

内容

1.自定义指定微服务的访问路径

说明:配置zuul.routes.指定微服务的serverId = 指定路径即可。例如:

作用:cloud-register-user微服务会被映射到:/user/**路径。

zuul:
 routes: 
   cloud-register-user: /user/**

2.忽略指定微服务

说明:使用zuul.ignored-services配置需要忽略的服务,多个用逗号分隔。

作用:Zuul忽略cloud-register-user和cloud-register-consumer-ribbon微服务,只代理其它微服务。

zuul:
 ignored-services: cloud-register-user,cloud-register-consumer-ribbon

3.忽略所有微服务,只路由指定微服务

说明:将zuul.ignored-services设为'*',routes配置指定的微服务。

作用:忽略所有微服务,只代理cloud-register-user微服务。

zuul:
 ignored-services: '*'
 routes:
   cloud-register-user: /user/**

4.同时指定微服务的serviceId和对应的路径

作用:效果与例1一致。

zuul:
 routes:
   user-route: user  # user-route只是给路由一个名称,可以随便命名
     service-id: cloud-register-user
     path: /user/**  # service-id对应的路径

5.同时指定path和URL

说明:这样就可以将/user/**映射到http://localhost:8080/**,需要注意的是改该方式不会作为HystrixCommand执行,同时不支持Ribbon负载均衡。

作用:当访问http://localhost:8023/user/1时,则会转发至http://localhost:8080/1

zuul:
 routes:
   user-route: user # user-route只是给路由一个名称,可以随便命名
    url: http://localhost:8080/ #指定URL
    path: /user/**   # URL对应的路径

6.同时指定path和URL,并且不破坏Hystrix、Ribbon特性

说明:这样就可以指定path和URL,并且不破坏Hystrix、Ribbon特性

zuul:
 routes:
   user-route:
    path: /user/**
    service-id: cloud-register-user
ribbon:
 eureka:
   enabled: false  # 为Ribbon禁用Eureka
cloud-register-user: # 这边是serviceId
 ribbon:
   listOfServers: http://localhost:8080,http://localhost:8081

7.使用正则表达式指定Zuul的路由匹配规则

说明:借助PatternServiceRouteMapper,实现从微服务到映射路由的正则配置。

作用:下面将如cloud-register-user-v2这个微服务,映射到/v1/cloud-register-user/**这个路径上

如:对微服务的serviceId命名为cloud-register-user-v1,那么我们可以这么来访问http://localhost:8023/v1/cloud-register-user/1

8.路由前缀

全局时:

说明:设置 zuul.prefix 可以为所有的匹配增加前缀, 例如 /api,代理前缀默认会从请求路径中移除(通过zuul.stripPrefix=false可以关闭这个功能),zuul.stripPrefix默认为true。

作用:当strip-prefix=true的时候 (http://localhost:8023/api/cloud-register-user

/1 -> http://localhost:8002/1)  

当strip-prefix=false的时候(http://localhost:5016/api/cloud-register-user
/1 -> http://localhost:8002/api/1) 

局部时:

说明:下面面为true的配置,当访问zuul的/user/1路径,请求将会被转发到cloud-register-user微服务的/1路径

作用:当strip-prefix=true的时候 (http://localhost:8023/user/1-> http://localhost:8002/1)  

当strip-prefix=false的时候(http://localhost:8023/user/1 -> http://localhost:8002/user/1)

如:配置全局的,与prefix一起使用

zuul:
  prefix: /api
  strip-prefix: true



如:配置局部的,与path一起使用

zuul:
  routes:
    provide-user: 
      path: /user/**
      strip-prefix: false

9.忽略某些路径

说明:2中有讲有忽略微服务,但是有时需要更细粒度的路由控制。如,想让Zuul代理某个微服务,同时又想保护该微服务的某些敏感路径。此时,可使用ignored-patterns指定忽略的正则

作用:这样就可以将cloud-register-user微服务映射到/user/**路径,但会忽略该微服务中所有包含/admin的路径。

zuul:
 ignored-patterns: /**/admin/**  # 忽略所有包含/admin/的路径
 routes:
   cloud-register-user: /user/**

10.本地转发

作用:但访问Zuul的 /path-a/**路径,将转发到Zuul的path-b/**,其它遗留路径将转发到 path-c/**

zuul:
 routes:
   route-name:
     path: /path-a/**
     url: forward:/path-b
   legacy:
     path: /**
     url: path-c

11.other

如果无法掌握Zuul路由规则,可配如下日志

logging:
 level: 
   com.netflix: DEBUG

源码获取

1.gitee:https://gitee.com/StarskyBoy/cloud

2.github: https://github.com/StarskyBoy/cloud

获取更多信息,请扫我

猜你喜欢

转载自blog.csdn.net/StarskyBoy/article/details/85012075
今日推荐