SpringCloud-Gateway 網關路由、斷言、過濾

Gateway 簡介

是什麼?

Spring Cloud 全家桶中有個很重要的組件:網關。在 1.x 版本中使用的是 Zuul 網關,可是到了 2.x,因爲Zuul的升級不斷跳票,Spring Cloud 本身研發了一套網關組件:Spring Cloud Gateway。html

Spring Cloud Gateway基於 Spring Boot 2.x,Spring WebFlux 和 Project Reactor 構建,使用了 Webflux 中的 reactor-netty 響應式編程組件,底層使用了 Netty 通信框架。java

詳見:官網react

能幹嗎?

反向代理git

鑑權github

流量控制正則表達式

熔斷spring

日誌監控編程

......瀏覽器

網關在微服務架構中的位置

Gateway 的三大概念

Route(路由):路由是構建網關的基本模塊,它由 ID、目標 URI、一系列的斷言和過濾器組成,若是斷言爲 true 則匹配該路由cookie

Predicate(斷言)參考的是 Java8 中的 java.util.function.Predicate。開發人員能夠匹配 HTTP 請求中的全部內容(例如請求頭或請求參數),若是請求與斷言相匹配則進行路由

Filter(過濾):指的是 Spring 框架中 GatewayFilter 的實例,使用過濾器,能夠在請求被路由以前或以後對請求進行修改

工做流程

Clients make requests to Spring Cloud Gateway. If the Gateway Handler Mapping determines that a request matches a route, it is sent to the Gateway Web Handler. This handler runs the request through a filter chain that is specific to the request. The reason the filters are divided by the dotted line is that filters can run logic both before and after the proxy request is sent. All 「pre」 filter logic is executed. Then the proxy request is made. After the proxy request is made, the 「post」 filter logic is run.

翻譯:客戶端向 Spring Cloud Gateway 發出請求。若是網關處理程序映射肯定請求與路由匹配,則將其發送到網關 Web 處理程序。該處理程序經過特定於請求的過濾器鏈來運行請求。 篩選器由虛線分隔的緣由是,篩選器能夠在發送代理請求以前和以後運行邏輯。全部 「前置「 過濾器邏輯均被執行,而後發出代理請求,發出代理請求後,將運行「 後置 」過濾器邏輯。

總結:路由轉發 + 執行過濾器鏈

兩種配置方式

配置文件方式

咱們以訪問「百度新聞網」爲例,添加以下配置

server:
  port: 9527
spring:
  application:
    name: cloud-gateway9527
  cloud:
    gateway:
      routes:
        - id: news						# 路由id
          uri: http://news.baidu.com	# 真實調用地址
          predicates:
            - Path=/guonei				# 斷言,符合規則進行路由

瀏覽器雖然輸入 localhost:9527/guonei,卻會轉發到指定的地址

編碼方式

新增配置文件

@Configuration
public class GatewayConfig {
    @Bean
    public RouteLocator routes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("news2", r -> r.path("/guoji").uri("http://news.baidu.com"))
                .build();
    }
}

效果:

動態路由

開啓後,默認狀況下 Gateway 會根據註冊中心註冊的服務列表,以註冊中心上微服務名爲路徑建立動態路由進行轉發,從而實現動態路由的功能

spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true #開啓從註冊中心動態建立路由的功能,利用微服務名進行路由
      routes:
        - id: payment_routh1
          #uri: http://localhost:8001     #靜態,寫死了地址,只能調用一個服務
          uri: lb://CLOUD-PAYMENT-SERVICE #動態,lb://微服務名
          predicates:
            - Path=/payment/get/**
        - id: payment_routh2
          #uri: http://localhost:8001
          uri: lb://CLOUD-PAYMENT-SERVICE
          predicates:
            - Path=/payment/lb/**

Predicate 的使用

時間相關配置

After:在指定時間之進行路由

Before:在指定時間之進行路由

Between:在指定時間之進行路由

predicates:
    - Path=/payment/lb/**
    #- After=2020-04-25T16:30:58.215+08:00[Asia/Shanghai]
    #- Before=2020-04-25T16:40:58.215+08:00[Asia/Shanghai]
    - Between=2020-04-25T16:35:58.215+08:00[Asia/Shanghai],2020-04-25T16:40:58.215+08:00[Asia/Shanghai]

上述配置的時間格式能夠經過如下代碼獲得

@Test
public void test(){
    ZonedDateTime now = ZonedDateTime.now();
    System.out.println(now);
}

請求相關配置

Cookie

配置說明:【Cookie=cookie名, cookie值的正則表達式規則】

predicates:
  - Path=/payment/lb/**
  - Cookie=id, [0-9]

使用 curl 工具模擬攜帶 cookie 發送請求

Header

配置說明:【Header=header名, header值的正則表達式規則】

predicates:
  - Path=/payment/lb/**
  - Header=h, [a-h]

Host

配置說明:【Host=主機名(可配置多個,也可使用通配符)】

predicates:
  - Path=/payment/lb/**
  - Host=**.a.com,**.b.cn

Method

配置說明:【Method=請求類型】

predicates:
  - Path=/payment/lb/**
  - Method=GET

Path

配置說明:【Path=請求路徑】

predicates:
  - Path=/payment/lb/**

Query

配置說明:【Query=參數名,參數值】

predicates:
  - Path=/payment/lb/**
  - Query=name, zhangsan

詳見:官網

Filter 的使用

  • 生命週期:pre、post
  • 種類:GatewayFilter、GlobalFilter

GatewayFilter 在官方文檔有幾十種!詳細配置可參考 官網,這裏主要介紹自定義全局過濾器。

@Component
@Slf4j
public class MyGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String username = exchange.getRequest().getQueryParams().getFirst("username");
        //用戶名爲空時,給出錯誤響應
        if (username == null) {
            log.info("用戶名爲空,非法登陸");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}


獲取完整代碼

相關文章
相關標籤/搜索