跟我学SpringCloud | 第十三篇:Spring Cloud Gateway服务化和过滤器

SpringCloud系列教程 | 第十三篇:Spring Cloud Gateway服务化和过滤器

Springboot: 2.1.6.RELEASEhtml

SpringCloud: Greenwich.SR1git

如无特殊说明,本系列教程全采用以上版本github

上一篇文章服务网关 Spring Cloud GateWay 初级篇,介绍了 Spring Cloud Gateway 的相关术语、技术原理,以及如何快速使用 Spring Cloud Gateway。这篇文章咱们继续学习 Spring Cloud Gateway 的高级使用方式,好比如何配置服务中心来使用,如何使用熔断、限流等高级功能。spring

1. 注册中心

1.1 准备服务和注册中心

上篇主要讲解了网关代理单个服务的使用语法,在实际的工做中,服务的相互调用都是依赖于服务中心提供的入口来使用,服务中心每每注册了不少服务,若是每一个服务都须要单独配置的话,这将是一份很枯燥的工做。Spring Cloud Gateway 提供了一种默认转发的能力,只要将 Spring Cloud Gateway 注册到服务中心,Spring Cloud Gateway 默认就会代理服务中心的全部服务,下面用代码演示。后端

在介绍Zuul的时候,咱们用到了Eureka和producer,本次演示仍是须要他们两个,将他们两个CV过来。api

1.2 服务网关注册到注册中心

上一篇用到的gateway也CV过来,在依赖文件里面加入:浏览器

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

添加对eureka的依赖,在启动文件加入注解@EnableEurekaClient。app

修改配置文件application.yml:负载均衡

server:
  port: 8080
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
logging:
  level:
    org.springframework.cloud.gateway: debug

配置说明:socket

  • spring.cloud.gateway.discovery.locator.enabled:是否与服务注册于发现组件进行结合,经过 serviceId 转发到具体的服务实例。默认为 false,设为 true 便开启经过服务中心的自动根据 serviceId 建立路由的功能。
  • eureka.client.service-url.defaultZone指定注册中心的地址,以便使用服务发现功能
  • logging.level.org.springframework.cloud.gateway 调整相 gateway 包的 log 级别,以便排查问题

修改完成后启动 gateway 项目,访问注册中心地址 http://localhost:8761/ 便可看到名为 API-GATEWAY的服务。

1.3 测试

将 gateway 注册到服务中心以后,网关会自动代理全部的在注册中心的服务,访问这些服务的语法为:

http://网关地址:端口/服务中心注册 serviceId/具体的url

好比咱们的 producer 项目有一个 /hello 的服务,访问此服务的时候会返回:"hello "+name+",producer is ready"。

好比访问地址:http://localhost:8081/hello?name=spring,页面返回:hello spring,producer is ready。

按照上面的语法咱们经过网关来访问,浏览器输入:http://localhost:8080/SPRING-CLOUD-PRODUCER/hello?name=spring 一样返回:hello spring,producer is ready。证实服务网关转发成功。

咱们将项目 producer 复制一份为 producer1,将/hello服务的返回值修改成 hello spring,producer1 is ready。修改端口号为 8082 ,修完完成后重启,这时候访问注册中心后台会发现有两个名为 SPRING-CLOUD-PRODUCER的服务。

在浏览器屡次访问地址:http://localhost:8888/SPRING-CLOUD-PRODUCER/hello,页面交替返回如下信息:

hello spring,producer is ready。
hello spring,producer1 is ready。

说明后端服务自动进行了均衡负载。

2. 基于 Filter(过滤器) 实现的高级功能

在Zuul高级篇中大概介绍过 Filter 的概念。

Spring Cloud Gateway 的 Filter 的生命周期不像 Zuul 的那么丰富,它只有两个:“pre” 和 “post”。

  • PRE: 这种过滤器在请求被路由以前调用。咱们可利用这种过滤器实现身份验证、在集群中选择请求的微服务、记录调试信息等。
  • POST:这种过滤器在路由到微服务之后执行。这种过滤器可用来为响应添加标准的 HTTP Header、收集统计信息和指标、将响应从微服务发送给客户端等。

Spring Cloud Gateway 的 Filter 分为两种:GatewayFilter 与 GlobalFilter。GlobalFilter 会应用到全部的路由上,而 GatewayFilter 将应用到单个路由或者一个分组的路由上。

Spring Cloud Gateway 内置了9种 GlobalFilter,好比 Netty Routing Filter、LoadBalancerClient Filter、Websocket Routing Filter 等,根据名字便可猜想出这些 Filter 的做者,具体你们能够参考官网内容:Global Filters

利用 GatewayFilter 能够修改请求的 Http 的请求或者响应,或者根据请求或者响应作一些特殊的限制。 更多时候咱们会利用 GatewayFilter 作一些具体的路由配置,下面咱们作一些简单的介绍。

2.1 快速上手 Filter 使用

咱们以 AddRequestParameter GatewayFilter 来演示一下,如何在项目中使用 GatewayFilter,AddRequestParameter GatewayFilter 能够在请求中添加指定参数。

2.1.1 配置application.yml示例

server:
  port: 8080
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: add_request_parameter_route
          uri: http://localhost:8081
          filters:
            - AddRequestParameter=foo, bar
          predicates:
            - Method=GET
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
logging:
  level:
    org.springframework.cloud.gateway: debug

这里的 routes 手动指定了服务的转发地址,设置全部的 GET 方法都会自动添加foo=bar,http://localhost:8081 是 producer 项目,咱们在此项目中添加一个 foo() 方法,用来接收转发中添加的参数 foo。

@RequestMapping("/foo")
public String foo(String foo) {
    return "hello "+foo+"!";
}

修改完成后重启 gateway、producer 项目。访问地址 http://localhost:8081/foo 页面返回:hello null!,说明并无接受到参数 foo;经过网关来调用此服务,浏览器访问地址 http://localhost:8080/foo 页面返回:hello bar!,说明成功接收到参数 foo 参数的值 bar ,证实网关在转发的过程当中已经经过 filter 添加了设置的参数和值。

2.2 服务化路由转发

面咱们使用 uri 指定了一个服务转发地址,单个服务这样使用问题不大,可是咱们在注册中心每每会使用多个服务来共同支撑整个服务的使用,这个时候咱们就指望能够将 Filter 做用到每一个应用的实例上,spring cloud gateway 工了这样的功能,只须要简单配置便可。

为了测试两个服务提供者是否都被调用,咱们在 producer1 项目中也一样添加 foo() 方法。

@RequestMapping("/foo")
public String foo(String foo) {
    return "hello "+foo+"!@@@@";
}

为了和 producer 中 foo() 方法有所区别,这里使用了多加了4个@。同时将 gateway 项目配置文件中的 uri 内容修改以下:

#格式为:lb://应用注册服务名
uri: lb://spring-cloud-producer

修改完以后,从新启动项目 gateway、producer1,浏览器访问地址: http://localhost:8080/foo 页面交替出现:

hello bar!
hello bar!@@@@

证实请求依据均匀转发到后端服务,而且后端服务均接收到了 filter 增长的参数 foo 值。

这里其实默认使用了全局过滤器 LoadBalancerClient ,当路由配置中 uri 所用的协议为 lb 时(以uri: lb://spring-cloud-producer为例),gateway 将使用 LoadBalancerClient 把 producer 经过 eureka 解析为实际的主机和端口,并进行负载均衡。

示例代码-Github

参考:

http://www.ityouknow.com/springcloud/2019/01/19/spring-cloud-gateway-service.html

原文出处:https://www.cnblogs.com/babycomeon/p/11167514.html

相关文章
相关标签/搜索