先粗略熟悉一部分相关的Bean的功能,对于后续的 源码整体流程阅读 会有一定的帮助,个人习惯。
/** * @author Spencer Gibb * @author Ziemowit Stolarczyk */ @Configuration(proxyBeanMethods = false) @ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true) @EnableConfigurationProperties //这四个配置类后面讲解。 //加载这两个之前 先加载本类, @AutoConfigureBefore({ HttpHandlerAutoConfiguration.class, WebFluxAutoConfiguration.class }) //加载这两个之后,在加载本类, @AutoConfigureAfter({ GatewayLoadBalancerClientAutoConfiguration.class, GatewayClassPathWarningAutoConfiguration.class }) @ConditionalOnClass(DispatcherHandler.class) public class GatewayAutoConfiguration { //比较简单:时间 String类型转成 ZonedDateTime类型 @Bean public StringToZonedDateTimeConverter stringToZonedDateTimeConverter() { return new StringToZonedDateTimeConverter(); } //路由定位构造器,猜测用来构造路由的,先记录着 @Bean public RouteLocatorBuilder routeLocatorBuilder( ConfigurableApplicationContext context) { return new RouteLocatorBuilder(context); } //猜测也是跟Route相关的Bean, 属性路由定义器,读取yml配置属性相关的 @Bean @ConditionalOnMissingBean public PropertiesRouteDefinitionLocator propertiesRouteDefinitionLocator( GatewayProperties properties) { return new PropertiesRouteDefinitionLocator(properties); } // 在内存注册的路由定义的Bean @Bean @ConditionalOnMissingBean(RouteDefinitionRepository.class) public InMemoryRouteDefinitionRepository inMemoryRouteDefinitionRepository() { return new InMemoryRouteDefinitionRepository(); } //看名字 是定义了一个复合的路由定义定位 //即把 InMemoryRouteDefinitionRepository,PropertiesRouteDefinitionLocator合成一个 //CachingRouteDefinitionLocator,DiscoveryClientRouteDefinitionLocator这两个暂时没有看到生成的地方 @Bean @Primary public RouteDefinitionLocator routeDefinitionLocator( List<RouteDefinitionLocator> routeDefinitionLocators) { return new CompositeRouteDefinitionLocator( Flux.fromIterable(routeDefinitionLocators)); } //配置服务Bean,依赖 ConversionService转换服务(webFluxConversionService),与验证器 @Bean public ConfigurationService gatewayConfigurationService(BeanFactory beanFactory, @Qualifier("webFluxConversionService") ObjectProvider<ConversionService> conversionService, ObjectProvider<Validator> validator) { return new ConfigurationService(beanFactory, conversionService, validator); } /** *GatewayProperties properties gateway的配置Bean *List<GatewayFilterFactory> gatewayFilters 所有的过滤器,所有的实现在 学习2中有说明 *List<RoutePredicateFactory> predicates 路由断言工厂,所有的实现在 学习2中有说明 *RouteDefinitionLocator routeDefinitionLocator :这里注入的应该是 CompositeRouteDefinitionLocator的Bean,因为它是@primary的 * ConfigurationService configurationService 就是上面的ConfigurationService * 由以上属性,生成一个 RouteDefinitionRouteLocator, * RouteLocator 这个接口主要是一个 Flux<Route> getRoutes(); 即获取所有的路由 **/ @Bean public RouteLocator routeDefinitionRouteLocator(GatewayProperties properties, List<GatewayFilterFactory> gatewayFilters, List<RoutePredicateFactory> predicates, RouteDefinitionLocator routeDefinitionLocator, ConfigurationService configurationService) { return new RouteDefinitionRouteLocator(routeDefinitionLocator, predicates, gatewayFilters, properties, configurationService); } @Bean @Primary @ConditionalOnMissingBean(name = "cachedCompositeRouteLocator") // TODO: property to disable composite? public RouteLocator cachedCompositeRouteLocator(List<RouteLocator> routeLocators) { return new CachingRouteLocator( new CompositeRouteLocator(Flux.fromIterable(routeLocators))); } //路由刷新的 监听器 @Bean @ConditionalOnClass( name = "org.springframework.cloud.client.discovery.event.HeartbeatMonitor") public RouteRefreshListener routeRefreshListener( ApplicationEventPublisher publisher) { return new RouteRefreshListener(publisher); } //FilteringWebHandler webHanlder的过滤器,依赖 GlobalFilter全局过滤器 @Bean public FilteringWebHandler filteringWebHandler(List<GlobalFilter> globalFilters) { return new FilteringWebHandler(globalFilters); } // 解读 spring.cloud.gateway.globalcors.*的配置的Bean @Bean public GlobalCorsProperties globalCorsProperties() { return new GlobalCorsProperties(); } //路由断言 处理的Mapping @Bean public RoutePredicateHandlerMapping routePredicateHandlerMapping( FilteringWebHandler webHandler, RouteLocator routeLocator, GlobalCorsProperties globalCorsProperties, Environment environment) { return new RoutePredicateHandlerMapping(webHandler, routeLocator, globalCorsProperties, environment); } // 解析配置 spring.cloud.gateway.* 的Bean @Bean public GatewayProperties gatewayProperties() { return new GatewayProperties(); } // ConfigurationProperty beans // 省略一大部分 自带的功能Bean 。。。。。。。。。。 } //这是 跟Hystrix 相关的,暂时不解读 @Configuration(proxyBeanMethods = false) @ConditionalOnClass({ HystrixObservableCommand.class, RxReactiveStreams.class }) protected static class HystrixConfiguration { @Bean @ConditionalOnEnabledFilter public HystrixGatewayFilterFactory hystrixGatewayFilterFactory( ObjectProvider<DispatcherHandler> dispatcherHandler) { return new HystrixGatewayFilterFactory(dispatcherHandler); } @Bean @ConditionalOnMissingBean(FallbackHeadersGatewayFilterFactory.class) @ConditionalOnEnabledFilter public FallbackHeadersGatewayFilterFactory fallbackHeadersGatewayFilterFactory() { return new FallbackHeadersGatewayFilterFactory(); } } // actuator 这个配置暂时不知道 是什么功能,不研究 @Configuration(proxyBeanMethods = false) @ConditionalOnClass(Health.class) protected static class GatewayActuatorConfiguration { @Bean @ConditionalOnProperty(name = "spring.cloud.gateway.actuator.verbose.enabled", matchIfMissing = true) @ConditionalOnAvailableEndpoint public GatewayControllerEndpoint gatewayControllerEndpoint( List<GlobalFilter> globalFilters, List<GatewayFilterFactory> gatewayFilters, List<RoutePredicateFactory> routePredicates, RouteDefinitionWriter routeDefinitionWriter, RouteLocator routeLocator, RouteDefinitionLocator routeDefinitionLocator) { return new GatewayControllerEndpoint(globalFilters, gatewayFilters, routePredicates, routeDefinitionWriter, routeLocator, routeDefinitionLocator); } @Bean @Conditional(OnVerboseDisabledCondition.class) @ConditionalOnAvailableEndpoint public GatewayLegacyControllerEndpoint gatewayLegacyControllerEndpoint( RouteDefinitionLocator routeDefinitionLocator, List<GlobalFilter> globalFilters, List<GatewayFilterFactory> gatewayFilters, List<RoutePredicateFactory> routePredicates, RouteDefinitionWriter routeDefinitionWriter, RouteLocator routeLocator) { return new GatewayLegacyControllerEndpoint(routeDefinitionLocator, globalFilters, gatewayFilters, routePredicates, routeDefinitionWriter, routeLocator); } } private static class OnVerboseDisabledCondition extends NoneNestedConditions { OnVerboseDisabledCondition() { super(ConfigurationPhase.REGISTER_BEAN); } @ConditionalOnProperty(name = "spring.cloud.gateway.actuator.verbose.enabled", matchIfMissing = true) static class VerboseDisabled { } } }