inConstructs the Route, Predicate, and Filter of SCG initial resolutionWe have already seenRouteDefinitionLocator, let’s take a look at the class relationships involved in the two interfaces.

RouteDefinitionLocator

RouteDefinitionLocator interface has only one method, #getRouteDefinitions, which is used to obtain all RouteDefinitions. In section 1, we learned that routing information can be obtained from configuration files. And the corresponding implementation classes for PropertiesRouteDefinitionLocator, SCG actually also supports obtain routing information from other sources, such as the allocation center, Behind the corresponding class of DiscoveryClientRouteDefinitionLocator (article will in detail explain), can be seen from the official documentation of SCG in future versions will be based on the database (such as Redis, directing and Cassandra) for routing, In 2.2.6. RELEASE version, you can see there is a interface RouteDefinitionRepository, but only one implementation class InMemoryRouteDefinitionRepository, estimate future versions will be added based on the realization of the database.

RouteDefinitionRepository

InMemoryRouteDefinitionRepository

InMemoryRouteDefinitionRepository RouteDefinitionRepository interface is achieved, RouteDefinitionRepository inherited RouteDefinitionLocator and RouteDefinitionWriter, defines two methods in RouteDefinitionWriter save and delete, Used to add or delete routing information.

InMemoryRouteDefinitionRepository route definition of warehouse is based on memory, and the only provides the implementation of the class. We can customize the extension as needed and store it in other storage media.

public interface RouteDefinitionWriter {

	Mono<Void> save(Mono<RouteDefinition> route);

	Mono<Void> delete(Mono<String> routeId);

}
Copy the code

CompositeRouteDefinitionLocator

This is mainly used to our routing information from different sources together for RouteDefinitionRouteLocator to use.

CachingRouteDefinitionLocator

Look at the name is used for caching, but this version is not used.

RouteLocator



RouteLocatorThere is only one method in the interfacegetRoutes

public interface RouteLocator {

	Flux<Route> getRoutes();

}
Copy the code

The method name indicates that it is used to obtain all routes.

RouteDefinitionRouteLocator

RouteDefinitionRouteLocator is used for assembling a RouteDefinitionLocator, GatewayFilterFactory and RoutePredicateFactory and generates the Route, RouteDefinitionLocator is CompositeRouteDefinitionLocator

Custom RouteLocator

Customize routing information through the RouteLocatorBuilder, the SCG API driver.

CompositeRouteLocator

Used to RouteDefinitionRouteLocator and custom RouteLocator combination merger.

CachingRouteLocator

By its name, it does caching, so how do you do caching? In the above CompositeRouteLocator getRoutes method, is called RouteDefinitionRouteLocator or custom RouteLocator getRoutes method, But RouteDefinitionRouteLocator during initialization will Route, therefore is called when initial CachingRouteLocator each RouteLocator getRoutes assembled all the Route and cache, For RoutePredicateHandlerMapping calls.

@Bean
@Primary
@ConditionalOnMissingBean(name = "cachedCompositeRouteLocator")
public RouteLocator cachedCompositeRouteLocator(List<RouteLocator> routeLocators) {
	return new CachingRouteLocator(
			new CompositeRouteLocator(Flux.fromIterable(routeLocators)));
}
Copy the code

conclusion

RouteDefinitionLocator is used to gather routing information from different sources. A RouteDefinitionLocator can directly define routes. Can also access all of the configuration by RouteDefinitionLocator RouteDefinition, eventually converted to the Route for the caller RoutePredicateHandlerMapping acquisition.