RoutePredicateFactories–(SpringCloudGateway’s routing assertion rule test)

Article directory

  • Route Predicate Factories
  • (SpringCloudGateway’s routing assertion rule test)
    • 1 basic configuration
      • 1.1 spring-cloud-gateway version 2.2.9.RELEASE
      • 1.2 gatewayserver/bootstrap.yml
      • 1.3 The actual Consumer interface to be accessed through gateway routing is: localhost:11001/qme or localhost:11002/qme
        • 1.3.1 ProducerFeign
        • 1.3.2 Consumer Feign Controller
        • 1.3.3 ProducerFeignController
    • 2 postman test
      • 2.1 PathRoutePredicateFactory
        • 2.1.1 gateway-service-dev.yaml
        • 2.1.2 Successful request case –>
        • 2.1.3 Request failure case –>
      • 2.2 MethodRoutePredicateFactory
        • 2.2.1 gateway-service-dev.yaml
        • 2.2.2 Successful request case –>
        • 2.2.3 Request failure case –>
      • 2.3 QueryRoutePredicateFactory
        • 2.3.1 gateway-service-dev.yaml
        • 2.3.2 Successful request case –>
        • 2.3.3 Request failure case –>
      • 2.4 RemoteAddrPredicateFactory
        • 2.4.1 gateway-service-dev.yaml
        • 2.4.2 Successful request case –>
        • 2.4.3 Request failure case –>
      • 2.5 Before/After/Between Route PredicateFactory
        • 2.5.1 gateway-service-dev.yaml
        • 2.5.2 Successful request case –>
        • 2.5.3 Request failure case –>
      • 2.6 CookieRoutePredicate
        • 2.6.1 gateway-service-dev.yaml
        • 2.6.2 Successful request case –>
        • 2.6.3 Request failure case –>
      • 2.7 HeaderRoutePredicate
        • 2.7.1 gateway-service-dev.yaml
        • 2.7.2 Successful request case –>
        • 2.7.3 Request failure case –>
      • 2.8 HostRoutePredicate
        • 2.8.1 gateway-service-dev.yaml
        • 2.8.2 Successful request case –>
        • 2.8.3 Request failure case –>
      • 2.9WeigherRoutePredicateFactory
        • 2.9.1 gateway-service-dev.yaml
        • 2.9.2 Fine-tuning for easy observation
        • 2.9.3 Successful request case –>

RoutePredicateFactories

(Routing assertion rule test of SpringCloudGateway)

1 Basic Configuration

1.1 spring-cloud-gateway version 2.2.9.RELEASE

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

1.2 gatewayserver/bootstrap.yml

server:
  port: 13001
spring:
  application:
    name: gateway-service
  profiles:
    active: dev

  cloud:
    nacos:
      #Configuration center
      config:
        server-addr: localhost:8848
        #Extension can only be written as yaml
        file-extension: yaml

1.3 The actual Consumer interface to be accessed through gateway routing is: localhost:11001/qme or localhost:11002/qme

1.3.1 ProducerFeign

@FeignClient("producer-service")//producer service name spring.application.name
public interface Producer Feign {<!-- -->

    @RequestMapping("/api/feign/qme")
    String qme();
}

1.3.2 Consumer FeignController

@RestController
@RefreshScope //Allow this Controller to dynamically identify the configuration modified by the configuration center
public class ConsumerFeignController {<!-- -->

    @Value("${class.number}")
    private String classNo;
    @Resource
    private ProducerFeign producerFeign;

    @RequestMapping("/qme")
    public String qme(){<!-- -->
        System.out.println(classNo);
        String result = producerFeign.qme();
        return result;
    }
}

1.3.3 ProducerFeignController

@RestController
@RequestMapping("/api/feign")
public class ProducerFeignController {<!-- -->
    @Value("${server.port}")
    private int port;

    @RequestMapping("/qme")
    public String qme(){<!-- -->
        return new Date() + "-->" + port;
    }
}

2 postman test

2.1 PathRoutePredicateFactory

2.1.1 gateway-service-dev.yaml

spring:
  cloud:
    gateway:
      routes: # predicate factory and gateway filter
        - id: consumer
          uri: lb://consumer-service #internal protocol lb http:// universal for remote
          predicates:
            - Path=/consumer/** #Allow requests for paths with consumer prefixes to be routed through the gateway
          filters:
            - StripPrefix=1 #The filter removes the first prefix of the path

2.1.2 Successful request case –>

2.1.3 Request failure case –>

without /consumer/

2.2 MethodRoutePredicateFactory

2.2.1 gateway-service-dev.yaml

spring:
  cloud:
    gateway:
      routes: # predicate factory and gateway filter
        - id: consumer
          uri: lb://consumer-service #internal protocol lb http:// universal for remote
          predicates:
            - Method=POST #Allow POST requests to be routed through the gateway

2.2.2 Successful request case –>

2.2.3 Request failure case –>

2.3 QueryRoutePredicateFactory

2.3.1 gateway-service-dev.yaml

spring:
  cloud:
    gateway:
      routes: # predicate factory and gateway filter
        - id: consumer
          uri: lb://consumer-service #internal protocol lb http:// universal for remote
          predicates:
            - RemoteAddr=192.168.0.1/24 #As long as the user in the local area network can request the gateway for routing

2.3.2 Successful request case –>

2.3.3 Request failure case –>

If the specific ip is replaced with localhost, the request rule does not match and the request fails

2.4 RemoteAddrPredicateFactory

2.4.1 gateway-service-dev.yaml

spring:
  cloud:
    gateway:
      routes: # predicate factory and gateway filter
        - id: consumer
          uri: lb://consumer-service #internal protocol lb http:// universal for remote
          predicates:
            - Query=answerMe, hi #Allow requests with key-value pair formal parameters to be routed through the gateway url?answerMe=hi

2.4.2 Successful request case –>

2.4.3 Request failure case –>

2.5 Before/After/Between RoutePredicateFactory

Take BeforeRoutePredicate as an example

2.5.1 gateway-service-dev.yaml

spring:
  cloud:
    gateway:
      routes: # predicate factory and gateway filter
        - id: consumer
          uri: lb://consumer-service #internal protocol lb http:// universal for remote
          predicates:
            #Allow requests to be routed through the gateway before the time of this assertion
            - Before=2023-07-14T21:24:47.789 + 08:00[Asia/Shanghai]

2.5.2 Successful request case –>

2.5.3 Request failure case –>

BetweenRoutePredicate

spring:
  cloud:
    gateway:
      routes: # predicate factory and gateway filter
        - id: consumer
          uri: lb://consumer-service #internal protocol lb http:// universal for remote
predicates:
            #During the time period of this assertion, requests are allowed to route through the gateway
            - Between=2023-07-14T15:35:47.789 + 08:00[Asia/Shanghai], 2023-07-14T16:35:47.789 + 08:00[Asia/Shanghai]

AfterRoutePredicate

spring:
  cloud:
    gateway:
      routes: # predicate factory and gateway filter
        - id: consumer
          uri: lb://consumer-service #internal protocol lb http:// universal for remote
predicates:
            # Allow requests to be routed through the gateway after the time of this assertion
            - After=2023-07-14T16:35:47.789+08:00[Asia/Shanghai]

2.6 CookieRoutePredicate

2.6.1 gateway-service-dev.yaml

spring:
  cloud:
    gateway:
      routes: # predicate factory and gateway filter
        - id: consumer
          uri: lb://consumer-service #internal protocol lb http:// universal for remote
          predicates:
            - Cookie=username, wn #Allow requests with cookies that match key-value pairs to be routed through the gateway

2.6.2 Successful request case –>

2.6.3 Request failure case –>

2.7 HeaderRoutePredicate

2.7.1 gateway-service-dev.yaml

spring:
  cloud:
    gateway:
      routes: # predicate factory and gateway filter
        - id: consumer
          uri: lb://consumer-service #internal protocol lb http:// universal for remote
          predicates:
            - Header=token, 112233 #Allow requests with token=112233 in the request header to be routed through the gateway

2.7.2 Successful request case –>

2.7.3 Request failure case –>

2.8 HostRoutePredicate

2.8.1 gateway-service-dev.yaml

spring:
  cloud:
    gateway:
      routes: # predicate factory and gateway filter
        - id: consumer
          uri: lb://consumer-service #internal protocol lb http:// universal for remote
          predicates:
            - Host=**.wn.com:13001 #Allow host requests with port xxx.wn.com:13001 to be routed through the gateway

2.8.2 Successful request case –>

2.8.3 Request failure case –>

2.9 WeigherRoutePredicateFactory

2.9.1 gateway-service-dev.yaml

spring:
  cloud:
    gateway:
      routes:
        - id: consumer_high
          uri: http://localhost:11001
          predicates:
            - Weight=consumer, 8
        - id: consumer_low
          uri: http://localhost:11002
          predicates:
            - Weight=consumer, 2 #Allow the gateway to distribute requests to different nodes according to the weight setting

2.9.2 Make fine-tuning for easy observation

@RestController
@RefreshScope //Allow this Controller to dynamically identify the configuration modified by the configuration center
public class ConsumerFeignController {<!-- -->

    //@Value("${class.number}")
    //private String classNo;
    private int count;
    @Resource
    private ProducerFeign producerFeign;

    @RequestMapping("/qme")
    public String qme(){<!-- -->
        System.out.println(count++);
// System.out.println(classNo);
        String result = producerFeign.qme();
        return result;
    }
}

2.9.3 Request success stories –>

Number of requests received by a node with a weight of 8

Number of requests received by a node with a weight of 2