process

We use official pictures to illustrate the Gateway process. When the client access gateway, will first through DispatcherHandler# RoutePredicateHandlerMapping handle calls, is the gateway of figure Handler Mapping, Then call the FilteringWebHandler, which is the Gateway WebHandler in the diagram.

We learn fromJava8 seriesKnow, java8 Predicate function, used to represent a Boolean expressions involving type T, RoutePredicateHandlerMapping is also similar, when returns false, that do not conform to the requirements, so he is primarily used to determine whether the routing match, Returns false if it does not match.

FilteringWebHandler is a filter handler by name. As you can see from the graph, it will have a filter chain that handles pre – and post-request operations, similar to Zuul’s pre and post.

Route

Now that we have seen the Gateway process above, suppose we have multiple URLs, such as example.com/testA and a sign, how do we know which requests are acceptable and which are not? We can store these URLs in a large map, but how do we filter a specific URL, so this kind of coarse-grained control is not possible. So the Gateway is designed in such a way that it configures rules and routes for each URL. As shown in the figure below, each Route has its own ID, URL, predicate, and gatewayFilters.



Predicates can only use one predicate. It doesn’t. Java 8’s predicate has methods for OR, AND, and negate, which it does. So here’s how it works, when we only have one Predicate:



When the and method is called, so that we have two predicates, it is structured as follows. It places the existing Predicate in the left and the new Predicate in the right.



When the and method is called again, there are three predicates, which are structured as follows:



The same is true for calling or, which uses this structure to implement the same methods as Java 8’s Predicate.

RouteDefinition

Predicate can be configured using RouteDefinition (RouteDefinition). The RouteDefinition can be configured using RouteDefinition (RouteDefinition). The RouteDefinition can be configured using RouteDefinition (RouteDefinition).



As you can see, the difference between Route and Route is the difference between predicates and predicate. Each object in the collection has a name and a map, and the map is used to store the various key-value pairs that are used to determine whether or not they meet the criteria for filtering. For example, our configuration file could say:

spring: application: name: gateway cloud: gateway: routes: - id: route1 uri: https://www.examplea.com predicates: -After =2017-01-20T17:42:47.789-07:00[America/Denver] -Cookie = Chocolate, ch.p filters: -After =2017-01-20T17:42:47.789-07:00[America/Denver] -Cookie = Chocolate, ch.p filters: - AddRequestHeader=X-Request-red, blue - AddRequestParameter=red, blue - id: route2 uri: https://www.exampleb.com predicates: - Path=/red/{segment},/blue/{segment} - Query=green filters: - AddResponseHeader=X-Response-Red, Blue - RewritePath=/consumingserviceendpoint, /backingserviceendpoint

RoutePredicateFactory

We have already used RouteDefinition to define the configuration information of the corresponding configuration file. The RouteDefinition will eventually become Route, and the configured predicates, filters, and so on will eventually become Route properties. Within the Gateway, you use the RoutePredicateFactory factory class to generate individual Predicates from the configuration information. We’ve defined a dozen RoutePredicateFactories and put them in the Map cache. The key is to remove the RoutePredicateFactory from the class name, such as the Path we configured above. He can know the factory class is PathRoutePredicateFactory. As many predicates as you configure, the corresponding factory class creates the predicates for you, and then assembles them into the Route’s Predicate structure, which is shown above.

GatewayFilterFactory

Since Predicate has its own factory class that generates different predicates based on configuration information, the same is true for filters. The system also defines a number of GatewayFilterFactory, put in the map cache, his key is to remove the GatewayFilterFactory class name. For instance the AddResponseHeader configuration above, we can know that his factory class is AddResponseHeaderGatewayFilterFactory. The design is the same as RoutePredicateFactory.

Locator

So a Locator is a Route and a routeDefinitionLocator, which is a Route and a routeDefinition. That is to say, they are used to locate specific Route and RouteDefinition.