gateway學習筆記


0 環境

  • 操作系統:win10
  • 編輯器:idea
  • springcloud版本:H版

1 簡介

Spring Cloud Gateway使用的是Spring Boot和Spring Webflux提供的Netty底層環境,不能和傳統的Servlet容器一起使用,也不能打包成一個WAR包。

  • 特點
    • 路徑重寫
    • 動態路由
    • 限流
    • 集成springcloud DiscoveryClient
    • 集成hystrix斷路器
  • 對比
    • gateway是spring家族的 可以與spring家族的其他組件無縫融合
    • zuul不支持長連接(websocket)
    • gateway占用資源資源更新且性能強於zuul(異步+非阻塞)
    • 限流

2 概念

  • 概念了解
    • Route --> 網關的基礎元素(組成 ---> ID + URI + 斷言 + 過濾器) 一個請求到達網關了 由gateway Handler Mapping通過斷言進行路由匹配(mapping) 當斷言為真 匹配路由(場景: 下班了 我走到了大門口 我奔着某某棟方向走去 但保安手拿溫度槍 氣勢洶洶的攔住了我(斷言) 測了一下溫度 看了一下通行證 戴口罩的 確定你沒毛病 進去吧 就這樣回到某棟某室)
    • predicate
      • java8中函數 匹配HTTP請求(匹配條件)
    • filter
      • 在請求發送前/后進行一些業務處理

3 gateway基本用法

3.1 創建springboot項目(gateway)

在這里插入圖片描述

3.2 添加依賴

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

3.3 yml配置

我們訪問目標地址http://httpbin.org
在這里插入圖片描述

spring:
  cloud:
    gateway:
      routes:
        - id: hi
          uri: http://httpbin.org
          predicates:
            - Path=/get

-不建議properties這樣配置(繁瑣)
在這里插入圖片描述

3.4 啟動類配置bean

和yml配置效果是一樣的()

/*    @Bean
    RouteLocator routeLocator(RouteLocatorBuilder builder){
        // 可以添加多個
        return builder.routes()
                // 訪問網址 http://httpbin.org/get
                // 通過網關后訪問 http://localhost:8080/get
                .route("hi", r -> r.path("/get").uri("http://httpbin.org"))
                .build();
    }*/

3.5 啟動gateway

在這里插入圖片描述

4 結合微服務

還是延用以前的eureka server和provider以及gateway
后面說啟用就是這三個啟動類的啟動/重啟

4.1 yml配置

spring:
  cloud:
    gateway:
      discovery:
        locator:
          # 開啟自動代理
          enabled: true
          # 服務id為true --> 這樣小寫服務就可訪問了
          #lower-case-service-id: true
  application:
    name: gateway

eureka:
  client:
    service-url:
      #      defaultZone: http://localhost:1234/eureka
      defaultZone: http://localhost:8761/eureka

#日志
logging:
  level:
    org.springframework.cloud.gateway:
      debug

4.2 啟用

  • 訪問 http://localhost:8080/provider/hello
    在這里插入圖片描述
    在這里插入圖片描述
    在這里插入圖片描述
  • 小寫服務
    在這里插入圖片描述
    #啟用它設為true即可 lower-case-service-id: true
    spring:
      cloud:
        gateway:
          discovery:
            locator:
              # 開啟自動代理
              enabled: true
              # 服務id為true --> 這樣小寫服務就可訪問了
              lower-case-service-id: true
      application:
        name: gateway
    
    eureka:
      client:
        service-url:
          #      defaultZone: http://localhost:1234/eureka
          defaultZone: http://localhost:8761/eureka
    
    #日志
    logging:
      level:
        org.springframework.cloud.gateway:
          debug
    
  • 重啟eureka server和consumer以及gateway
    在這里插入圖片描述
  • 這時訪問大寫服務會報錯
    在這里插入圖片描述

4 斷言

4.1 時間斷言

  • Before --> 請求時間在某個時間點之前 才會被路由
  • After --> 請求時間在某個時間點之后 才會被路由
  • Between --> 請求時間在2個時間點之間(兩個時間點,分開)
spring:
  cloud:
    gateway:
      routes:
        - id: before_route
          uri: http://www.baidu.com
          # 時間斷言
          predicates:
            - Before=2020-03-31T08:38:47+08:00

在這里插入圖片描述
若是改為Before=2020-03-24T08:38:47+08:00
在這里插入圖片描述

4.2 路徑斷言

一開始的path=get也是 就不在重復添加

spring:
  cloud:
    gateway:
      routes:
        # 路徑斷言 訪問指定地址http://www.w3school.com.cn/tcpip/*
        # 也就是localhost:8080/tcpip/*
        - id: w3c_route
          uri: http://www.w3school.com.cn
          predicates:
            - Path=/tcpip/{segment}
  • 訪問成功
    在這里插入圖片描述
  • 非該路徑 訪問失敗
    在這里插入圖片描述

4.3 query斷言

spring:
  cloud:
    gateway:
      routes:
        # 默認參數
        - id: query_route
          uri: http://httpbin.org
          predicates:
            # 可以指定參數和值 ?name=1開頭的值 才允許訪問
            - Query=name,1.*
  • 未加參數
    在這里插入圖片描述
  • 加參數1開頭 成功
    在這里插入圖片描述
    在這里插入圖片描述
  • 加參數2開頭 失敗
    在這里插入圖片描述

4.4 Method斷言

  • yml配置
spring:
  cloud:
    gateway:
      routes:
        - id: method_route
          uri: http://httpbin.org
          # 該網站post請求405 --> 沒有post 只能用get測試 用post測試get請求404就對了
          predicates:
            - Method=GET
  • post請求時 404(若是post方法 405說明該網站沒有post請求) 成功的截圖就是一開始的那張不在粘貼了
    在這里插入圖片描述

4.5 斷言組合使用

spring:
  cloud:
    gateway:
      routes:
        # 組合使用
        - id: many_route
          uri: http://httpbin.org
          predicates:
            # 可以指定參數和值 ?name=1開頭的值
            - Query=name,1.*
            - Method=GET
            - After=2020-03-01T08:38:47+08:00

在這里插入圖片描述

4.6 自定義路由斷言

  • 配置自定義斷言工廠
// 自定義路由斷言工廠
// 命名需要以RoutePredicateFactory結尾 比aRoutePredicateFactory 那么yml在使用時a就是斷言工廠的名字
@Component
public class CheckAuthRoutePredicateFactory extends AbstractRoutePredicateFactory<CheckAuthRoutePredicateFactory.User> {

    public CheckAuthRoutePredicateFactory() {
        super(User.class);
    }

    // 自定義配置類
    @Override
    public Predicate<ServerWebExchange> apply(User config) {
        return exchange -> {
            System.out.println("進入apply:" + config.getName());
            if (config.getName().equals("kitty")){
                return true;
            }
            return false;
        };
    }

    public static class User{
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
  • yml配置
spring:
  cloud:
    gateway:
      routes:
        # 自定義斷言工廠 -name就是之前以xxxRoutePredicateFactory的xxxx為斷言工廠名
        - id: customer_route
          uri: http://httpbin.org
          predicates:
          - name: CheckAuth
            args:
              name: kitty
  • 當參數name=kitty與自定義斷言工廠的getName一致時
    在這里插入圖片描述
  • 當參數name=kitty與自定義斷言工廠的getName不一致時
    在這里插入圖片描述

5 過濾器

gateway過濾器分類
- GlobalFilter
- GatewayFilter

5.1 AddRequestParameter過濾器

在請求添加時 自動添加額外參數
在這里插入圖片描述

6 小結

  • 基本配置 啟動類bean或是在yml中配置routes xxx
  • 學習了服務整合時 eureka server和provider以及gateway 依賴的添加 yml配置 連接服務 開啟自動代理以及訪問時服務大小寫的設置 日志的設置
  • 斷言的學習
    • path斷言 --> url匹配
    • query斷言 --> 參數匹配(自定義參數過濾規則) 類似?name=xxx
    • method斷言 --> 匹配HTTP的方法
    • 時間斷言 --> 之前/之后/之間
    • 組合斷言 --> 之前的這些斷言有機的組合
    • 自定義斷言 --> 需要繼承extends AbstractRoutePredicateFactory 在yml配置自定義工廠進行匹配
  • 過濾器的學習 中的AddRequestParameter 添加額外的參數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM