1. 程式人生 > 其它 >SpringCloudAlibaba - Gateway 自定義路由謂詞工廠

SpringCloudAlibaba - Gateway 自定義路由謂詞工廠

目錄

前言

Route(路由)是Spring Cloud Gateway的基礎元素,就是一個轉發規則,其包含ID、目標URLPredicate集合以及Filter集合


環境

Spring Cloud Hoxton.SR9 + Spring Cloud Alibaba 2.2.6.RELEASE


路由配置示例

  • 如果訪問gateway的/baidu這個路徑就會進入該路由,會用AddRequestHeader這個過濾器去處理,再將請求轉發到http://www.baidu.com
spring:
  cloud:
    gateway:
      routes: 
        - id: some_route
          uri: http://www.baidu.com
          predicates:
            - Path=/baidu
          filters:
            - AddRequestHeader=X-Request-Foo, Bar

路由配置的兩種形式

路由到指定URL

通配

  • 訪問GATEWAY_URL/**會轉發到http://www.baidu.com/**
spring:
  cloud:
    gateway:
      routes:
      - id: {唯一標識}
        uri: http://www.baidu.com

精確匹配

  • 訪問GATEWAY_URL/user-center/user/轉發到http://www.coisini.club/user-center/user/
spring:
  cloud:
    gateway:
      routes:
      - id: {唯一標識}
        uri: http://www.coisini.club/user-center/user/

路由到微服務

通配

  • 訪問GATEWAY_URL/**轉發到user-center微服務的/**
spring:
  cloud:
    gateway:
      routes:
      - id: {唯一標識}
        uri: lb://user-center

精確匹配

  • 訪問GATEWAY_URL/user-center/user/name轉發到user-center微服務的/user/name
spring:
  cloud:
    gateway:
      routes:
      - id: {唯一標識}
        uri: lb://user-center/user/name

路由謂詞工廠

路由謂詞工廠(Route Predicate Factories)是作用在指定路由之上的一堆謂詞條件


內建的路由謂詞工廠

謂詞工廠 作用 引數
After 請求時的時間在配置的時間後時轉發該請求 一個帶有時區的具體時間
Before 請求時的時間在配置的時間前時轉發該請求 一個帶有時區的具體時間
Between 請求時的時間在配置的時間段內時轉發該請求 一個帶有時區的具體時間段
Cookie 請求時攜帶的Cookie名稱及值與配置的名稱及值相符時轉發該請求 Cookie的名稱及值,支援使用正則表示式來匹配值
Header 請求時攜帶的Header名稱及值與配置的名稱及值相符時轉發該請求 Header的名稱及值,支援使用正則表示式來匹配值
Host 請求時名為Host的Header的值與配置的值相符時轉發該請求 Host的值,支援配置多個且支援使用萬用字元
Method 請求時所使用的HTTP方法與配置的請求方法相符時轉發該請求 HTTP請求方法,例如GET、POST等
Path 請求時所訪問的路徑與配置的路徑相匹配時轉發該請求 萬用字元、佔位符或具體的介面路徑,可以配置多個
Query 請求時所帶有的引數名稱與配置的引數名稱相符時轉發該請求 引數名稱和引數值(非必須),支援使用正則表示式對引數值進行匹配
RemoteAddr 請求時的IP地址與配置的IP地址相符時轉發該請求 IP地址或IP段

Method 謂詞工廠示例

  • Method謂詞工廠示例,當HTTP請求方法是GET時才轉發
gateway:
  discovery:
    locator:
      # 讓gateway通過服務發現元件找到其他的微服務
      enabled: true
  routes:
    - id: method_route
      uri: lb://user-center
      predicates:
        # 當HTTP請求方法是GET時,才會轉發使用者微服務
        # 如請求方法滿足條件,訪問http://localhost:8040/** -> user-center/**
        # eg. 訪問http://localhost:8040/test/coisini -> user-center/test/coisini
        - Method=GET
  • GET請求測試

  • POST請求測試


自定義路由謂詞工廠

  • 實現限制服務只能在09:00 - 17:00內才可以訪問

程式碼配置

  • application.yml
spring:
  application:
    name: core-gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: user-center
          uri: http://www.baidu.com
          predicates:
            - TimeBetween=上午9:00,下午5:00
  • TimeBetweenConfig.java
import lombok.Data;
import java.time.LocalTime;

@Data
public class TimeBetweenConfig {
    private LocalTime start;
    private LocalTime end;
}
  • TimeBetweenRoutePredicateFactory.java
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * 自定義路由謂詞工廠
 * 路由謂詞工廠必須以RoutePredicateFactory結尾
 */
@Component
public class TimeBetweenRoutePredicateFactory extends AbstractRoutePredicateFactory<TimeBetweenConfig> {

    public TimeBetweenRoutePredicateFactory() {
        super(TimeBetweenConfig.class);
    }

    /**
     * 控制路由的條件
     * @param config
     * @return
     */
    @Override
    public Predicate<ServerWebExchange> apply(TimeBetweenConfig config) {
        LocalTime start = config.getStart();
        LocalTime end = config.getEnd();

        return exchange -> {
            LocalTime now = LocalTime.now();
            return now.isAfter(start) && now.isBefore(end);
        };
    }

    /**
     * 控制配置類和配置檔案的對映關係
     * @return
     */
    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("start", "end");
    }
}

測試

  • 09:00 - 17:00外訪問

  • 09:00 - 17:00內訪問


- End -
白嫖有風險
點贊加收藏
以上為本篇文章的主要內容,希望大家多提意見,如果喜歡記得點個推薦哦 作者:Maggieq8324 出處:https://www.cnblogs.com/maggieq8324/ 本文版權歸作者和部落格園共有,歡迎轉載,轉載時保留原作者和文章地址即可。