gateway整理

一, 网关的作用

  1.性能:API高可用、负载均衡

  2.安全:权限身份认证、脱敏、流量清洗、后端签名、黑名单

  3.路由:动态路由规则

  4.日志:日志记录

  5.限流:流量控制、错峰流控、可以定义多种限流规则

  6.监控:记录请求响应数据、api耗时分析、性能监控

二, 过滤器

  网关过滤器 (GatewayFilter )有 20 多个 实现 类

  网关过滤器工厂(GatewayFilterFactory)有22个实现类

  全局过滤器(GlobalFilter )有11个实现类,全局过滤器是特殊的过滤器,会根据条件应用到所有路由中

三, 重要概念

  路由(route): 由ID、目标URI、谓词集合和过滤器集合定义

  断言(Predicate): 匹配断言的条件跳转到路由, 可以根据请求路径 请求方法 请求参数等进行判断

  过滤器(Filter): 是GatewayFilter的一个实例 对请求与响应进行修改

四, 常用配置

cors

image-20200226115300750

断言

 spring:
  cloud:
    gateway:
      routes:
      - id: ingredients
        uri: lb://ingredients
        predicates:
        - Path=//ingredients/**

PrefixPath过滤器--增加前缀

spring:
  cloud:
    gateway:
      routes:
      - id: fsh-house
        uri: lb://fsh-house
        predicates:
        - Path=/house/**
        filters:
        - PrefixPath:/house

StripPrefix 过滤器--去除前缀

spring:
  cloud:
    gateway:
      routes:
      - id: fsh-house
        uri: lb://fsh-house
        predicates:
        - Path=/house/**
        filters:
        - StripPrefix:/house

 

hystrix网关过滤器

 spring:
  cloud:
    gateway:
      routes:
      - id: ingredients
        uri: lb://ingredients
        predicates:
        - Path=//ingredients/**
        filters:
        name: Hystrix
          args:
            name: fetchIngredients
            fallbackUri: forward:/fallback
      - id: ingredients-fallback
        uri: http://localhost:9994
        predicates:
        - Path=/fallback

重试网关过滤器

 spring:
  cloud:
    gateway:
      routes:
      - id: fsh-house
        uri: lb://fsh-house
        predicates:
        - Path=/house/**
        filters:
        - name: Retry 
          args:
            retries3
            series:
            - SERVER_ERROR
            statuses:
            - OK
            methods:
            - GET
            - POST
            exceptions:
            - java.io.IOException

限流网关过滤器

 server:
  port8081
 spring:
  cloud:
    gateway:
      routes:
      - id: limit_route
        uri: http://httpbin.org:80/get
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]
        filters:
       name: RequestRateLimiter
          args:
            key-resolver'#{@hostAddrKeyResolver}'
            redis-rate-limiter.replenishRate1
            redis-rate-limiter.burstCapacity3
  application:
    name: gateway-limiter
  redis:
    host: localhost
    port6379
    database0
 


 

 

 

posted on 2021-05-06 17:33  enlht  阅读(102)  评论(0)    收藏  举报