spring cloud gateway 配置分析

srping cloud gateway 版本: 2.0.2

自动配置类列表spring.factories

# Auto Configure
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.GatewayMetricsAutoConfiguration,\
org.springframework.cloud.gateway.config.GatewayRedisAutoConfiguration,\
org.springframework.cloud.gateway.discovery.GatewayDiscoveryClientAutoConfiguration
  • GatewayClassPathWarningAutoConfiguration
    依赖检查类
  • GatewayAutoConfiguration
    主配置类
  • GatewayLoadBalancerClientAutoConfiguration
    负载均衡客户端配置
  • GatewayMetricsAutoConfiguration
    度量配置
  • GatewayRedisAutoConfiguration
    redis配置
  • GatewayDiscoveryClientAutoConfiguration
    自动发现客户端配置

GatewayClassPathWarningAutoConfiguration

这个主要是检查:

  1. 如果有spring-boot-starter-web, 提示移除
  2. 如果没有spring-boot-starter-webflux 提示添加

GatewayLoadBalancerClientAutoConfiguration

依赖RibbonAutoConfiguration创建的LoadBalancerClient
使用LoadBalancerClient创建了LoadBalancerClientFilter

GatewayDiscoveryClientAutoConfiguration

创建List: lb://+serviceId
创建List : RewritePath ‘/’ + serviceId + ‘/(?.*)’ => ‘/${remaining}’
创建DiscoveryLocatorProperties
创建DiscoveryClientRouteDefinitionLocator

GatewayMetricsAutoConfiguration

创建GatewayMetricsFilter过滤器

RedisAutoConfiguration

创建redisTemplate

GatewayAutoConfiguration

@Configuration
//spring.cloud.gateway.enabled 决定是否打开网关, 默认打开
@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true)
@EnableConfigurationProperties
@AutoConfigureBefore(HttpHandlerAutoConfiguration.class)
@AutoConfigureAfter({ GatewayLoadBalancerClientAutoConfiguration.class,
		GatewayClassPathWarningAutoConfiguration.class })
@ConditionalOnClass(DispatcherHandler.class)
创建NettyConfiguration
  • 创建HttpClientProperties
  • 创建nettyClientOptions: connectTimeout, ssl, pool, proxy
  • 创建HttpClient
  • 创建NettyRoutingFilter
  • 创建NettyWriteResponseFilter
  • 创建ReactorNettyWebSocketClient
创建HiddenHttpMethodFilter
创建RouteLocatorBuilder
创建GatewayProperties

这里从属性文件中创建路由器配置和默认过滤器配置:
List routes ;
List defaultFilters ;
List streamingMediaTypes ;

创建 PropertiesRouteDefinitionLocator

PropertiesRouteDefinitionLocator实现了RouteDefinitionLocator, 保存了从GatewayProperties获取的RouteDefinition

创建 InMemoryRouteDefinitionRepository

InMemoryRouteDefinitionRepository 实现了RouteDefinitionLocator, 保存了运行时创建的RouteDefinition

创建CompositeRouteDefinitionLocator(Primary RouteDefinitionLocator)

合并InMemoryRouteDefinitionRepository,PropertiesRouteDefinitionLocator,
DiscoveryClientRouteDefinitionLocator 及任何自定义的RouteDefinitionLocator

创建RouteLocator
	@Bean
	public RouteLocator routeDefinitionRouteLocator(GatewayProperties properties,
												   List<GatewayFilterFactory> GatewayFilters,
												   List<RoutePredicateFactory> predicates,
												   RouteDefinitionLocator routeDefinitionLocator) {
		return new RouteDefinitionRouteLocator(routeDefinitionLocator, predicates, GatewayFilters, properties);
	}

使用GatewayProperties+所有的GatewayFilterFactory+所有的RoutePredicateFactory + 上面创建的CompositeRouteDefinitionLocator
创建路由器列表RouteDefinitionRouteLocator

创建CachingRouteLocator (Primary RouteLocator)

使用CachingRouteLocator包装RouteDefinitionRouteLocator

创建RouteRefreshListener

监听上下文事件, 必要时刷新CachingRouteLocator的缓存

创建GlobalCorsProperties

全局跨域访问配置

创建FilteringWebHandler

过滤器处理核心类, 在这里组装过滤器链.

创建RoutePredicateHandlerMapping

请求处理核心类, 使用FilteringWebHandler+RouteLocator+GlobalCorsProperties+Environment 创建, 所有的请求处理都在这里组装.

创建SecureHeadersProperties

http头部和安全相关的配置

创建ForwardedHeadersFilter 过滤器
创建RemoveHopByHopHeadersFilter过滤器
创建XForwardedHeadersFilter过滤器
创建全局过滤器
  • AdaptCachedBodyGlobalFilter
  • RouteToRequestUrlFilter
  • ForwardRoutingFilter
  • ForwardPathFilter
  • WebsocketRoutingFilter
  • WeightCalculatorWebFilter
创建predicate factory
  • AfterRoutePredicateFactory
  • BeforeRoutePredicateFactory
  • BetweenRoutePredicateFactory
  • CookieRoutePredicateFactory
  • HeaderRoutePredicateFactory
  • HostRoutePredicateFactory
  • MethodRoutePredicateFactory
  • PathRoutePredicateFactory
  • QueryRoutePredicateFactory
  • ReadBodyPredicateFactory
  • RemoteAddrRoutePredicateFactory
  • WeightRoutePredicateFactory
  • CloudFoundryRouteServiceRoutePredicateFactory
创建内置过滤器
  • AddRequestHeaderGatewayFilterFactory
  • AddRequestParameterGatewayFilterFactory
  • AddResponseHeaderGatewayFilterFactory
  • HystrixGatewayFilterFactory
  • ModifyRequestBodyGatewayFilterFactory
  • ModifyResponseBodyGatewayFilterFactory
  • PrefixPathGatewayFilterFactory
  • PreserveHostHeaderGatewayFilterFactory
  • RedirectToGatewayFilterFactory
  • RemoveRequestHeaderGatewayFilterFactory
  • RemoveResponseHeaderGatewayFilterFactory
  • RequestRateLimiterGatewayFilterFactory
  • RewritePathGatewayFilterFactory
  • RetryGatewayFilterFactory
  • SetPathGatewayFilterFactory
  • SecureHeadersGatewayFilterFactory
  • SetRequestHeaderGatewayFilterFactory
  • SetResponseHeaderGatewayFilterFactory
  • SetStatusGatewayFilterFactory
  • SaveSessionGatewayFilterFactory
  • StripPrefixGatewayFilterFactory
  • RequestHeaderToRequestUriGatewayFilterFactory
创建gatewayControllerEndpoint

总结

spring cloud gateway的创建是比较复杂的, 核心类是GatewayAutoConfiguration, 它做了以下几件事:

  • 配置了Netty
  • 创建GatewayProperties, 这里从属性文件中获取了路由器定义和默认过滤器定义
  • 创建了属性RouteDefinitionLocator, 内存RouteDefinitionLocator和自动发现服务RouteDefinitionLocator, 并且将这三者合并为CompositeRouteDefinitionLocator
  • 使用GatewayProperties+所有的GatewayFilterFactory,RoutePredicateFactory+CompositeRouteDefinitionLocator创建路由器列表RouteLocator, 这个东西包含所有.
  • 创建了一批全局过滤器
  • 创建过滤器处理类 FilteringWebHandler
  • 创建请求处理类 RoutePredicateHandlerMapping
    原文作者:Spring Cloud
    原文地址: https://blog.csdn.net/crazyman2010/article/details/85858950
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞