GateWay路由模式底层源码分析|学习笔记

简介: 快速学习GateWay路由模式底层源码分析

开发者学堂课程【精通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);

}

相关文章
|
6月前
|
负载均衡 Java Spring
Spring cloud gateway 如何在路由时进行负载均衡
Spring cloud gateway 如何在路由时进行负载均衡
624 15
|
8月前
|
SpringCloudAlibaba Java 网络架构
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
316 0
|
7月前
|
Java Nacos 网络架构
Spring Cloud gateway 网关四 动态路由
Spring Cloud gateway 网关四 动态路由
|
7月前
|
微服务
springCloud之路由网关gateway
springCloud之路由网关gateway
|
8月前
|
设计模式 缓存 Java
【设计模式】JAVA Design Patterns——API Gateway(API网关模式)
【设计模式】JAVA Design Patterns——API Gateway(API网关模式)
|
8月前
|
Java Nacos 网络架构
SpringCloud Gateway的使用 + Nacos动态路由
SpringCloud Gateway的使用 + Nacos动态路由
|
8月前
|
网络架构
Gateway路由
Gateway路由
|
8月前
|
JSON Java Nacos
spring-gateway 基于 nacos 配置文件的动态路由
spring-gateway 基于 nacos 配置文件的动态路由
193 0
|
8月前
|
Java 数据库 Nacos
spring-gateway基于数据库 + nacos 的动态路由
spring-gateway基于数据库 + nacos 的动态路由
208 0
|
8月前
|
Java API 网络架构
深入了解 Spring Cloud Config、Spring Cloud Gateway 与断路器模式
Spring Microservices 是一个框架,它使用 Spring 框架更容易地构建和管理基于微服务的应用程序。微服务是一种架构风格,其中一个大型应用程序被构建为一组小型、独立可部署的服务。每个服务具有明确定义的职责,并通过 API 与其他服务通信。
87 0