Gateway service gateway assertion factory

1. What is a service gateway?


The service gateway is the second-generation gateway framework officially launched by Spring Cloud. It is used to replace the first-generation gateway Netflix Zuul. It not only provides routing, but also provides the basic functions of the gateway based on the Filter chain; the service gateway serves as the entire distributed system. The traffic entrance of the service plays a decisive role, such as:

  • Protocol conversion, routing and forwarding
  • Traffic aggregation, traffic monitoring, log output
  • Traffic control at the gateway layer
  • Perform permission verification at the gateway layer


2. Implementation Principle of Service Gateway


The core components of Gateway are routing and filters. Different from Netflix Zuul, Gateway has an additional assertion to determine which Gateway web handler the request should be handed over for processing. The process is as follows

First, the request is handed over to the Gateway handler mapping for processing. If the request matches the assertion, the request is handed over to the Gateway web handler for processing. When the Gateway web handler processes a request, it will go through a series of Filter chains. As shown in the figure, the filter chain is divided into two by a dotted line. The left half is processed before the filter sends the proxy request, that is, the pre stage. Executing the left half can generally achieve current limiting, log output, authentication, etc. The right half is processed after the request, that is, the post stage. Executing the right half can modify the response data, such as changing the response header and converting the protocol, etc.;

3. Assertion Factory

When a request arrives, it is first handed over to the assertion factory for processing. If the assertion factory is hit, the next step is executed, otherwise an error message is returned; the service gateway Gateway has multiple built-in assertion factories, as follows:

1. AfterRoutePredicateFactory assertion factory

spring:
    cloud:
        gateway:
            routes:
                -id: after_routes # 路由id,要保证id唯一。
                 uri: lb://alibaba-nacos # 请求转发路径
                 predicate:
                    -After= 2010-01-20T17:42:47.789-07:00[America/Denver] # 表示服务只能在配置日期后访问,并转发至http://alibaba-nacos/xxx

2、BeforeRoutePredicateFactory

spring:
    cloud:
        gateway:
            routes:
                -id: before_routes # 路由id,要保证id唯一。
                 uri: lb://alibaba-nacos # 请求转发路径
                 predicate:
                    -Before= 2010-01-20T17:42:47.789-07:00[America/Denver] # 表示服务只能在配置日期前访问,并转发至http://alibaba-nacos/xxx

3、BetweenRoutePredicateFactory

spring:
    cloud:
        gateway:
            routes:
                -id: between_routes # 路由id,要保证id唯一。
                 uri: lb://alibaba-nacos # 请求转发路径
                 predicate:
                    -Between= 2017-01-20T17:42:47.789-07:00[America/Denver], 2027-01-21T17:42:47.789-07:00[America/Denver]  # 表示服务只能在配置日期时间段访问,并转发至http://alibaba-nacos/xxx

Note: Regarding the time assertion factory, a tricky point when setting it up is to print the time including the time zone through ZonedDateTime.now()

4、CookieRoutePredicateFactory

spring:
  cloud:
    gateway:
      routes:
        - id: cookie_route
          uri: lb://alibaba-nacos
          predicates:
            # 当且仅当带有名为somecookie,并且值符合正则ch.p的Cookie时,才会转发到用户微服务
            # 如Cookie满足条件,则访问http://localhost:8040/** -> user-center/**
            # eg. 访问http://localhost:8040/users/1 -> user-center/users/1
            - Cookie=somecookie, ch.p

5、HeaderRoutePredicateFactory

spring:
  cloud:
    gateway:
      routes:
        - id: header_route
          uri: lb://alibaba-nacos
          predicates:
            # 当且仅当带有名为X-Request-Id,并且值符合正则\d+的Header时,才会转发到用户微服务
            # 如Header满足条件,则访问http://localhost:8040/** -> user-center/**
            # eg. 访问http://localhost:8040/users/1 -> user-center/users/1
            - Header=X-Request-Id, \d+

6、HostRoutePredicateFactory

spring:
  cloud:
    gateway:
      routes:
        - id: host_route
          uri: lb://alibaba-nacos
          predicates:
            # 当且仅当名为Host的Header符合**.somehost.org或**.anotherhost.org时,才会转发用户微服务
            # 如Host满足条件,则访问http://localhost:8040/** -> user-center/**
            # eg. 访问http://localhost:8040/users/1 -> user-center/users/1
            - Host=**.somehost.org,**.anotherhost.org

7、MethodRoutePredicateFactory

		
 
spring:
  cloud:
    gateway:
      routes:
        - id: method_route
          uri: lb://alibaba-nacos
          predicates:
            # 当且仅当HTTP请求方法是GET时,才会转发用户微服务
            # 如请求方法满足条件,访问http://localhost:8040/** -> user-center/**
            # eg. 访问http://localhost:8040/users/1 -> user-center/users/1
            - Method=GET

8、PathRoutePreidicateFactory

spring:
  cloud:
    gateway:
      routes:
        - id: path_route
          uri: lb://alibaba-nacos
          predicates:
            # 当且仅当访问路径是/users/*或者/some-path/**,才会转发用户微服务
            # segment是一个特殊的占位符,单层路径匹配
            # eg. 访问http://localhost:8040/users/1 -> user-center/users/1
            - Path=/users/{segment},/some-path/**

9、QueryRoutePredicateFactory

 
spring:
  cloud:
    gateway:
      routes:
        - id: query_route
          uri: lb://user-center
          predicates:
            # 当且仅当请求带有名为foo的参数,且参数值符合正则ba.,才会转发到用户微服务
            # eg. 访问http://localhost:8040/users/1?baz=baz -> user-center的/users/1?baz=baz
            - Query=foo, ba.

10、RemoteaddRoutePredicateFactory

spring:
  cloud:
    gateway:
      routes:
        - id: remoteaddr_route
          uri: lb://user-center
          predicates:
            # 当且仅当请求IP是192.168.1.1/24网段,例如192.168.1.10,才会转发到用户微服务
            # eg. 访问http://localhost:8040/users/1 -> user-center的/users/1
            - RemoteAddr=192.168.1.1/24

4. Custom assertion factory

If the system assertion factory provided above does not meet the business needs, then we can implement a custom assertion factory. By viewing the source code, we know that the above default assertion factories all inherit from AbstractRoutePredicateFactory. Then if we want to implement the assertion factory, we first need to inherit AbstractRoutePredicateFactory. , and it should be noted that: the suffix of the custom assertion factory class name must be xxxRoutePredicateFactory;

Let's implement a service that can only be accessed during a specified time period every day. The specific implementation is as follows:
 

package com.corn.gateway;
 
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
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.List;
import java.util.function.Predicate;
 
/**
 * 自定义gateway 断言
 * @Author corn
 * @Date 2021/3/23 14:37
 */
@Slf4j
@Component
public class TimeBetweenRoutePredicateFactory extends AbstractRoutePredicateFactory<TimeBetweenRoutePredicateFactory.TimeBetweenConfig> {
 
    public TimeBetweenRoutePredicateFactory() {
        super(TimeBetweenConfig.class);
    }
 
    /**
     *@描述 gateway 断言具体执行逻辑
     *@参数
     *@返回值
     *@创建人  corn
     *@创建时间  2021/3/23
     */
    @Override
    public Predicate<ServerWebExchange> apply(TimeBetweenConfig config) {
        LocalTime start = config.getStart();
        LocalTime end = config.getEnd();
        return exchange->{
          return LocalTime.now().isAfter(start)&&LocalTime.now().isBefore(end);
        };
    }
 
    /**
     *@描述 用来将配置文件中的配置和TimeBetweenConfig 中的属性做映射
     *@参数
     *@返回值
     *@创建人  corn
     *@创建时间  2021/3/23
     */
    @Override
    public List<String> shortcutFieldOrder() {
        return Lists.newArrayList("start","end");
    }
 
  
    /**
     *@描述 设置参数
     *@参数 
     *@返回值 
     *@创建人  corn
     *@创建时间  2021/3/29
     */
    @Data
    protected  static class TimeBetweenConfig {
        private LocalTime start;
        private LocalTime end;
    }
 
    public static void main(String[] args) {
        // 获取gateway 支持的时间格式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
        String format = dateTimeFormatter.format(LocalTime.now());
        System.out.println(format);
    }
}

 The configuration is as follows:

spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: lb://alibaba-nacos
          predicates:
            - TimeBetween=上午5:00,下午11:59  # 这样就实现了我们的服务只能每天的早上5点到晚上的11:59 运行访问,其他时间段是不允许访问的

Summarize

This section describes the background, components, principles and assertion factory of the service gateway. The assertion actually determines whether the request is routed to the Router, and then after the assertion is processed, it will enter the filter chain for processing in the Filter logic.

Guess you like

Origin blog.csdn.net/Isonion/article/details/132731626