5.Springcloud学习之-路由网关zuul

467 阅读4分钟

1.背景

今天我们学习SpringCloud的Hystrix熔断器

我们今天继续使用之前eureka-server作为服务注册中心

使用Springboot和springcloud的版本如下

  • springboot版本:2.3.5-release
  • springcloud版本:Hoxton.SR9

2.Zuul的功能

通过官网的信息我们可以看到zuul的功能有以下:

  1. Authentication and Security - identifying authentication requirements for each resource and rejecting requests that do not satisfy them.
  2. Insights and Monitoring - tracking meaningful data and statistics at the edge in order to give us an accurate view of production.
  3. Dynamic Routing - dynamically routing requests to different backend clusters as needed.
  4. Stress Testing - gradually increasing the traffic to a cluster in order to gauge performance.
  5. Load Shedding - allocating capacity for each type of request and dropping requests that go over the limit.
  6. Static Response handling - building some responses directly at the edge instead of forwarding them to an internal cluster
  7. Multiregion Resiliency - routing requests across AWS regions in order to diversify our ELB usage and move our edge closer to our members

翻译过来:

  •   1.验证与安全保障: 识别面向各类资源的验证要求并拒绝那些与要求不符的请求。
  •   2.审查与监控: 在边缘位置追踪有意义数据及统计结果,从而为我们带来准确的生产状态结论。
  •   3.动态路由: 以动态方式根据需要将请求路由至不同后端集群处。
  •   4.压力测试: 逐渐增加指向集群的负载流量,从而计算性能水平。
  •   5.负载分配: 为每一种负载类型分配对应容量,并弃用超出限定值的请求。
  •   6.静态响应处理: 在边缘位置直接建立部分响应,从而避免其流入内部集群。
  •   7.多区域弹性: 跨越AWS区域进行请求路由,旨在实现ELB使用多样化并保证边缘位置与使用者尽可能接近。
  •   

3 Zuul工作原理

3.1过滤器机制

zuul的核心是一系列的filters, 其作用可以类比Servlet框架的Filter,或者AOP。

zuul把Request route到 用户处理逻辑 的过程中,这些filter参与一些过滤处理,比如Authentication,Load Shedding等 image

4. Zuul过滤器类型和生命周期

4.1 过滤器类型

Zuul大部分功能都是通过过滤器来实现的。Zuul中定义了四种标准过滤器类型,这些过滤器类型对应于请求的典型生命周期。

(1) PRE:这种过滤器在请求被路由之前调用。我们可利用这种过滤器实现身份验证、在集群中选择请求的微服务、记录调试信息等。

(2) ROUTING:这种过滤器将请求路由到微服务。这种过滤器用于构建发送给微服务的请求,并使用Apache HttpClient或Netfilx Ribbon请求微服务。

(3) POST:这种过滤器在路由到微服务以后执行。这种过滤器可用来为响应添加标准的HTTP Header、收集统计信息和指标、将响应从微服务发送给客户端等。

(4) ERROR:在其他阶段发生错误时执行该过滤器

4.1 zuul的生命周期

image

这个后续看源码也可以看到生命周期,后面一起讲哈

5. 项目搭建

添加依赖

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

添加注解

@EnableZuulProxy

@EnableZuulProxy

增加配置类

spring:
  application:
    name: ms-zuul

server:
  port: 7000

eureka:
  client:
    service-url:
      defaultZone: http://192.168.1.119:8000/eureka
    register-with-eureka: true
  instance:
    prefer-ip-address: true
    #appname: ${spring.application.name}
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    hostname: ${spring.cloud.client.ip-address}

##服务的实列ID来访问
# service id pattern 通过服务名称路由
# key结构 : zuul.routes.customName.path=xxx
# 路径匹配规则
#zuul:
#  routes:
#    ms-feign-producer:
#     path: /producer/**
#     service-id: ms-feign-producer


##通过服务名称
# key结构 : zuul.routes.customName.path=xxx
# 路径匹配规则
zuul:
  routes:
    ms-feign-producer:
     path: /producer/**


##通过path和URL
# key结构 : zuul.routes.customName.path=xxx
# 路径匹配规则
#zuul:
#  routes:
#    ms-feign-producer2:
#      path: /producer2/**
#      url: http://192.168.1.119:8081/
## 统一设置路由前缀
  #prefix: /ms-zuul
## 是否忽略路由前缀 默认为true
  #strip-prefix: true

## 关闭通过微服务名称路访问(暴露了微服务)
  #ignored-services: *
## 忽略一些特殊的访问路径
  #ignored-patterns:
## Arrays.asList("Cookie", "Set-Cookie", "Authorization")); 默认这些头部信息将不会传递
  sensitive-headers:

6 测试

路由的方式很多种,我们可以看到上面的配置文件中我们提供了三种:

6.1服务的实列ID来访问(推荐)

##服务的实列ID来访问
# service id pattern 通过服务名称路由
# key结构 : zuul.routes.customName.path=xxx
# 路径匹配规则
#zuul:
#  routes:
#    ms-feign-producer:
#     path: /producer/**
#     service-id: ms-feign-producer

6.2服务名称

##通过服务名称
# key结构 : zuul.routes.customName.path=xxx
# 路径匹配规则
zuul:
  routes:
    ms-feign-producer:
     path: /producer/**

6.3通过path和URL(无注册中心)

##通过path和URL
# key结构 : zuul.routes.customName.path=xxx
# 路径匹配规则
#zuul:
#  routes:
#    ms-feign-producer2:
#      path: /producer2/**
#      url: http://192.168.1.119:8081/,http://192.168.1.120:8081/