Spring Cloud 系列之 Gateway 服務網關(二)

本篇文章為系列文章,未讀第一集的同學請猛戳這里:Spring Cloud 系列之 Gateway 服務網關(一)

本篇文章講解 Gateway 網關的多種路由規(guī)則、動態(tài)路由規(guī)則(配合服務發(fā)現的路由規(guī)則)。


路由規(guī)則

點擊鏈接觀看:路由規(guī)則視頻(獲取更多請關注公眾號「哈嘍沃德先生」)

Spring Cloud Gateway 創(chuàng)建 Route 對象時, 使用 RoutePredicateFactory 創(chuàng)建 Predicate 對象,Predicate 對象可以賦值給 Route。

  • Spring Cloud Gateway 包含許多內置的 Route Predicate Factories。
  • 所有這些斷言都匹配 HTTP 請求的不同屬性。
  • 多個 Route Predicate Factories 可以通過邏輯與(and)結合起來一起使用。

路由斷言工廠 RoutePredicateFactory 包含的主要實現類如圖所示,包括 Datetime、 請求的遠端地址、 路由權重、 請求頭、 Host 地址、 請求方法、 請求路徑和請求參數等類型的路由斷言。

接下來我們舉例說明其中一部分如何使用,其余等大家工作中需要應用時再查詢資料學習或者咨詢我也可以。

Path

spring:
  application:
    name: gateway-server # 應用名稱
  cloud:
    gateway:
      # 路由規(guī)則
      routes:
        - id: product-service           # 路由 ID,唯一
          uri: http://localhost:7070/   # 目標 URI,路由到微服務的地址
          predicates:                   # 斷言(判斷條件)
            - Path=/product/**          # 匹配對應 URL 的請求,將匹配到的請求追加在目標 URI 之后
  • 請求 http://localhost:9000/product/1 將會路由至 http://localhost:7070/product/1

Query

spring:
  application:
    name: gateway-server # 應用名稱
  cloud:
    gateway:
      # 路由規(guī)則
      routes:
        - id: product-service           # 路由 ID,唯一
          uri: http://localhost:7070/   # 目標 URI,路由到微服務的地址
          predicates:                   # 斷言(判斷條件)
            #- Query=token               # 匹配請求參數中包含 token 的請求
            - Query=token, abc.         # 匹配請求參數中包含 token 并且其參數值滿足正則表達式 abc. 的請求

Method

spring:
  application:
    name: gateway-server # 應用名稱
  cloud:
    gateway:
      # 路由規(guī)則
      routes:
        - id: product-service           # 路由 ID,唯一
          uri: http://localhost:7070/   # 目標 URI,路由到微服務的地址
          predicates:                   # 斷言(判斷條件)
            - Method=GET                # 匹配任意 GET 請求

Datetime

spring:
  application:
    name: gateway-server # 應用名稱
  cloud:
    gateway:
      # 路由規(guī)則
      routes:
        - id: product-service           # 路由 ID,唯一
          uri: http://localhost:7070/   # 目標 URI,路由到微服務的地址
          predicates:                   # 斷言(判斷條件)
            # 匹配中國上海時間 2020-02-02 20:20:20 之后的請求
            - After=2020-02-02T20:20:20.000+08:00[Asia/Shanghai]

RemoteAddr

spring:
  application:
    name: gateway-server # 應用名稱
  cloud:
    gateway:
      # 路由規(guī)則
      routes:
        - id: product-service           # 路由 ID,唯一
          uri: http://localhost:7070/   # 目標 URI,路由到微服務的地址
          predicates:                   # 斷言(判斷條件)
            - RemoteAddr=192.168.10.1/0 # 匹配遠程地址請求是 RemoteAddr 的請求,0表示子網掩碼

Header

spring:
  application:
    name: gateway-server # 應用名稱
  cloud:
    gateway:
      # 路由規(guī)則
      routes:
        - id: product-service           # 路由 ID,唯一
          uri: http://localhost:7070/   # 目標 URI,路由到微服務的地址
          predicates:                   # 斷言(判斷條件)
            # 匹配請求頭包含 X-Request-Id 并且其值匹配正則表達式 \d+ 的請求
            - Header=X-Request-Id, \d+

動態(tài)路由(服務發(fā)現的路由規(guī)則)

動態(tài)路由其實就是面向服務的路由,Spring Cloud Gateway 支持與 Eureka 整合開發(fā),根據 serviceId 自動從注冊中心獲取服務地址并轉發(fā)請求,這樣做的好處不僅可以通過單個端點來訪問應用的所有服務,而且在添加或移除服務實例時不用修改 Gateway 的路由配置。

添加依賴

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

動態(tài)獲取 URI

點擊鏈接觀看:動態(tài)獲取 URI 視頻(獲取更多請關注公眾號「哈嘍沃德先生」)

配置文件

配置注冊中心和動態(tài)路由規(guī)則。

server:
  port: 9000 # 端口

spring:
  application:
    name: gateway-server # 應用名稱
  cloud:
    gateway:
      # 路由規(guī)則
      routes:
        - id: product-service           # 路由 ID,唯一
          uri: lb://product-service     # lb:// 根據服務名稱從注冊中心獲取服務請求地址
          predicates:                   # 斷言(判斷條件)
            - Path=/product/**          # 匹配對應 URL 的請求,將匹配到的請求追加在目標 URI 之后

# 配置 Eureka Server 注冊中心
eureka:
  instance:
    prefer-ip-address: true       # 是否使用 ip 地址注冊
    instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
  client:
    service-url:                  # 設置服務注冊中心地址
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

啟動類

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// 開啟 EurekaClient 注解,目前版本如果配置了 Eureka 注冊中心,默認會開啟該注解
//@EnableEurekaClient
@SpringBootApplication
public class GatewayServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServerApplication.class, args);
    }

}

訪問

訪問:http://localhost:9000/product/1 結果如下:

服務名稱轉發(fā)

點擊鏈接觀看:服務名稱轉發(fā)視頻(獲取更多請關注公眾號「哈嘍沃德先生」)

即使配置了動態(tài)獲取 URI 的方式,項目中微服務一旦過多幾十上百個時,配置中仍然要寫很多配置,這時候就可以使用服務名稱轉發(fā),與服務發(fā)現組件進行結合,通過 serviceId 轉發(fā)到具體服務實例。默認匹配URL /微服務名稱/** 路由到具體微服務。

配置文件

配置注冊中心和動態(tài)路由規(guī)則。

server:
  port: 9000 # 端口

spring:
  application:
    name: gateway-server # 應用名稱
  cloud:
    gateway:
      discovery:
        locator:
          # 是否與服務發(fā)現組件進行結合,通過 serviceId 轉發(fā)到具體服務實例。
          enabled: true                  # 是否開啟基于服務發(fā)現的路由規(guī)則
          lower-case-service-id: true    # 是否將服務名稱轉小寫

# 配置 Eureka Server 注冊中心
eureka:
  instance:
    prefer-ip-address: true       # 是否使用 ip 地址注冊
    instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
  client:
    service-url:                  # 設置服務注冊中心地址
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

啟動類

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// 開啟 EurekaClient 注解,目前版本如果配置了 Eureka 注冊中心,默認會開啟該注解
//@EnableEurekaClient
@SpringBootApplication
public class GatewayServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServerApplication.class, args);
    }

}

訪問

配置文件中沒有配置任何訂單服務的信息,訪問:http://localhost:9000/order-service/order/1 結果如下:

下一篇我們講解 Gateway 網關過濾器和全局過濾器以及自定義過濾器的使用,記得關注噢~

本文采用 知識共享「署名-非商業(yè)性使用-禁止演繹 4.0 國際」許可協議。

大家可以通過 分類 查看更多關于 Spring Cloud 的文章。


?? 您的點贊轉發(fā)是對我最大的支持。

?? 關注公眾號 哈嘍沃德先生「文檔 + 視頻」每篇文章都配有專門視頻講解,學習更輕松噢 ~


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容