网关(Spring Cloud Gateway)

组件RPS
Spring Cloud GatwayRequests/sec:32213.38
Zuul1.0Requests/sec:20080.13
LinkerdRequests/sec:28050.76

网关提供 API 全托管服务,丰富的 API 管理功能,辅助企业管理大规模的 API,以降低管理成本和安全风险,包括协议适配、协议转发、安全策略、防刷、流量、监控日志等功能。

  • Route(路由):它由一个ID、一个目标URI、一组断言和一组过滤器定义。如果聚合谓词为true,则匹配路由。
  • Predicate(断言):这是一个Java8函数断言。输入类型是Spring Framework ServerWebExchange。这允许您匹配来自HTTP请求的任何内容,例如头或参数。
  • Filter(过滤):用特定工厂构建的GatewayFilter实例。在这里,您可以在发送下游请求之前或之后修改请求和响应。

 工作原理:

Route Predicate Factories 

  • The After Route Predicate Factory:指定日期时间之后发生的请求,接受一个参数datetime(它是java ZonedDateTime)。
spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]
  •  The After Route Predicate Factory:指定日期时间之前发生的请求,接受一个参数datetime(它是java ZonedDateTime)。
spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: https://example.org
        predicates:
        - Before=2017-01-20T17:42:47.789-07:00[America/Denver]
  •  The Between Route Predicate Factory:发生在datetime1之后和datetime2之前的请求。datetime2参数必须在datetime1之后
spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: https://example.org
        predicates:
        - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
  •  The Cookie Route Predicate Factory:匹配具有给定名称且其值与正则表达式匹配的Cookie,接受两个参数,Cookie名称和regexp(这是一个Java正则表达式)。
spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=chocolate, ch.p
  •  The Header Route Predicate Factory:匹配具有给定名称且其值与正则表达式匹配的Header,接受两个参数,Header名称和regexp(这是一个Java正则表达式)。
spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://example.org
        predicates:
        - Header=X-Request-Id, \d+
  •  The Host Route Predicate Factory:主机名匹配。
spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://example.org
        predicates:
        - Host=**.somehost.org,**.anotherhost.org
  •  The Method Route Predicate Factory:请求方式匹配。
spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: https://example.org
        predicates:
        - Method=GET,POST
  • The Path Route Predicate Factory:路径匹配 。
spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: https://example.org
        predicates:
        - Path=/red/{segment},/blue/{segment}
  •  The Query Route Predicate Factory:参数匹配,一个必需的参数和一个可选的regexp(这是一个Java正则表达式)。
spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=green


spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=red, gree.
  •  The RemoteAddr Route Predicate Factory:IP地址匹配。
spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: https://example.org
        predicates:
        - RemoteAddr=192.168.1.1/24

GatewayFilter Factories

  • The AddRequestHeader GatewayFilter Factory:请求头键值过滤。
# 匹配请求的下游请求头
spring:
  cloud:
    gateway:
      routes:
      - id: add_request_header_route
        uri: https://example.org
        filters:
        - AddRequestHeader=X-Request-red, blue

# 匹配路径或主机的URI变量
spring:
  cloud:
    gateway:
      routes:
      - id: add_request_header_route
        uri: https://example.org
        predicates:
        - Path=/red/{segment}
        filters:
        - AddRequestHeader=X-Request-Red, Blue-{segment}
  • The AddRequestParamter GatewayFilter Factory:请求参数键值过滤。
spring:
  cloud:
    gateway:
      routes:
      - id: add_request_parameter_route
        uri: https://example.org
        filters:
        - AddRequestParameter=red, blue

spring:
  cloud:
    gateway:
      routes:
      - id: add_request_parameter_route
        uri: https://example.org
        predicates:
        - Host: {segment}.myhost.org
        filters:
        - AddRequestParameter=foo, bar-{segment}
  • The DeduperesponseHeader GatewayFilter Factory:名称参数和可选策略参数。名称可以包含以空格分隔的标题名称列表。
spring:
  cloud:
    gateway:
      routes:
      - id: dedupe_response_header_route
        uri: https://example.org
        filters:
        - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
  • Spring Cloud CircuitBreaker GatewayFilter Factory:断路器网关过滤器。
# 
spring:
  cloud:
    gateway:
      routes:
      - id: circuitbreaker_route
        uri: https://example.org
        filters:
        - CircuitBreaker=myCircuitBreaker

#
spring:
  cloud:
    gateway:
      routes:
      - id: circuitbreaker_route
        uri: lb://backing-service:8088
        predicates:
        - Path=/consumingServiceEndpoint
        filters:
        - name: CircuitBreaker
          args:
            name: myCircuitBreaker
            fallbackUri: forward:/inCaseOfFailureUseThis
        - RewritePath=/consumingServiceEndpoint, /backingServiceEndpoint

#
@Bean
public RouteLocator routes(RouteLocatorBuilder builder) {
    return builder.routes()
        .route("circuitbreaker_route", r -> r.path("/consumingServiceEndpoint")
            .filters(f -> f.circuitBreaker(c -> c.name("myCircuitBreaker").fallbackUri("forward:/inCaseOfFailureUseThis"))
                .rewritePath("/consumingServiceEndpoint", "/backingServiceEndpoint")).uri("lb://backing-service:8088")
        .build();
}

spring:
  cloud:
    gateway:
      routes:
      - id: ingredients
        uri: lb://ingredients
        predicates:
        - Path=//ingredients/**
        filters:
        - name: CircuitBreaker
          args:
            name: fetchIngredients
            fallbackUri: forward:/fallback
      - id: ingredients-fallback
        uri: http://localhost:9994
        predicates:
        - Path=/fallback
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值