开发者学堂课程【精通Spring Cloud Alibaba:GateWay路由模式底层源码分析】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/634/detail/10129
GateWay 路由模式底层源码分析
内容简介:
一、入口
二、如何获取参数
一、入口:
# Auto confiaure
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
@Nullable
private List<HandlerMapping> handlerMappings;
@Nullable
private List<HandlerAdapter> handlerAdapters;
@Nullable
private List<HandlerResultHandler> resultHandlers;
*@date 2020/1/1621:30
*/
@Component
public class TokenGlobalFilter implements GlobalFilter {
@override
public Mono<Void> filter(ServerwebExchange exchange,GatewayFilterChain chain){
//如何获取参数呢?
string token = exchange.getRequest().getQueryParams() .getFirst(key. "token");if (StringUtils.isEmpty (token)){
ServerHttpResponse response =,exchange.getResponse();
response.setStatusCode(Httpstatus.INTERNAL_SERVER_ERROR);String msg = "token not is null ";
DataBuffer buffer = response.bufferFactory().wrap(msg.getBytes());
returnresponse.writewith(Mono.just(buffer));
//直接转发到我们真实服务
return chain.filter(exchange);
}
concatMap(mapping -> mapping.getHandler(exchange)) Flux<Object>
getHandler 的子类是:
@Override
public Mono<object>getHandler(ServerwebExchange exchange) {
return getHandlerInternal(exchange).map(handler -> {
if (Corsutils.isCorsRequest(exchange.getRequest())) {
CorsConfiguration configA = this.global
CorsConfigSource.getCorsConfigurationcorsConfiguration configB = get
CorsConfiguration(handler,exchange);
CorsConfiguration config = (configA != null ? configA.combine(configB)
if ( !getCorsProcessor().process(config, exchange) ll
CorsUtils.isPreFlightRequest(exchange.getRequest())){
return REQUEST_HANDLED_HANDLER;
}
}
getHandlerInternal 子类
点到 RoutePredicateHandlerMapping:
@Override
protected Mono<?>getHandlerInternal(ServerwebExchange exchange) {
exchange.getAttributes( ).put(GATEWAY_HANDLER_NAPPER_ATTR,getclass().getSimpleName());
return lookupRoute(exchange)
// .Log( "route-predicate-handLer-mapping" , LeveL.FINER)/ /name this
.flatMap( ( Function<Route,Mono<?>>) r -> {
exchange.getAttributes ( ).remove(GATEwAY_PREDICATE_ROUTE_ATTR);
if ( logger.isDebugEnabled()) {
logger.debug( o:"Mapping [" + getExchangeDesc(exchange)+ "] to " + r);
}
lookupRoute 中文翻译查找路由
访问类型是单个 Route
路由策略:
private fina1 String id;private final URI uri;
private final int order;
private final AsyncPredicate<ServerwebExchange> predicate;
private final List<GatewayFilter> gatewayFilters;
一个路由要经过很多 Filter 才能真正转发执行
执行以后的类型:
真实走的是 FilteringwebHandler
Route route = exchange.getRequiredAttribute(GATEWAY_ROUTE_ATTR);
List<GatewayFilter> gatewayFilters = route.getFilters();
这个时候会查询到路由对应有哪些 Filter(0-8均为默认 Filter ):
0=‘OrderedGatewayfilterdelegate=bGatewayfiterAdapterdelegate=orgspringframeworkcloud.gatewey fiter dapNCachedBodyGloblFl ter@7da 4626, oder=-24742648) 是最优先的 filter
二、如何获取参数
@Component
blic class TokenGlobalFilter implements GlobalFilter{@Override
public Mono<Void> filter(ServerWebExchange exchange,GatewayFilterChain chain){
//如何获取参数呢?
Stringtoken=exchange.getRequest()getQueryParams()getFirst(key:"token"); if(StringUtils.isEmpty(token)){
ServerHttpResponse response =exchange.getResponse();
response.setStatusCode(HttpStatus.INTERNAL SERVER ERROR);
String msg = "token not is null";
DataBuffer buffer = response.bufferFactory( )wrap(msg.getBytes( ) );
return response.writeWith(Monojust(buffer));
}
//直接转发到我们真实服务
return chain.filter(exchange);
}