vertx原始碼 vert x web的原始碼解析

2021-10-13 15:06:47 字數 3029 閱讀 1119

本來應該要寫的是eventbus的原始碼分析的,但是vert.x中國使用者組(群號:515203212)的小夥伴們好像對vert.x-web深入了解的需求更大一些。所以就先分析vert.x-web的實現原理吧。

分析完之後發現篇幅較長,請耐心看完。

vert.x-web主要的功能是路由,還有常用的http相關工具(例如:bodyhandler,sessionhandler)。 本文主要圍繞vert.x-web是如何實現路由來展開分析。

vert.x-web最主要的是3個類: router, route,routingcontext。

route的api介面和實現類routeimpl

routingcontext的api介面和實現類routingcontextimpl

路由過程

一. 先來看router部分。

router其實就是route的容器, 裡面裝載了全部的route。當request到來時,通過router匹配容器裡的多個route。router把路由的工作丟給routingcontext做了。

router相關的api基本都是建立route(路由點)的。

例如,route(),get(), get(path), 還有建立正則請求路徑的route等等。

除了這些說些比較重要的。

public inte***ce router else else catch (illegalargumentexception e) catch (illegalargumentexception e1) else

//省略若干**... return true;

//匹配路徑 private boolean pathmatches(string mountpoint, routingcontext ctx) else else else else else catch (illegalargumentexception e) else if (failed && currentroute.hasnextfailurehandler(this)) catch (throwable t) else else if (currentroute.hasnextcontexthandler(this)) else catch (throwable t) else catch (illegalargumentexception e) {

unhandledfailure(400, e, route.router());

return true;

//while迴圈結束 匹配失敗 呼叫checkhandlenomatch return false;

至此 vert.x-web基本分析完了。建議上下結合看。因為有些重要的方法提前分析完了

為了方便大家理解, 我要用我最擅長的蜜汁畫畫,畫個圖方便大家理解。

算了,不畫了。

再來扯扯subrouter,即掛載路由的實現。

subrouter也是通過route新增在到router容器中,這個route的path是/mountpoint*

即以/掛載路徑開頭的請求路徑都會匹配上該route,由route進入到subrouter中。

上面提到subrouter要呼叫了自身的handlecontext或handlefailure方法,

直接看關鍵**。

public void handlecontext(routingcontext ctx) {

private final string mountpoint;

set iter, routingcontext inner) {

super(mountpoint, request, iter);

this.inner = inner;

if (mountpoint.charat(mountpoint.length() - 1) == '/') {

mountpoint = mountpoint.substring(0, mountpoint.length() - 1);

//拼接掛載點 this.mountpoint = parentmountpoint == null ? mountpoint : parentmountpoint + mountpoint;

//匹配逐一subrouter容器內的全部route 跟routingcontext一樣 if (!super.iteratenext()) {

//匹配失敗時,再回到父router繼續匹配 這裡確實設計的很巧妙 //分析一下場景, 當subrouter呼叫它的容器內的route的handler發生異常時, //而當前subrouter又沒有設定關於異常的route //此時又回到了父router。 所以為mainrouter設定乙個關於異常的route即可。 inner.next();

接著來扯扯大家經常遇到的疑問。router如何傳到多個verticle中?

這個問題很簡單, 上片分析過shareddata的localmap, 把router存到localmap,然後在其他的verticle再取出來。

示例**

//1. 在verticle1中

val localmap1 = shareddata.getlocalmap("router")

val router = router.router(vertx)

//2. 在verticle2中

val localmap2 = shareddata.getlocalmap("router")

其實這樣傳沒啥意義, 因為當請求到來時, router還是在呼叫router.accept方法所在的執行緒執行。分析完路由的整個過也沒發現有關執行緒切換的**吧?

2. 要想多個請求都全部寫在乙個httpserververticle很簡單,有空扯扯這個吧。(此刻是想快點終結這篇文章了)

總結:有個重要的問題忘了說, routingcontext,router,route中的很多屬性都是延遲初始化的。

又不知道要總結啥了,我真的是不善於總結的人。

原始碼基於vertx-web-3.5.1

由於平時下班回到家要學習其他的,所以能做到周更就不錯了。而且新工作也沒有用vert.x,所以平時接觸的vert.x就是業餘時間自己玩玩了。當然如果深圳有vert.x的工作, 求內推,哈哈哈哈。

Vertx原始碼解析 Web(上)

我們在前面的文章分析了vertx核心單機部分的原始碼。今天輪到vert.x web,由於web的內容比較多,因此分為上下兩部分。乙個基本的vertxweb 片段如下 上面建立了乙個http服務,暴露8080埠,並註冊了乙個path為hello,方法為get的介面。上面涉及到vert.x web的類有...

《原始碼閱讀》原始碼閱讀技巧,原始碼閱讀工具

檢視某個類的完整繼承關係 選中類的名稱,然後按f4 quick type hierarchy quick type hierarchy可以顯示出類的繼承結構,包括它的父類和子類 supertype hierarchy supertype hierarchy可以顯示出類的繼承和實現結構,包括它的父類和...

Cartographer原始碼篇 原始碼分析 1

在安裝編譯cartographer 1.0.0的時候,我們可以看到 主要包括cartorgarpher ros cartographer ceres sover三個部分。其中,ceres solver用於非線性優化,求解最小二乘問題 cartographer ros為ros平台的封裝,獲取感測器資料...