0
点赞
收藏
分享

微信扫一扫

gateway学习——转发的过程 源码解读(4)

WikongGuan 2022-01-11 阅读 52

问题:

访问gateway的地址:localhost:8080/spring-nacos/user/info
是如何转发到 localhost:8081/spring-nacos/user/info 项目的

DispatcherHandler类就是gateway的请求入口,
(怎么知道这是入口,大概是:http请求——>netty线程模型,处理read事件——>然后调用DispatcherHandler.hanlder方法。这一块没去研究,百度到的)。

DispatcherHandler

先介绍一下这个类:
public class DispatcherHandler implements WebHandler, ApplicationContextAware {
	//1: 实现了 ApplicationContextAware 接口,setApplicationContext(ApplicationContext applicationContext)方法,
	//2: 实现了WebHandler 接口,handle(ServerWebExchange exchange); 方法。
}

看下其实现该方法的逻辑:

1:setApplicationContext:

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) {
		//初始化策略
		initStrategies(applicationContext);
	}

	//这里初始化是给 属性handlerMappings ,handlerAdapters ,resultHandlers 赋值。
	protected void initStrategies(ApplicationContext context) {
		//这里是获取所有的 HandlerMapping 的Bean,
		//例如:RoutePredicateHandlerMapping, 是在:GatewayAutoConfiguration中 用@Bean注入
		//RequestMappingHandlerMapping, 是在:WebFluxAutoConfiguration中内部类:EnableWebFluxConfiguration的RequestMappingHandlerAdapter--->super.createRequestMappingHandlerAdapter();
		//RouterFunctionMapping, 与上面 RequestMappingHandlerMapping 实在同一个类中: @Bean RouterFunctionMapping
		//SimpleUrlHandlerMapping 是在 WebFluxConfigurationSupport 的 @Bean的HandlerMapping
		Map<String, HandlerMapping> mappingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
				context, HandlerMapping.class, true, false);
		//排序
		ArrayList<HandlerMapping> mappings = new ArrayList<>(mappingBeans.values());
		AnnotationAwareOrderComparator.sort(mappings);
		//定义为 不可以修改
		this.handlerMappings = Collections.unmodifiableList(mappings);
		
		//获取 HandlerAdapter ,有三个。
		// RequestMappingHandlerAdapter 是在 WebFluxConfigurationSupport  的@Bean RequestMappingHandlerAdapter
		// HandlerFunctionAdapter 是在 WebFluxConfigurationSupport   的@Bean HandlerFunctionAdapter
		// SimpleHandlerAdapter 是在 WebFluxConfigurationSupport   的@Bean SimpleHandlerAdapter 
		Map<String, HandlerAdapter> adapterBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
				context, HandlerAdapter.class, true, false);

		this.handlerAdapters = new ArrayList<>(adapterBeans.values());
		AnnotationAwareOrderComparator.sort(this.handlerAdapters);
		// 这里获取 :HandlerResultHandler, 都是在 WebFluxConfigurationSupport类中注入的Bean
		// ResponseEntityResultHandler 
		// ResponseBodyResultHandler
		// ViewResolutionResultHandler
		// ServerResponseResultHandler
		Map<String, HandlerResultHandler> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
				context, HandlerResultHandler.class, true, false);

		this.resultHandlers = new ArrayList<>(beans.values());
		AnnotationAwareOrderComparator.sort(this.resultHandlers);
	}

2:handle(ServerWebExchange exchange)方法

@Override
	public Mono<Void> handle(ServerWebExchange exchange) {
		if (this.handlerMappings == null) {
			return createNotFoundError();
		}
		//明天再将主流程,很重要
		return Flux.fromIterable(this.handlerMappings)
				.concatMap(mapping -> mapping.getHandler(exchange))
				.next()
				.switchIfEmpty(createNotFoundError())
				.flatMap(handler -> invokeHandler(exchange, handler))
				.flatMap(result -> handleResult(exchange, result));
	}
举报

相关推荐

0 条评论