-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1.启动加载流程
91 lines (81 loc) · 5.86 KB
/
1.启动加载流程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
spring-cloud-gateway-sample项目启动,
需要添加注释@EnableAutoConfiguration默认加载spring-could-gateway-core里的spring-factories里的配置默认启动类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.gateway.config.GatewayClassPathWarningAutoConfiguration,\
org.springframework.cloud.gateway.config.GatewayAutoConfiguration,\
org.springframework.cloud.gateway.config.GatewayLoadBalancerClientAutoConfiguration,\
org.springframework.cloud.gateway.config.GatewayRedisAutoConfiguration,\
org.springframework.cloud.gateway.discovery.GatewayDiscoveryClientAutoConfiguration
在以上自动加载配置的类中,GatewayClassPathWarningAutoConfiguration,GatewayRedisAutoConfiguration, GatewayDiscovRibbonAutoConfiguratioeryClientAutoConfiguration
都是在GatewayAutoConfiguration加载之后再加载,GatewayLoadBalancerClientAutoConfiguration加载之后记载RibbonAutoConfiguration类。
下面看下GatewayAutoConfiguration类的加载逻辑
注解如下:
@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true)
应用环境中需要有spring.cloud.gateway.enabled配置,但不是必须的
@EnableConfigurationProperties
应用环境中有带有@ConfigurationProperties的bean提供有效的支持
@AutoConfigureBefore(HttpHandlerAutoConfiguration.class)
加载GatewayAutoConfiguration类前先加载HttpHandlerAutoConfiguration的bean这里用到webflux
@AutoConfigureAfter({GatewayLoadBalancerClientAutoConfiguration.class, GatewayClassPathWarningAutoConfiguration.class})
加载完GatewayAutoConfiguration类之后初始化GatewayLoadBalancerClientAutoConfiguration,GatewayClassPathWarningAutoConfiguration类
@ConditionalOnClass(DispatcherHandler.class)
类加载中存在DispatcherHandler.class,才初始化这个bean
代码如下:
1.初始化NettyConfiguration这个bean,同时检查类路径中存在HttpClient.class,
并在内部初始化HttpClient,Consumer,HttpClientProperties,NettyRoutingFilter,NettyWriteResponseFilter,ReactorNettyWebSocketClient
2.初始化RouteLocatorBuilder,PropertiesRouteDefinitionLocator(没有则创建),
InMemoryRouteDefinitionRepository(RouteDefinitionRepository(没有则创建)),RouteDefinitionLocator,
RouteLocator(实际初始化bean对象为RouteDefinitionRouteLocator,CachingRouteLocator(首选bean)),RouteRefreshListener,
FilteringWebHandler,RoutePredicateHandlerMapping
//ConfigurationProperty beans
GatewayProperties,SecureHeadersProperties
// HttpHeaderFilter beans
ForwardedHeadersFilter,RemoveHopByHopHeadersFilter,XForwardedHeadersFilter
// GlobalFilter beans
AdaptCachedBodyGlobalFilter,RouteToRequestUrlFilter,ForwardRoutingFilter,ForwardPathFilter,WebsocketRoutingFilter,WeightCalculatorWebFilter
// Predicate Factory beans
// 1.路由Filter Facatory
AfterRoutePredicateFactory,BeforeRoutePredicateFactory,BetweenRoutePredicateFactory,CookieRoutePredicateFactory,HeaderRoutePredicateFactory,
HostRoutePredicateFactory,MethodRoutePredicateFactory,PathRoutePredicateFactory,QueryRoutePredicateFactory,ReadBodyPredicateFactory,
RemoteAddrRoutePredicateFactory,WeightRoutePredicateFactory,
// 2.WebFilter Facatory
AddRequestHeaderGatewayFilterFactory,AddRequestParameterGatewayFilterFactory,AddResponseHeaderGatewayFilterFactory
ModifyRequestBodyGatewayFilterFactory,ModifyResponseBodyGatewayFilterFactory,PrefixPathGatewayFilterFactory,
PreserveHostHeaderGatewayFilterFactory,RedirectToGatewayFilterFactory,RemoveRequestHeaderGatewayFilterFactory,
RemoveResponseHeaderGatewayFilterFactory,SetPathGatewayFilterFactory,SecureHeadersGatewayFilterFactory,
SetRequestHeaderGatewayFilterFactory,SetResponseHeaderGatewayFilterFactory,SetStatusGatewayFilterFactory,
SaveSessionGatewayFilterFactory,StripPrefixGatewayFilterFactory,RequestHeaderToRequestUriGatewayFilterFactory
// 3.熔断,限流,重写,重试等Filter Facatory
// 初始化熔断器配置,熔断工厂filter
@Configuration
@ConditionalOnClass({HystrixObservableCommand.class, RxReactiveStreams.class})
protected static class HystrixConfiguration {
@Bean
public HystrixGatewayFilterFactory hystrixGatewayFilterFactory(DispatcherHandler dispatcherHandler) {
return new HystrixGatewayFilterFactory(dispatcherHandler);
}
}
// 初始化限流工厂filter
@Bean(name = PrincipalNameKeyResolver.BEAN_NAME)
@ConditionalOnBean(RateLimiter.class)
public PrincipalNameKeyResolver principalNameKeyResolver() {
return new PrincipalNameKeyResolver();
}
@Bean
@ConditionalOnBean({RateLimiter.class, KeyResolver.class})
public RequestRateLimiterGatewayFilterFactory requestRateLimiterGatewayFilterFactory(RateLimiter rateLimiter, PrincipalNameKeyResolver resolver) {
return new RequestRateLimiterGatewayFilterFactory(rateLimiter, resolver);
}
RewritePathGatewayFilterFactory,RetryGatewayFilterFactory,
//初始化spring-boot执行器 用于心跳检测
@Configuration
@ConditionalOnClass(Health.class)
protected static class GatewayActuatorConfiguration {
@Bean
@ConditionalOnEnabledEndpoint
public GatewayControllerEndpoint gatewayControllerEndpoint(RouteDefinitionLocator routeDefinitionLocator, List<GlobalFilter> globalFilters,
List<GatewayFilterFactory> GatewayFilters, RouteDefinitionWriter routeDefinitionWriter,
RouteLocator routeLocator) {
return new GatewayControllerEndpoint(routeDefinitionLocator, globalFilters, GatewayFilters, routeDefinitionWriter, routeLocator);
}
}