上篇文章介绍了 Gataway 和注册中心的使用,以及 Gataway 中 Filter 的基本使用,这篇文章我们将继续介绍 Filter 的一些常用功能。

修改请求路径的过滤器

StripPrefix Filter

StripPrefix Filter 是一个请求路径截取的功能,我们可以利用这个功能来做特殊业务的转发。

application.yml 配置如下:

spring:
cloud:
gateway:
routes:
- id: nameRoot
uri: http://nameservice
predicates:
- Path=/name/**
filters:
- StripPrefix=2

上面这个配置的例子表示,当请求路径匹配到/name/**会将包含name和后边的字符串接去掉转发, StripPrefix=2就代表截取路径的个数,这样配置后当请求/name/bar/foo后端匹配到的请求路径就会变成http://nameservice/foo

我们还是在 cloud-gateway-eureka 项目中进行测试,修改 application.yml 如下:

spring:
cloud:
routes:
- id: nameRoot
uri: lb://spring-cloud-producer
predicates:
- Path=/name/**
filters:
- StripPrefix=2

配置完后重启 cloud-gateway-eureka 项目,访问地址:http://localhost:8888/name/foo/hello页面会交替显示:

hello world!
hello world smile!

和直接访问地址 http://localhost:8888/hello展示的效果一致,说明请求路径中的 name/foo/ 已经被截取。

PrefixPath Filter

PrefixPath Filter 的作用和 StripPrefix 正相反,是在 URL 路径前面添加一部分的前缀

spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: http://example.org
filters:
- PrefixPath=/mypath

大家可以下来去测试,这里不在演示。

限速路由器

限速在高并发场景中比较常用的手段之一,可以有效的保障服务的整体稳定性,Spring Cloud Gateway 提供了基于 Redis 的限流方案。所以我们首先需要添加对应的依赖包spring-boot-starter-data-redis-reactive

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>

配置文件中需要添加 Redis 地址和限流的相关配置

spring:
application:
name: cloud-gateway-eureka
redis:
host: localhost
password:
port: 6379
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: requestratelimiter_route
uri: http://example.org
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10
redis-rate-limiter.burstCapacity: 20
key-resolver: "#{@userKeyResolver}"
predicates:
- Method=GET
  • filter 名称必须是 RequestRateLimiter
  • redis-rate-limiter.replenishRate:允许用户每秒处理多少个请求
  • redis-rate-limiter.burstCapacity:令牌桶的容量,允许在一秒钟内完成的最大请求数
  • key-resolver:使用 SpEL 按名称引用 bean

项目中设置限流的策略,创建 Config 类。

public class Config {

    @Bean
KeyResolver userKeyResolver() {
return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
}
}

根据请求参数中的 user 字段来限流,也可以设置根据请求 IP 地址来限流,设置如下:

@Bean
public KeyResolver ipKeyResolver() {
return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
}

这样网关就可以根据不同策略来对请求进行限流了。

熔断路由器

在之前的 Spring Cloud 系列文章中,大家对熔断应该有了一定的了解,如过不了解可以先读这篇文章:熔断器 Hystrix

Spring Cloud Gateway 也可以利用 Hystrix 的熔断特性,在流量过大时进行服务降级,同样我们还是首先给项目添加上依赖。

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

配置示例

spring:
cloud:
gateway:
routes:
- id: hystrix_route
uri: http://example.org
filters:
- Hystrix=myCommandName

配置后,gateway 将使用 myCommandName 作为名称生成 HystrixCommand 对象来进行熔断管理。如果想添加熔断后的回调内容,需要在添加一些配置。

spring:
cloud:
gateway:
routes:
- id: hystrix_route
uri: lb://spring-cloud-producer
predicates:
- Path=/consumingserviceendpoint
filters:
- name: Hystrix
args:
name: fallbackcmd
fallbackUri: forward:/incaseoffailureusethis

fallbackUri: forward:/incaseoffailureusethis配置了 fallback 时要会调的路径,当调用 Hystrix 的 fallback 被调用时,请求将转发到/incaseoffailureuset这个 URI。

重试路由器

RetryGatewayFilter 是 Spring Cloud Gateway 对请求重试提供的一个 GatewayFilter Factory

配置示例:

spring:
cloud:
gateway:
routes:
- id: retry_test
uri: lb://spring-cloud-producer
predicates:
- Path=/retry
filters:
- name: Retry
args:
retries: 3
statuses: BAD_GATEWAY

Retry GatewayFilter 通过这四个参数来控制重试机制: retries, statuses, methods, 和 series。

  • retries:重试次数,默认值是 3 次
  • statuses:HTTP 的状态返回码,取值请参考:org.springframework.http.HttpStatus
  • methods:指定哪些方法的请求需要进行重试逻辑,默认值是 GET 方法,取值参考:org.springframework.http.HttpMethod
  • series:一些列的状态码配置,取值参考:org.springframework.http.HttpStatus.Series。符合的某段状态码才会进行重试逻辑,默认值是 SERVER_ERROR,值是 5,也就是 5XX(5 开头的状态码),共有5 个值。

网关服务Spring Cloud Gateway(三)的更多相关文章

  1. 网关服务Spring Cloud Gateway(一)

    Spring 官方最终还是按捺不住推出了自己的网关组件:Spring Cloud Gateway ,相比之前我们使用的 Zuul(1.x) 它有哪些优势呢?Zuul(1.x) 基于 Servlet,使 ...

  2. 网关服务Spring Cloud Gateway(二)

    上一篇文章服务网关 Spring Cloud GateWay 初级篇,介绍了 Spring Cloud Gateway 的相关术语.技术原理,以及如何快速使用 Spring Cloud Gateway ...

  3. Consul集群加入网关服务(Spring Cloud Gateway)

    Consul集群加入网关服务 架构示意图 外部的应用或网站通过外部网关服务消费各种服务,内部的生产者本身也可能是消费者,内部消费行为通过内部网关服务消费. 一个内部网关和一个外部网关以及一个Consu ...

  4. 微服务网关实战——Spring Cloud Gateway

    导读 作为Netflix Zuul的替代者,Spring Cloud Gateway是一款非常实用的微服务网关,在Spring Cloud微服务架构体系中发挥非常大的作用.本文对Spring Clou ...

  5. .net core下,Ocelot网关与Spring Cloud Gateway网关的对比测试

    有感于 myzony 发布的 针对 Ocelot 网关的性能测试 ,并且公司下一步也需要对.net和java的应用做一定的整合,于是对Ocelot网关.Spring Cloud Gateway网关做个 ...

  6. 最全面的改造Zuul网关为Spring Cloud Gateway(包含Zuul核心实现和Spring Cloud Gateway核心实现)

    前言: 最近开发了Zuul网关的实现和Spring Cloud Gateway实现,对比Spring Cloud Gateway发现后者性能好支持场景也丰富.在高并发或者复杂的分布式下,后者限流和自定 ...

  7. 创建网关项目(Spring Cloud Gateway)

    创建网关项目 加入网关后微服务的架构图 创建项目 POM文件 <properties> <java.version>1.8</java.version> <s ...

  8. spring Cloud网关之Spring Cloud Gateway

    Spring Cloud Gateway是什么?(官网地址:https://cloud.spring.io/spring-cloud-gateway/reference/html/) Spring C ...

  9. api网关揭秘--spring cloud gateway源码解析

    要想了解spring cloud gateway的源码,要熟悉spring webflux,我的上篇文章介绍了spring webflux. 1.gateway 和zuul对比 I am the au ...

随机推荐

  1. web服务器http.server 【python】

    参考博客: http://lxneng.iteye.com/blog/492063 http://www.cnblogs.com/itech/archive/2011/12/31/2308697.ht ...

  2. Python 练习题:计算 MAC 地址

    #!/usr/bin/env python #-*- coding:utf-8 -*- ''' 给一个MAC地址加1 ''' mac = '52:54:00:e6:b2:0a' prefix_mac ...

  3. docker run 详解

    docker run 用于把镜像启动为容器,语法如下: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 常见用法: [root@localhost ~]$ ...

  4. 苹果降频门:旧款iPhone哪些功能受到影响

    要说苹果最近发生的大事,就数网络上传的沸沸扬扬的降频门事件了,近期苹果在新发布的iOS 11系统中新增了一项功能,意在降低旧款手机的电量消耗,但限制了旧款iPhone的性能,那么iPhone有哪些功能 ...

  5. N76E003学习之路(二)

    最近一直在想N76E003和STM8M003的对比情况,在网上找了不少资料,看了不少文档,具体总结如下: STM8S003F3P6:一共20个脚,最多支持16个GPIO,支持16个外部中断:2个16位 ...

  6. PopupMenu弹出菜单

    CMenu MoviePopupMenu;//声明 MoviePopupMenu.CreatePopupMenu();//创建弹出菜单 根据对象类型增加弹出项 ) // FLASH对象 { CStri ...

  7. Android N 7 【 classes.dex】反编译失败:com.googlecode.d2j.DexException: not support version.

    Microsoft Windows [版本 6.1.7601]版权所有 (c) 2009 Microsoft Corporation.保留所有权利. D:\Android反编译工具[全]\2016\d ...

  8. 【BZOJ4195】[Noi2015]程序自动分析 并查集

    [BZOJ4195][Noi2015]程序自动分析 Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3 ...

  9. mysql日期处在某两个时间段之间的between比较

    where SYSDATE() between '2018-08-28 09:21:48' and '2018-08-28 09:25:48' sysdate()等于2018-08-28 09:23: ...

  10. 170607、SQL Select语句完整的执行顺序

    SQL Select语句完整的执行顺序: 1.from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将数据划分为多个分组: 4.使用聚集函 ...