SpringCloud Gateway使用和配置,SpringCloud Gateway predicates詳細配置


SpringCloud Gateway使用和配置,

SpringCloud Gateway routes詳細配置,SpringCloud Gateway predicates詳細配置
SpringCloud Gateway 跨域配置,SpringCloud Gateway 超時配置

 

================================

©Copyright 蕃薯耀 2021-03-18

https://www.cnblogs.com/fanshuyao/

 

一、SpringCloud Gateway概述


spring cloud gateway旨在提供一種簡單而有效的方法來路由到api,並為它們提供跨領域的關注點,例如:安全性、監視/度量和恢復能力。

客戶端向Spring雲網關發出請求。如果網關處理程序映射確定請求與路由匹配,則將其發送到網關Web處理程序。此處理程序通過特定於請求的篩選器鏈運行請求。過濾器被虛線分割的原因是,過濾器可以在代理請求發送之前和之后運行邏輯。執行所有“預”過濾器邏輯。然后發出代理請求。在發出代理請求之后,運行“post”過濾器邏輯。

官方文檔地址:

https://docs.spring.io/spring-cloud-gateway/docs/2.2.7.RELEASE/reference/html/

 

SpringCloud Gateway主要成員:
Route(路由):網關的基本構建塊。它由一個ID、一個目標URI、一組謂詞和一組篩選器定義。如果聚合謂詞為true,則匹配路由。
Predicate(斷言,即路由匹配規則):這是一個Java 8 Function Predicate。輸入類型是springframework serverwebexchange。這允許您匹配來自HTTP請求的任何內容,例如頭或參數。
Filter(過濾器):這些是用特定工廠構建的Spring Framework GatewayFilter的實例。在這里,您可以在發送下游請求之前或之后修改請求和響應。

 

流程圖:

 

 

二、SpringCloud Gateway使用和配置
1、pom.xml引入依賴
主要的包是:spring-cloud-starter-gateway

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

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.4.4</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>


<!-- Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency. -->
<!-- 
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
 -->

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
    <version>2.2.7.RELEASE</version>
</dependency>

 

2、application.properties文件配置

基於Eureka為服務注冊中心

server.port=8701

spring.application.name=SPRINGCLOUD-EUREKA-GATEWAY

#gateway路由配置
#使用服務發現路由
spring.cloud.gateway.discovery.locator.enabled=true
#服務路由名小寫
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

#設置路由id,沒有固定規則,要求唯一,建設配合服務名
spring.cloud.gateway.routes[0].id=GATEWAY-SERVICE
#設置路由的uri,可以是調用的服務名,也可以請求的地址,當predicates匹配成功后,使用該路由的uri進行服務調用
#設置為服務名:lb://SPRINGCLOUD-EUREKA-SERVER
#設置為請求的地址:http://127.0.0.1:8601
#使用lb,有2個微服務,先啟動一個,再啟動gateway,然后再啟動第二個微服務,未自動實現負載均衡;要先啟動2個微服務后,再啟動gateway,這樣才能實現負載均衡
spring.cloud.gateway.routes[0].uri=lb://SPRINGCLOUD-EUREKA-SERVER
#設置路由斷言,即調用的地址匹配的規則


#斷言predicates的屬性可以有:
#Path:Path=/**
#Cookie:Cookie=chocolate, ch.p,前面的為name,逗號后面的為值
#Header:Header=X-Request-Id, \d+,前面的為name,逗號后面的為值
#Host:Host=**.somehost.org,**.anotherhost.org
#Method:Method=GET
#Query:Query=aaa,請求參數必須有name為aaa的參數;Query=aaa, 111:請求參數必須有name為aaa的參數,且aaa參數的值為111;
#After:After=2021-03-17T15:47:51.534+08:00[Asia/Shanghai],日期時間,在該日期以后請求才被匹配,時間可以使用java.time.ZonedDateTime中的ZonedDateTime.now()獲取當前時間
#Before:Before=2022-03-17T15:47:51.534+08:00[Asia/Shanghai],日期時間,在該日期之前才被匹配
#Between:Between=2021-03-17T15:47:51.534+08:00[Asia/Shanghai],2022-03-17T15:47:51.534+08:00[Asia/Shanghai],使用兩個參數用逗號分隔,在兩個時間范圍內的請求才被匹配
#RemoteAddr:RemoteAddr=192.168.1.1/24

#斷言方式一:
#/gateway/**:表示/gateway/路徑下所有請求
#spring.cloud.gateway.routes[0].predicates[0].args.pattern=/**
#這個不能少,少了會報錯:reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalArgumentException: Unable to find RoutePredicateFactory with name gatewayTest
#spring.cloud.gateway.routes[0].predicates[0].name=Path

#斷言方式二:請求微服務上的服務,方式一和方式二是2種不同的寫法,方式一將規則分開,方式二寫在一起
spring.cloud.gateway.routes[0].predicates[0]=Path=/test


#斷言方式三:跳轉到百度
spring.cloud.gateway.routes[1].id=GATEWAY-REDIRECT
spring.cloud.gateway.routes[1].uri=https://www.baidu.com
spring.cloud.gateway.routes[1].predicates[0]=Path=/redirect/**
spring.cloud.gateway.routes[1].filters[0].name=RedirectTo
spring.cloud.gateway.routes[1].filters[0].args.status=301
spring.cloud.gateway.routes[1].filters[0].args.url=https://www.baidu.com


#eureka實例名稱
#eureka.instance.hostname=eureka8601.com
eureka.instance.instance-id=GATEWAY-8701
#路徑顯示IP地址
eureka.instance.prefer-ip-address=true
#eureka客戶端向服務端發送心跳的時間間隔,單元為秒,默認為30秒
eureka.instance.lease-renewal-interval-in-seconds=2
#eureka服務端收到最后一次心跳等待的時間上限,超時將移除服務,單元為秒,默認為90秒
eureka.instance.lease-expiration-duration-in-seconds=5

#false表示向注冊中心注冊自己
eureka.client.register-with-eureka=true
#是否從Eureka抓取已有的注冊信息,默認為true。單節點不用設置,集群必須設置為true,才能配置ribbon使用負載均衡
eureka.client.fetch-registry=true
#設置與Eureka server交互的地址查詢服務和注冊服務都需要依賴這個地址
eureka.client.service-url.defaultZone=http://eureka8501.com:8501/eureka
#集群配置
#eureka.client.service-url.defaultZone=http://eureka8501.com:8501/eureka,http://eureka8502.com:8501/eureka

 

3、啟動類

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class SpringCloud8701GatewayApplication {

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

}

 

三、springCloud Gateway routes predicates 配置方式

配置的方式有2種,1種是鍵值對的方式,一種是完全展開參數的方式

 

1、通過鍵值對的方式

使用兩個參數定義Cookie路由,即Cookie名稱mycookie和匹配mycookievalue的值。

 

application.yml文件的配置方式

快捷方式配置由篩選器名稱、等號(=)和用逗號(,)分隔的參數值識別。

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=mycookie,mycookievalue

 

application.properties文件的配置方式:

#設置路由id,沒有固定規則,要求唯一,建設配合服務名
spring.cloud.gateway.routes[0].id=cookie_route
#設置路由的uri,可以是調用的服務名,也可以請求的地址,當predicates匹配成功后,使用該路由的uri進行服務調用
##設置為服務名:lb://SPRINGCLOUD-EUREKA-SERVER
#設置為請求的地址:http://127.0.0.1:8601
spring.cloud.gateway.routes[0].uri=https://example.org
spring.cloud.gateway.routes[0].predicates[0]=Cookie=mycookie,mycookievalue

 

2、完全展開的參數
完全展開的參數更像是帶有名稱/值對的標准yaml配置。通常,會有一個name鍵和一個args鍵。args鍵是用於配置謂詞或篩選器的鍵值對的映射。

 

application.yml文件的配置方式

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - name: Cookie
          args:
            name: mycookie
            regexp: mycookievalue

 

application.properties文件的配置方式:

spring.cloud.gateway.routes[0].id=cookie_route
spring.cloud.gateway.routes[0].uri=https://example.org
spring.cloud.gateway.routes[0].predicates[0].name=Cookie
spring.cloud.gateway.routes[0].predicates[0].args.name=mycookie
spring.cloud.gateway.routes[0].predicates[0].args.regexp=mycookievalue

 

四、springCloud Gateway predicates 詳細配置

1、After route predicate
After route路由接受一個參數datetime(這是一個java分區日期時間)。此路由匹配在指定日期時間之后發生的請求,即在此時間后的請求才能正常訪問。

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]

After=2017-01-20T17:42:47.789-07:00[America/Denver]中的時間可以使用java.time.ZonedDateTime中的ZonedDateTime.now()獲取當前時間
具體示例:

import java.time.ZonedDateTime;

public class DatetimeUtil {

    public static void main(String[] args) {
        ZonedDateTime t = ZonedDateTime.now();
        
        //2021-03-17T15:47:51.534+08:00[Asia/Shanghai]
        System.out.println(t);
    }
}

 

2、Before route predicate
Before route路由接受一個參數datetime(它是java分區的日期時間)。此路由匹配在指定日期時間之前發生的請求。

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: https://example.org
        predicates:
        - Before=2017-01-20T17:42:47.789-07:00[America/Denver]

 

3、Between route predicate
路由間接受兩個參數,datetime1和datetime2,它們是java分區的日期時間對象。此謂詞匹配發生在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]

 

4、Cookie route predicate
Cookie路由工廠接受兩個參數,Cookie名稱和regexp(這是一個Java正則表達式)。此路由匹配具有給定名稱且其值與正則表達式匹配的cookie。

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=chocolate, ch.p

 

5、Header route predicate
請求頭路由有兩個參數,頭名稱和regexp(這是一個Java正則表達式)。此路由與具有給定名稱的標頭匹配,該標頭的值與正則表達式匹配。

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://example.org
        predicates:
        - Header=X-Request-Id, \d+

 

6、Host route predicate
主機路由采用一個參數:主機名模式列表。這個模式是一個螞蟻風格的模式。作為分隔符。這個路由匹配與模式匹配的主機頭。

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://example.org
        predicates:
        - Host=**.somehost.org,**.anotherhost.org

 

7、Method Route Predicate
方法路由接受一個methods參數,該參數是一個或多個參數:要匹配的HTTP方法。

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: https://example.org
        predicates:
        - Method=GET,POST

如果請求方法是GET或POST,則此路由匹配。

 

8、Path Route Predicate
路徑路由工廠有兩個參數:一個Spring路徑匹配器模式列表和一個名為matchOptionalTrailingSeparator的可選標志。

spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: https://example.org
        predicates:
        - Path=/red/{segment},/blue/{segment}

如果請求路徑是:/red/1或/red/blue或/blue/green,則此路由匹配。

示例:

spring.cloud.gateway.routes[0].id=GATEWAY-SERVICE
spring.cloud.gateway.routes[0].uri=lb://SPRINGCLOUD-EUREKA-SERVER
spring.cloud.gateway.routes[0].predicates[0]=Path=/test
或者
spring.cloud.gateway.routes[0].predicates[0]=Path=/gateway/**

 

9、Query route predicate
查詢路由有兩個參數:一個必需的參數和一個可選的regexp(這是一個Java正則表達式)。

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=green

如果請求參數中包含名稱為green的參數,則路由匹配。

 

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=red, gree.

如果請求參數中包含名稱為green的參數,且值為red,則路由匹配。

 

10、RemoteAddr route predicate
遠程Addr route獲取源的列表(最小大小1),這些源是CIDR表示法(IPv4或IPv6)字符串,例如192.168.0.1/16(其中192.168.0.1是IP地址,16是子網掩碼)。

spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: https://example.org
        predicates:
        - RemoteAddr=192.168.1.1/24

 


11、Weight route predicate
權重路由工廠有兩個參數:group和Weight(int)。每組計算重量。

spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: https://weighthigh.org
        predicates:
        - Weight=group1, 8
      - id: weight_low
        uri: https://weightlow.org
        predicates:
        - Weight=group1, 2

 

 


四、springCloud Gateway GatewayFilter 詳細配置


1、The AddRequestHeader GatewayFilter Factory

spring:
  cloud:
    gateway:
      routes:
      - id: add_request_header_route
        uri: https://example.org
        filters:
        - AddRequestHeader=X-Request-red, blue

 

2、The AddRequestParameter GatewayFilter Factory

spring:
  cloud:
    gateway:
      routes:
      - id: add_request_parameter_route
        uri: https://example.org
        filters:
        - AddRequestParameter=red, blue

 

3、The AddResponseHeader GatewayFilter Factory

spring:
  cloud:
    gateway:
      routes:
      - id: add_response_header_route
        uri: https://example.org
        filters:
        - AddResponseHeader=X-Response-Red, Blue

 

4、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

 

5、The RedirectTo GatewayFilter Factory
重定向到網關過濾器工廠需要兩個參數,status和url。status參數應該是300系列重定向HTTP代碼,例如301。url參數應該是有效的url。這是位置標頭的值。對於相對重定向,應該使用uri:no://op作為路由定義的uri。

spring:
  cloud:
    gateway:
      routes:
      - id: prefixpath_route
        uri: https://example.org
        filters:
        - RedirectTo=302, https://acme.org

示例:

#斷言方式三:跳轉到百度
spring.cloud.gateway.routes[1].id=GATEWAY-REDIRECT
spring.cloud.gateway.routes[1].uri=https://www.baidu.com
spring.cloud.gateway.routes[1].predicates[0]=Path=/redirect/**
spring.cloud.gateway.routes[1].filters[0].name=RedirectTo
spring.cloud.gateway.routes[1].filters[0].args.status=302
spring.cloud.gateway.routes[1].filters[0].args.url=https://www.baidu.com

 

其它的過濾器見官方文檔:

https://docs.spring.io/spring-cloud-gateway/docs/2.2.7.RELEASE/reference/html/#gatewayfilter-factories

 

五、SpringCloud Gateway 自定義全局過濾器

@Bean
public GlobalFilter customFilter() {
    return new CustomGlobalFilter();
}

public class CustomGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("custom global filter");
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return -1;
    }
}

 

六、SpringCloud Gateway 超時配置
connect-timeout:必須以毫秒為單位指定連接超時。
response-timeout:響應超時必須指定為java.time.Duration文件

Http超時(響應和連接)可以為所有路由配置,並為每個特定路由重寫。

 

1、全局超時配置

spring:
  cloud:
    gateway:
      httpclient:
        connect-timeout: 1000
        response-timeout: 5s

 

2、單個路由超時配置

      - id: per_route_timeouts
        uri: https://example.org
        predicates:
          - name: Path
            args:
              pattern: /delay/{timeout}
        metadata:
          response-timeout: 200
          connect-timeout: 200

 

七、SpringCloud Gateway 跨域配置
可以配置網關來控制CORS行為。“全局”CORS配置是URL模式到Spring框架CORS配置的映射

spring:
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOrigins: "https://docs.spring.io"
            allowedMethods:
            - GET

在示例中,允許來自docs.spring.io對於所有GET請求的路徑。

 

 

 

 

 

(時間寶貴,分享不易,捐贈回饋,^_^)

================================

©Copyright 蕃薯耀 2021-03-18

https://www.cnblogs.com/fanshuyao/


免責聲明!

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



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