在SpringCloud Gateway中,路由定位器是查找请求连接的作用,我们通过源码看下,在SpringCloud Gateway中,路由定位器是如何工作的。
需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码
一、RouteLocator
SpringCloud gateway的路由定位器,包括CachingRouteLocator,CompositeRouteLocator,RouteDefinitionRouteLocator,他们的关系可以从GatewayAutoConfiguration中的代码看出, public RouteLocator routeDefinitionRouteLocator(GatewayProperties properties, List<GatewayFilterFactory> GatewayFilters, List<RoutePredicateFactory> predicates, RouteDefinitionLocator routeDefinitionLocator) { return new RouteDefinitionRouteLocator(routeDefinitionLocator, predicates, GatewayFilters, properties);}@Bean@Primary//TODO: property to disable composite?public RouteLocator cachedCompositeRouteLocator(List<RouteLocator> routeLocators) { return new CachingRouteLocator(new CompositeRouteLocator(Flux.fromIterable(routeLocators)));}就是CachingRouteLocator中包含了CompositeRouteLocator,CompositeRouteLocator中包含了RouteDefinitionRouteLocator类型,就是一个请求过来获取Route的入口类型就是CachingRouteLocator的getRoutes方法。同时这个类型还支持监听zk节点变更事件,刷新缓存 二、RouteDefinitionLocator
而RouteDefinitionRouteLocator定位器是通过RouteDefinitionLocator的RouteDefinition定位器获取定义的数据的。 有三种类型的RouteDefinitionLocator:DiscoveryClientRouteDefinitionLocator,DiscoveryClientRoutDefinitionRepository,PropertiesRouteDefinitionLocator。 其中,DiscoveryClientRoutDefinitionRepository使用的是DiscoveryClientRouteDefinitionLocator定位器,是从注册中心获取路由信息,PropertiesRouteDefinitionLocator是从配置文件中获取路由信息。 三、时序图如下
Java B2B2C多用户商城 springboot架构
|