springcloud gateway dynamic routing

1.1 Introduction to springcloud gateway

Spring Cloud Gateway is the API Gateway in the SpringCloud microservice system produced by Spring. It is an efficient non-blocking gateway built based on Spring5, Project Reactor, and WebFlux. It provides the following functions:

a. All request attributes can be used as routing conditions (Routing Predicates)

b. Provides a gateway filter (Gateway Filter) that acts on specified routes

c. Provides a global filter (Global filter) that applies to all routes

d. Provides circuit breaker integration (Circuit Breaker)

e. Provides service discovery integration (Discovery Client)

f. Provides rate limiting component integration (Rate Limiting)

g.Path Rewriting

1.2 Working principle of Gateway

The working principle of Gateway is as shown below (mainly involving the red box part)

Picture

The client request will first be processed by Gateway Handler Mapping to find a route matching the request in the routing table, and then the request will be handed over to the Web Handler for processing. The Web Handler maintains a filter chain and executes these filters in a chain. , these filters logically have two execution stages, pre and post.

Core classRoutePredicateHandlerMapping

Picture

1.3 Dynamic routing

What is dynamic routing

Dynamic routing: Without restarting the gateway application, routes can be added through the management API or management UI, which can take effect in real-time or quasi-real-time; and after the gateway application is restarted, the dynamically added routes still exist.

There are two basic requirements for dynamic routing: real-time and persistence.

1.4 Route definition

The attributes of Route are:

id, the unique number of the route can be used to define a route and is used in gateway. Some filters will perform specific operations based on whether the id belongs to a certain set;

order, the priority of the route, the smaller the value, the higher the priority (the routing table is sorted according to this);

metadata, which can define parameters such as connection timeout and response timeout, and its use may be expanded in the future;

uri, the upstream address, i.e. upstream, can be a certain service address or an address starting with lb, which means that the upstream node can be discovered through service discovery;

filters, filter list, after the request hits a route based on the assertion, the filters in the filter list will be executed in sequence;

predicate, route assertion, used to determine whether the route matches the current route.

Picture

1.5 How route assertion works

Intercept part of the source code of the core class

a.RoutePredicateHandlerMapping

Picture

b.2.RouteDefinitionRouteLocator

Picture

combinePredicates is combined into a route assertion based on the assertion definition in the route definition.

Picture

Obtains an instantiated assertion from the specified assertion factory based on the assertion definition in the route definition.

1.6 Route Assertion Factory

Whether a request hits a certain route is ultimately determined by the applyAsync method of the assertion factory corresponding to the assertion definition in the route definition.

Picture

RoutePredicateFactory

Picture

The apply of each factory implements how to determine whether the current request matches the assertion based on the configuration and ServerWebExchange (if you need to customize the assertion factory, you also need to implement this logic)

Paste here the official documentation and configuration examples https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gateway-request-predicates-factories;

Read more official documents, they are very detailed and complete.

Picture

The keyword Path indicates that the PathRoutePredicateFactory factory is used to create the assertion;

Path is followed by comma-separated ordered string data, indicating that as long as any path is matched, the assertion is true (see the apply method) and the route is hit. The Name of the assertion definition is the prefix string of the assertion factory.

1.7 Extensions

When the number of clusters reaches a certain level, you need to consider the performance bottleneck of routing:

a. The impact of the number of routes on performance

b. Routing and forwarding performance

I won’t expand on it because I have no experience with such performance issues in production, so I would like to explore it.