Gateway實現原理筆記

2021-10-10 08:00:53 字數 3532 閱讀 3890

引入spring-cloud-gateway依賴,檢視其mate-inf下的spring.factories檔案,可以看到以org.springframework.boot.autoconfigure.enableautoconfiguration為key,配置了gatewayclasspathwarningautoconfiguration,gatewayautoconfiguration,gatewayloadbalancerclientautoconfiguration,gatewayredisautoconfiguration等value,當springboot專案啟動後,會通過spi機制,去載入這些配置類。

//封裝配置的filters的引數資訊

//將route儲存到serverwebexchange的屬性中,後面的過濾器會用到這個屬性

exchange.getattributes().put(serverwebexchangeutils.gateway_route_attr, r);

//返回filteringwebhandler。用於構造***鏈,處理請求

return mono.just(this.webhandler);

}).switchifempty(mono.empty().then(mono.fromrunnable(() ->

})));}}

protected monolookuproute(serverwebexchange exchange) ).doonerror((e) -> ).onerrorresume((e) -> );

}).next().map((route) ->

this.validateroute(route, exchange);

//返回匹配到的route

return route;

});}

回到dispatcherhandler的handle(serverwebexchange exchange)方法 ,獲得匹配的route和對應的handler(filteringwebhandler)之後,呼叫dispatcherhandler.invokehandler方法,獲取匹配的處理器介面卡之後,呼叫其handle方法,即******handleradapter的handle方法,******handleradapter.handle中,將處理邏輯委託給了webhandler.handle方法。

private monoinvokehandler(serverwebexchange exchange, object handler) }}

return mono.error(new illegalstateexception("no handleradapter: " + handler));

}

******handleradapter.handle

public monohandle(serverwebexchange exchange, object handler)
在webhandler.handle方法中,首先會從exchange屬性中獲取儲存的route,從route中得到其專屬的gatewayfilter,再和全域性的globalfilters合併通過order大小排序,最後通過defaultgatewayfilterchain構建***鏈,執行過濾。主要包括routetorequesturlfilter

public monohandle(serverwebexchange exchange) 

//構建過濾器執行鏈

gateway閘道器筆記

gateway在專案中充當閘道器的作用 使用可看spring官方文件 projects spring cloud learn spring cloud gateway reference doc.route 路由 predicate 斷言 filter 過濾器 配置方式 spring cloud g...

Spring IOC實現原理筆記(一)

基本用法 建乙個測試的bean如下 public class mytestbean public void setteststr string teststr public void test beans.xml檔案如下 xmlns xmlns xsi xsi schemalocation spri...

kvo實現原理 KVC KVO實現原理

一 kvc運用了乙個isa swizzling技術。isa swizzling就是型別混合指標機制。kvc主要通過isa swizzling,來實現其內部查詢定位的。isa指標,如其名稱所指,就是is a kind of的意思 指向維護分發表的物件的類。該分發表實際上包含了指向實現類中的方法的指標,...