『互联网架构』软件架构-zuul微服务网关(中)(101)

咱们今天继续说springcloud的zuul。在最早我们是没有网关这个概念的,微服务搭建起来后,客户端就直接访问一个个微服务了,这些方式有很多的弊端,上次都说了,针对这些弊端,我们用一种什么样的方式去解决呢,那就是springcloud为我们整合一个框架zuul统一的微服务,在这些所有的后端的微服务中间加了一层网关层,网关层类似于设计模式里面的门面模式,就是靠这种门面进行隔离让后端的和请求端进行隔离解耦,互相之前不进行相互的影响,他们之前的影响都通过门面来进行解决。切记:网关调用后端的微服务,全部使用无状态请求。
源码:https://github.com/limingios/netFuture/tree/master/源码/『互联网架构』软件架构-zuul微服务网关(中)(101)/

(一)zuul聚合微服务

许多场景下,外部请求需要查询 zuul后端的多个微服务。举个例子,一个电影售票手机APP,在购票订单页上,既需要查询“电影微服务”获得电影相关信息,又需要查询“用户微服务”获得当前用户的信息。如果让手机端直接请求各个微服务(即使使用 zuul进行转发),那么网络开销、流量耗费、耗费时长可能都无法令我们满意。那么对于这种场景,可使用 zuul聚合微服务请求一一一手机 APP只需发送一个请求给 zuul,由 zuul请求用户微服务以及电影微服务,并组织好数据给手机 APP,使用这种方式,手机端只须发送一次请求即可,简化了客户端侧的开发;不仅如此,由于 zuul、用户微服务、电影微服务一般都在同一个局域网中,因此速度会非常快,效率会非常高。

  • 实例代码
    > 08-ms-gateway-zuul-aggregation

  • 运行项目(需启动两个用户微服务和一个订单微服务,eureka-server,zuul的项目
    1.08-ms-consumer-order-ribbon
    2.08-ms-eureka-server
    3.08-ms-gateway-zuul-aggregation
    4.08-ms-provider-user

eureka里面有3个服务

  • 聚合请求
    > 其实就是访问一个zuul的一个controller,通过zuul中的controller来选择性的请求多个微服务。http://127.0.0.1:8040/aggregate/1
    一个请求order的微服务,一个请求user的微服务。

(二)zuul的路由配置

前文已经编写了一个简单的 zuul网关,并让该网关代理了所有注册到 Eureka server的微服务。但在现实中可能只想让 zuul代理部分微服务,又或者需要对 URL进行更加精确的控制。

  • 源码

    08-ms-gateway-zuul

  • 路由忽略微服务

    配置忽略指定微服务,只需在application.yml里加上如下配置

zuul:
  ignored-services: microservice-provider-user

  • 其他路由配置课查看项目示例的配置文件
    > 指定微服务地址routes, user = microservice-provider-user
server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  routes:
    microservice-provider-user: /user/**
management:
  security:
    enabled: false

忽略指定微服务ignored-services

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  ignored-services: microservice-provider-user
management:
  security:
    enabled: false

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

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  ignored-services: '*'   # 使用'*'可忽略所有微服务
  routes:
    microservice-provider-user: /user/**
management:
  security:
    enabled: false

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

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  routes:
    user-route:                   # 该配置方式中,user-route只是给路由一个名称,可以任意起名。
      service-id: microservice-provider-user
      path: /user/**              # service-id对应的路径
management:
  security:
    enabled: false

同时指定path和url

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  routes:
    user-route:                   # 该配置方式中,user-route只是给路由一个名称,可以任意起名。
      url: http://localhost:8000/ # 指定的url
      path: /user/**              # url对应的路径。  这样就可以将/user/**映射到http://localhost:8000/**,这种方式访问不会作为HystrixCommand执行,也不能使用ribbon来负载多个URL,例6可以解决该问题
management:
  security:
    enabled: false

强烈建议使用下面的配置,同时指定path和URL,并且不破坏Zuul的Hystrix、Ribbon特性。

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  routes:
    user-route:
      path: /user/**
      service-id: microservice-provider-user
ribbon:
  eureka:
    enabled: false    # 禁用掉ribbon的eureka使用
microservice-provider-user:
  ribbon:
    listOfServers: localhost:8000,localhost:8001
management:
  security:
    enabled: false

为Zuul添加映射前缀1

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  prefix: /api
  strip-prefix: false
  routes:
    microservice-provider-user: /user/**
logging:
  level:
    com.netflix: DEBUG
management:
  security:
    enabled: false

# 访问Zuul的/api/microservice-provider-user/1路径,请求将会被转发到microservice-provider-user的/api/1,可查看日志打印,有助于理解。

为Zuul添加映射前缀2

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  routes:
    microservice-provider-user: 
      path: /user/**
      strip-prefix: false
logging:
  level:
    com.netflix: DEBUG

management:
  security:
    enabled: false


# 这样访问Zuul的/user/1路径,请求将会被转发到microservice-provider-user的/user/1,可查看日志打印,有助于理解。

忽略某些路径

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  ignoredPatterns: /**/admin/**   # 忽略所有包括/admin/的路径
  routes:
    microservice-provider-user: /user/**
management:
  security:
    enabled: false

忽略某些路径

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  routes:
    route-name:
      path: /path-a/**
      url: forward:/path-b
management:
  security:
    enabled: false

忽略某些路径

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  routes:
    route-name:
      path: /path-a/**
      url: forward:/path-b
management:
  security:
    enabled: false

其实上边的例子:https://cloud.spring.io/spring-cloud-static/Edgware.SR6/single/spring-cloud.html 都是从官网拆出来的,要看一手文档。其实我就是搬运工,加上了自己的理解。

PS:这次说了zuul的路由和在zuul网关做聚合项目。下次继续说zuul的微网关设置。

>>原创文章,欢迎转载。转载请注明:转载自IT人故事会,谢谢!
>>原文链接地址:『互联网架构』软件架构-zuul微服务网关(中)(101)
上一篇: 下一篇:

发表评论

电子邮件地址不会被公开。 必填项已用*标注