Springcloud distributed micro service E-commerce mall learn from me the common routing assertion factory of springcloud gateway

Posted by nakins on Wed, 09 Mar 2022 13:21:08 +0100

Spring cloud Gateway has built-in many routing assertion factories, which can be used directly through configuration or combined with multiple routing assertion factories. Next, we will introduce several common routing assertion factory classes.

1) Path route assertion factory

The Path route assertion factory receives a parameter and determines whether the accessed URI matches according to the rules defined by Path.

spring:
  cloud:
    gateway:
      routes:
        - id: host_route
    uri: http://minglisoft.cn
    predicates:
      - Path=/blog/detail/{segment}

If the request path is / blog/detail/xxx, this route will match. You can also use regular, such as / blog/detail / * * to match multi-level URI s starting with / blog/detail /.

We access the local gateway: http://localhost : 2001/blog/detail/36185, you can see that http://minglisoft.cn/blog/detail/36185 Corresponding content.

2) Query route assertion factory

The Query route assertion factory receives two parameters, a required parameter and an optional regular expression.

spring:
  cloud:
    gateway:
      routes:
        - id: query_route
      uri: http://minglisoft.cn
      predicates:
        - Query=foo, ba.

If the request contains a foo query parameter whose value matches ba, the route will match. bar and baz will also match, because the second parameter is a regular expression.

Test link: http://localhost : 2001/?foo=baz.

3) Method route assertion factory

The Method route assertion factory receives a parameter, that is, the HTTP Method to be matched.

spring:
  cloud:
    gateway:
      routes:
        - id: method_route
  uri: http://baidu.com
  predicates:
    - Method=GET

4) Header route assertion factory

The Header route assertion factory receives two parameters: the request Header name and the regular expression.

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

If the request has a request header named x-request-id, and its value matches the \ d + regular expression (the value is one or more numbers), then this route matches.

If you want to learn more about the usage of route assertion factory, you can refer to the official documentation for learning.

Custom route assertion factory

The custom route assertion factory needs to inherit the AbstractRoutePredicateFactory class and override the logic of the apply method.

In the apply method, you can use exchange Getrequest () gets the ServerHttpRequest object, so you can get the request parameters, request method, request header and other information.

The parameters of the apply method are user-defined configuration classes. When using, configure the parameters and obtain them directly in the apply method.

The name needs to end with RoutePredicateFactory, such as CheckAuthRoutePredicateFactory. When used, CheckAuth is the name of the route assertion factory. The code is shown below.

@Component
public class CheckAuthRoutePredicateFactory
        extends AbstractRoutePredicateFactory<CheckAuthRoutePredicateFactory.Config> {
    public CheckAuthRoutePredicateFactory() {
        super(Config.class);
    }

    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        return exchange -> {
            System.err.println("Entered CheckAuthRoutePredicateFactory\t" + config.getName());
            if (config.getName().equals("zhangsan")) {
                return true;
            }
            return false;
        };
    }

    public static class Config {
        private String name;

        public void setName(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }
}

Use examples are as follows:

spring:
  cloud:
    gateway:
      routes:
  - id: customer_route
  uri: http://minglisoft.cn
  predicates:
    - name: CheckAuth
  args:
    name: zhangsan

Recommended e-commerce source code

Topics: Spring Cloud