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 -
白嫖有風險
點贊加收藏
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章