您当前的位置: 首页 > 

顧棟

暂无认证

  • 4浏览

    0关注

    227博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【ES源码分析】Transport模块之REST的解析与处理

顧棟 发布时间:2022-01-19 16:31:58 ,浏览量:4

文章目录
  • Transport模块之REST的解析与处理
    • 注册REST处理
    • 处理请求
      • HTTP请求执行路径

Transport模块之REST的解析与处理

基于ES源码6.7.2

注册REST处理
#mermaid-svg-yU8Gz8iau9AZQvQL .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-yU8Gz8iau9AZQvQL .label text{fill:#333}#mermaid-svg-yU8Gz8iau9AZQvQL .node rect,#mermaid-svg-yU8Gz8iau9AZQvQL .node circle,#mermaid-svg-yU8Gz8iau9AZQvQL .node ellipse,#mermaid-svg-yU8Gz8iau9AZQvQL .node polygon,#mermaid-svg-yU8Gz8iau9AZQvQL .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-yU8Gz8iau9AZQvQL .node .label{text-align:center;fill:#333}#mermaid-svg-yU8Gz8iau9AZQvQL .node.clickable{cursor:pointer}#mermaid-svg-yU8Gz8iau9AZQvQL .arrowheadPath{fill:#333}#mermaid-svg-yU8Gz8iau9AZQvQL .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-yU8Gz8iau9AZQvQL .flowchart-link{stroke:#333;fill:none}#mermaid-svg-yU8Gz8iau9AZQvQL .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-yU8Gz8iau9AZQvQL .edgeLabel rect{opacity:0.9}#mermaid-svg-yU8Gz8iau9AZQvQL .edgeLabel span{color:#333}#mermaid-svg-yU8Gz8iau9AZQvQL .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-yU8Gz8iau9AZQvQL .cluster text{fill:#333}#mermaid-svg-yU8Gz8iau9AZQvQL div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-yU8Gz8iau9AZQvQL .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-yU8Gz8iau9AZQvQL text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-yU8Gz8iau9AZQvQL .actor-line{stroke:grey}#mermaid-svg-yU8Gz8iau9AZQvQL .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-yU8Gz8iau9AZQvQL .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-yU8Gz8iau9AZQvQL #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-yU8Gz8iau9AZQvQL .sequenceNumber{fill:#fff}#mermaid-svg-yU8Gz8iau9AZQvQL #sequencenumber{fill:#333}#mermaid-svg-yU8Gz8iau9AZQvQL #crosshead path{fill:#333;stroke:#333}#mermaid-svg-yU8Gz8iau9AZQvQL .messageText{fill:#333;stroke:#333}#mermaid-svg-yU8Gz8iau9AZQvQL .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-yU8Gz8iau9AZQvQL .labelText,#mermaid-svg-yU8Gz8iau9AZQvQL .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-yU8Gz8iau9AZQvQL .loopText,#mermaid-svg-yU8Gz8iau9AZQvQL .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-yU8Gz8iau9AZQvQL .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-yU8Gz8iau9AZQvQL .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-yU8Gz8iau9AZQvQL .noteText,#mermaid-svg-yU8Gz8iau9AZQvQL .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-yU8Gz8iau9AZQvQL .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-yU8Gz8iau9AZQvQL .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-yU8Gz8iau9AZQvQL .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-yU8Gz8iau9AZQvQL .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-yU8Gz8iau9AZQvQL .section{stroke:none;opacity:0.2}#mermaid-svg-yU8Gz8iau9AZQvQL .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-yU8Gz8iau9AZQvQL .section2{fill:#fff400}#mermaid-svg-yU8Gz8iau9AZQvQL .section1,#mermaid-svg-yU8Gz8iau9AZQvQL .section3{fill:#fff;opacity:0.2}#mermaid-svg-yU8Gz8iau9AZQvQL .sectionTitle0{fill:#333}#mermaid-svg-yU8Gz8iau9AZQvQL .sectionTitle1{fill:#333}#mermaid-svg-yU8Gz8iau9AZQvQL .sectionTitle2{fill:#333}#mermaid-svg-yU8Gz8iau9AZQvQL .sectionTitle3{fill:#333}#mermaid-svg-yU8Gz8iau9AZQvQL .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-yU8Gz8iau9AZQvQL .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-yU8Gz8iau9AZQvQL .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-yU8Gz8iau9AZQvQL .grid path{stroke-width:0}#mermaid-svg-yU8Gz8iau9AZQvQL .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-yU8Gz8iau9AZQvQL .task{stroke-width:2}#mermaid-svg-yU8Gz8iau9AZQvQL .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-yU8Gz8iau9AZQvQL .taskText:not([font-size]){font-size:11px}#mermaid-svg-yU8Gz8iau9AZQvQL .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-yU8Gz8iau9AZQvQL .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-yU8Gz8iau9AZQvQL .task.clickable{cursor:pointer}#mermaid-svg-yU8Gz8iau9AZQvQL .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-yU8Gz8iau9AZQvQL .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-yU8Gz8iau9AZQvQL .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-yU8Gz8iau9AZQvQL .taskText0,#mermaid-svg-yU8Gz8iau9AZQvQL .taskText1,#mermaid-svg-yU8Gz8iau9AZQvQL .taskText2,#mermaid-svg-yU8Gz8iau9AZQvQL .taskText3{fill:#fff}#mermaid-svg-yU8Gz8iau9AZQvQL .task0,#mermaid-svg-yU8Gz8iau9AZQvQL .task1,#mermaid-svg-yU8Gz8iau9AZQvQL .task2,#mermaid-svg-yU8Gz8iau9AZQvQL .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-yU8Gz8iau9AZQvQL .taskTextOutside0,#mermaid-svg-yU8Gz8iau9AZQvQL .taskTextOutside2{fill:#000}#mermaid-svg-yU8Gz8iau9AZQvQL .taskTextOutside1,#mermaid-svg-yU8Gz8iau9AZQvQL .taskTextOutside3{fill:#000}#mermaid-svg-yU8Gz8iau9AZQvQL .active0,#mermaid-svg-yU8Gz8iau9AZQvQL .active1,#mermaid-svg-yU8Gz8iau9AZQvQL .active2,#mermaid-svg-yU8Gz8iau9AZQvQL .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-yU8Gz8iau9AZQvQL .activeText0,#mermaid-svg-yU8Gz8iau9AZQvQL .activeText1,#mermaid-svg-yU8Gz8iau9AZQvQL .activeText2,#mermaid-svg-yU8Gz8iau9AZQvQL .activeText3{fill:#000 !important}#mermaid-svg-yU8Gz8iau9AZQvQL .done0,#mermaid-svg-yU8Gz8iau9AZQvQL .done1,#mermaid-svg-yU8Gz8iau9AZQvQL .done2,#mermaid-svg-yU8Gz8iau9AZQvQL .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-yU8Gz8iau9AZQvQL .doneText0,#mermaid-svg-yU8Gz8iau9AZQvQL .doneText1,#mermaid-svg-yU8Gz8iau9AZQvQL .doneText2,#mermaid-svg-yU8Gz8iau9AZQvQL .doneText3{fill:#000 !important}#mermaid-svg-yU8Gz8iau9AZQvQL .crit0,#mermaid-svg-yU8Gz8iau9AZQvQL .crit1,#mermaid-svg-yU8Gz8iau9AZQvQL .crit2,#mermaid-svg-yU8Gz8iau9AZQvQL .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-yU8Gz8iau9AZQvQL .activeCrit0,#mermaid-svg-yU8Gz8iau9AZQvQL .activeCrit1,#mermaid-svg-yU8Gz8iau9AZQvQL .activeCrit2,#mermaid-svg-yU8Gz8iau9AZQvQL .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-yU8Gz8iau9AZQvQL .doneCrit0,#mermaid-svg-yU8Gz8iau9AZQvQL .doneCrit1,#mermaid-svg-yU8Gz8iau9AZQvQL .doneCrit2,#mermaid-svg-yU8Gz8iau9AZQvQL .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-yU8Gz8iau9AZQvQL .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-yU8Gz8iau9AZQvQL .milestoneText{font-style:italic}#mermaid-svg-yU8Gz8iau9AZQvQL .doneCritText0,#mermaid-svg-yU8Gz8iau9AZQvQL .doneCritText1,#mermaid-svg-yU8Gz8iau9AZQvQL .doneCritText2,#mermaid-svg-yU8Gz8iau9AZQvQL .doneCritText3{fill:#000 !important}#mermaid-svg-yU8Gz8iau9AZQvQL .activeCritText0,#mermaid-svg-yU8Gz8iau9AZQvQL .activeCritText1,#mermaid-svg-yU8Gz8iau9AZQvQL .activeCritText2,#mermaid-svg-yU8Gz8iau9AZQvQL .activeCritText3{fill:#000 !important}#mermaid-svg-yU8Gz8iau9AZQvQL .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-yU8Gz8iau9AZQvQL g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-yU8Gz8iau9AZQvQL g.classGroup text .title{font-weight:bolder}#mermaid-svg-yU8Gz8iau9AZQvQL g.clickable{cursor:pointer}#mermaid-svg-yU8Gz8iau9AZQvQL g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-yU8Gz8iau9AZQvQL g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-yU8Gz8iau9AZQvQL .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-yU8Gz8iau9AZQvQL .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-yU8Gz8iau9AZQvQL .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-yU8Gz8iau9AZQvQL .dashed-line{stroke-dasharray:3}#mermaid-svg-yU8Gz8iau9AZQvQL #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-yU8Gz8iau9AZQvQL #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-yU8Gz8iau9AZQvQL #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-yU8Gz8iau9AZQvQL #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-yU8Gz8iau9AZQvQL #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-yU8Gz8iau9AZQvQL #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-yU8Gz8iau9AZQvQL #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-yU8Gz8iau9AZQvQL #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-yU8Gz8iau9AZQvQL .commit-id,#mermaid-svg-yU8Gz8iau9AZQvQL .commit-msg,#mermaid-svg-yU8Gz8iau9AZQvQL .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-yU8Gz8iau9AZQvQL .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-yU8Gz8iau9AZQvQL .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-yU8Gz8iau9AZQvQL g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-yU8Gz8iau9AZQvQL g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-yU8Gz8iau9AZQvQL g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-yU8Gz8iau9AZQvQL g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-yU8Gz8iau9AZQvQL g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-yU8Gz8iau9AZQvQL g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-yU8Gz8iau9AZQvQL .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-yU8Gz8iau9AZQvQL .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-yU8Gz8iau9AZQvQL .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-yU8Gz8iau9AZQvQL .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-yU8Gz8iau9AZQvQL .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-yU8Gz8iau9AZQvQL .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-yU8Gz8iau9AZQvQL .edgeLabel text{fill:#333}#mermaid-svg-yU8Gz8iau9AZQvQL .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-yU8Gz8iau9AZQvQL .node circle.state-start{fill:black;stroke:black}#mermaid-svg-yU8Gz8iau9AZQvQL .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-yU8Gz8iau9AZQvQL #statediagram-barbEnd{fill:#9370db}#mermaid-svg-yU8Gz8iau9AZQvQL .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-yU8Gz8iau9AZQvQL .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-yU8Gz8iau9AZQvQL .statediagram-state .divider{stroke:#9370db}#mermaid-svg-yU8Gz8iau9AZQvQL .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-yU8Gz8iau9AZQvQL .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-yU8Gz8iau9AZQvQL .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-yU8Gz8iau9AZQvQL .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-yU8Gz8iau9AZQvQL .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-yU8Gz8iau9AZQvQL .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-yU8Gz8iau9AZQvQL .note-edge{stroke-dasharray:5}#mermaid-svg-yU8Gz8iau9AZQvQL .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-yU8Gz8iau9AZQvQL .error-icon{fill:#522}#mermaid-svg-yU8Gz8iau9AZQvQL .error-text{fill:#522;stroke:#522}#mermaid-svg-yU8Gz8iau9AZQvQL .edge-thickness-normal{stroke-width:2px}#mermaid-svg-yU8Gz8iau9AZQvQL .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-yU8Gz8iau9AZQvQL .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-yU8Gz8iau9AZQvQL .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-yU8Gz8iau9AZQvQL .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-yU8Gz8iau9AZQvQL .marker{fill:#333}#mermaid-svg-yU8Gz8iau9AZQvQL .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-yU8Gz8iau9AZQvQL { color: rgba(0, 0, 0, 0.75); font: ; } Node ActionModule NetworkModule Netty4Plugin new ActionModule setupActions register new RestController ActionModule new NetworkModule plugin.getHttpTransports registerHttpTransport plugin.getTransports registerTransport plugin.getTransportInterceptors registerTransportInterceptor NetworkModule actionModule.initRestHandlers Node ActionModule NetworkModule Netty4Plugin

借由上图回顾一下通信模块的初始化过程,在ActionModule下对Rest请求处理进行了注册,注册过程在initRestHandlers方法中。可以发现对REST请求执行处理的类的命名是Rest*Action,同时可以发现这些处理类都继承了BaseRestHandler,而BaseRestHandler继承了RestHandler

    public void initRestHandlers(Supplier nodesInCluster) {
        List catActions = new ArrayList();
        Consumer registerHandler = a -> {
            if (a instanceof AbstractCatAction) {
                catActions.add((AbstractCatAction) a);
            }
        };
        registerHandler.accept(new RestMainAction(settings, restController));
        registerHandler.accept(new RestNodesInfoAction(settings, restController, settingsFilter));
        registerHandler.accept(new RestRemoteClusterInfoAction(settings, restController));
        registerHandler.accept(new RestNodesStatsAction(settings, restController));
        registerHandler.accept(new RestNodesUsageAction(settings, restController));
        registerHandler.accept(new RestNodesHotThreadsAction(settings, restController));
        registerHandler.accept(new RestClusterAllocationExplainAction(settings, restController));
        registerHandler.accept(new RestClusterStatsAction(settings, restController));
        registerHandler.accept(new RestClusterStateAction(settings, restController, settingsFilter));
        registerHandler.accept(new RestClusterHealthAction(settings, restController));
        ...
        ...
        ...
        for (ActionPlugin plugin : actionPlugins) {
            for (RestHandler handler : plugin.getRestHandlers(settings, restController, clusterSettings, indexScopedSettings,
                    settingsFilter, indexNameExpressionResolver, nodesInCluster)) {
                registerHandler.accept(handler);
            }
        }
        registerHandler.accept(new RestCatAction(settings, restController, catActions));
    }

RestClusterHealthAction为例,在其构造函数中对请求头中请求方法为GET,URI为/_cluster/health和拥有占位符/_cluster/health/{index}的处理类为this(即自己)。

    public RestClusterHealthAction(Settings settings, RestController controller) {
        super(settings);

        controller.registerHandler(RestRequest.Method.GET, "/_cluster/health", this);
        controller.registerHandler(RestRequest.Method.GET, "/_cluster/health/{index}", this);
    }

由于继承了BaseRestHandler,所以必须实现prepareRequest方法,用于在接收到请求时,做一些前置工作,比如验证参数,转换为内部RPC请求等。

处理请求 HTTP请求执行路径
#mermaid-svg-IA50hDvEOEDgARNs .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-IA50hDvEOEDgARNs .label text{fill:#333}#mermaid-svg-IA50hDvEOEDgARNs .node rect,#mermaid-svg-IA50hDvEOEDgARNs .node circle,#mermaid-svg-IA50hDvEOEDgARNs .node ellipse,#mermaid-svg-IA50hDvEOEDgARNs .node polygon,#mermaid-svg-IA50hDvEOEDgARNs .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-IA50hDvEOEDgARNs .node .label{text-align:center;fill:#333}#mermaid-svg-IA50hDvEOEDgARNs .node.clickable{cursor:pointer}#mermaid-svg-IA50hDvEOEDgARNs .arrowheadPath{fill:#333}#mermaid-svg-IA50hDvEOEDgARNs .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-IA50hDvEOEDgARNs .flowchart-link{stroke:#333;fill:none}#mermaid-svg-IA50hDvEOEDgARNs .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-IA50hDvEOEDgARNs .edgeLabel rect{opacity:0.9}#mermaid-svg-IA50hDvEOEDgARNs .edgeLabel span{color:#333}#mermaid-svg-IA50hDvEOEDgARNs .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-IA50hDvEOEDgARNs .cluster text{fill:#333}#mermaid-svg-IA50hDvEOEDgARNs div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-IA50hDvEOEDgARNs .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-IA50hDvEOEDgARNs text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-IA50hDvEOEDgARNs .actor-line{stroke:grey}#mermaid-svg-IA50hDvEOEDgARNs .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-IA50hDvEOEDgARNs .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-IA50hDvEOEDgARNs #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-IA50hDvEOEDgARNs .sequenceNumber{fill:#fff}#mermaid-svg-IA50hDvEOEDgARNs #sequencenumber{fill:#333}#mermaid-svg-IA50hDvEOEDgARNs #crosshead path{fill:#333;stroke:#333}#mermaid-svg-IA50hDvEOEDgARNs .messageText{fill:#333;stroke:#333}#mermaid-svg-IA50hDvEOEDgARNs .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-IA50hDvEOEDgARNs .labelText,#mermaid-svg-IA50hDvEOEDgARNs .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-IA50hDvEOEDgARNs .loopText,#mermaid-svg-IA50hDvEOEDgARNs .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-IA50hDvEOEDgARNs .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-IA50hDvEOEDgARNs .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-IA50hDvEOEDgARNs .noteText,#mermaid-svg-IA50hDvEOEDgARNs .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-IA50hDvEOEDgARNs .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-IA50hDvEOEDgARNs .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-IA50hDvEOEDgARNs .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-IA50hDvEOEDgARNs .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-IA50hDvEOEDgARNs .section{stroke:none;opacity:0.2}#mermaid-svg-IA50hDvEOEDgARNs .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-IA50hDvEOEDgARNs .section2{fill:#fff400}#mermaid-svg-IA50hDvEOEDgARNs .section1,#mermaid-svg-IA50hDvEOEDgARNs .section3{fill:#fff;opacity:0.2}#mermaid-svg-IA50hDvEOEDgARNs .sectionTitle0{fill:#333}#mermaid-svg-IA50hDvEOEDgARNs .sectionTitle1{fill:#333}#mermaid-svg-IA50hDvEOEDgARNs .sectionTitle2{fill:#333}#mermaid-svg-IA50hDvEOEDgARNs .sectionTitle3{fill:#333}#mermaid-svg-IA50hDvEOEDgARNs .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-IA50hDvEOEDgARNs .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-IA50hDvEOEDgARNs .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-IA50hDvEOEDgARNs .grid path{stroke-width:0}#mermaid-svg-IA50hDvEOEDgARNs .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-IA50hDvEOEDgARNs .task{stroke-width:2}#mermaid-svg-IA50hDvEOEDgARNs .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-IA50hDvEOEDgARNs .taskText:not([font-size]){font-size:11px}#mermaid-svg-IA50hDvEOEDgARNs .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-IA50hDvEOEDgARNs .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-IA50hDvEOEDgARNs .task.clickable{cursor:pointer}#mermaid-svg-IA50hDvEOEDgARNs .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-IA50hDvEOEDgARNs .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-IA50hDvEOEDgARNs .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-IA50hDvEOEDgARNs .taskText0,#mermaid-svg-IA50hDvEOEDgARNs .taskText1,#mermaid-svg-IA50hDvEOEDgARNs .taskText2,#mermaid-svg-IA50hDvEOEDgARNs .taskText3{fill:#fff}#mermaid-svg-IA50hDvEOEDgARNs .task0,#mermaid-svg-IA50hDvEOEDgARNs .task1,#mermaid-svg-IA50hDvEOEDgARNs .task2,#mermaid-svg-IA50hDvEOEDgARNs .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-IA50hDvEOEDgARNs .taskTextOutside0,#mermaid-svg-IA50hDvEOEDgARNs .taskTextOutside2{fill:#000}#mermaid-svg-IA50hDvEOEDgARNs .taskTextOutside1,#mermaid-svg-IA50hDvEOEDgARNs .taskTextOutside3{fill:#000}#mermaid-svg-IA50hDvEOEDgARNs .active0,#mermaid-svg-IA50hDvEOEDgARNs .active1,#mermaid-svg-IA50hDvEOEDgARNs .active2,#mermaid-svg-IA50hDvEOEDgARNs .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-IA50hDvEOEDgARNs .activeText0,#mermaid-svg-IA50hDvEOEDgARNs .activeText1,#mermaid-svg-IA50hDvEOEDgARNs .activeText2,#mermaid-svg-IA50hDvEOEDgARNs .activeText3{fill:#000 !important}#mermaid-svg-IA50hDvEOEDgARNs .done0,#mermaid-svg-IA50hDvEOEDgARNs .done1,#mermaid-svg-IA50hDvEOEDgARNs .done2,#mermaid-svg-IA50hDvEOEDgARNs .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-IA50hDvEOEDgARNs .doneText0,#mermaid-svg-IA50hDvEOEDgARNs .doneText1,#mermaid-svg-IA50hDvEOEDgARNs .doneText2,#mermaid-svg-IA50hDvEOEDgARNs .doneText3{fill:#000 !important}#mermaid-svg-IA50hDvEOEDgARNs .crit0,#mermaid-svg-IA50hDvEOEDgARNs .crit1,#mermaid-svg-IA50hDvEOEDgARNs .crit2,#mermaid-svg-IA50hDvEOEDgARNs .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-IA50hDvEOEDgARNs .activeCrit0,#mermaid-svg-IA50hDvEOEDgARNs .activeCrit1,#mermaid-svg-IA50hDvEOEDgARNs .activeCrit2,#mermaid-svg-IA50hDvEOEDgARNs .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-IA50hDvEOEDgARNs .doneCrit0,#mermaid-svg-IA50hDvEOEDgARNs .doneCrit1,#mermaid-svg-IA50hDvEOEDgARNs .doneCrit2,#mermaid-svg-IA50hDvEOEDgARNs .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-IA50hDvEOEDgARNs .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-IA50hDvEOEDgARNs .milestoneText{font-style:italic}#mermaid-svg-IA50hDvEOEDgARNs .doneCritText0,#mermaid-svg-IA50hDvEOEDgARNs .doneCritText1,#mermaid-svg-IA50hDvEOEDgARNs .doneCritText2,#mermaid-svg-IA50hDvEOEDgARNs .doneCritText3{fill:#000 !important}#mermaid-svg-IA50hDvEOEDgARNs .activeCritText0,#mermaid-svg-IA50hDvEOEDgARNs .activeCritText1,#mermaid-svg-IA50hDvEOEDgARNs .activeCritText2,#mermaid-svg-IA50hDvEOEDgARNs .activeCritText3{fill:#000 !important}#mermaid-svg-IA50hDvEOEDgARNs .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-IA50hDvEOEDgARNs g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-IA50hDvEOEDgARNs g.classGroup text .title{font-weight:bolder}#mermaid-svg-IA50hDvEOEDgARNs g.clickable{cursor:pointer}#mermaid-svg-IA50hDvEOEDgARNs g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-IA50hDvEOEDgARNs g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-IA50hDvEOEDgARNs .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-IA50hDvEOEDgARNs .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-IA50hDvEOEDgARNs .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-IA50hDvEOEDgARNs .dashed-line{stroke-dasharray:3}#mermaid-svg-IA50hDvEOEDgARNs #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-IA50hDvEOEDgARNs #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-IA50hDvEOEDgARNs #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-IA50hDvEOEDgARNs #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-IA50hDvEOEDgARNs #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-IA50hDvEOEDgARNs #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-IA50hDvEOEDgARNs #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-IA50hDvEOEDgARNs #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-IA50hDvEOEDgARNs .commit-id,#mermaid-svg-IA50hDvEOEDgARNs .commit-msg,#mermaid-svg-IA50hDvEOEDgARNs .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-IA50hDvEOEDgARNs .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-IA50hDvEOEDgARNs .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-IA50hDvEOEDgARNs g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-IA50hDvEOEDgARNs g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-IA50hDvEOEDgARNs g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-IA50hDvEOEDgARNs g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-IA50hDvEOEDgARNs g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-IA50hDvEOEDgARNs g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-IA50hDvEOEDgARNs .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-IA50hDvEOEDgARNs .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-IA50hDvEOEDgARNs .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-IA50hDvEOEDgARNs .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-IA50hDvEOEDgARNs .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-IA50hDvEOEDgARNs .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-IA50hDvEOEDgARNs .edgeLabel text{fill:#333}#mermaid-svg-IA50hDvEOEDgARNs .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-IA50hDvEOEDgARNs .node circle.state-start{fill:black;stroke:black}#mermaid-svg-IA50hDvEOEDgARNs .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-IA50hDvEOEDgARNs #statediagram-barbEnd{fill:#9370db}#mermaid-svg-IA50hDvEOEDgARNs .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-IA50hDvEOEDgARNs .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-IA50hDvEOEDgARNs .statediagram-state .divider{stroke:#9370db}#mermaid-svg-IA50hDvEOEDgARNs .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-IA50hDvEOEDgARNs .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-IA50hDvEOEDgARNs .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-IA50hDvEOEDgARNs .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-IA50hDvEOEDgARNs .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-IA50hDvEOEDgARNs .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-IA50hDvEOEDgARNs .note-edge{stroke-dasharray:5}#mermaid-svg-IA50hDvEOEDgARNs .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-IA50hDvEOEDgARNs .error-icon{fill:#522}#mermaid-svg-IA50hDvEOEDgARNs .error-text{fill:#522;stroke:#522}#mermaid-svg-IA50hDvEOEDgARNs .edge-thickness-normal{stroke-width:2px}#mermaid-svg-IA50hDvEOEDgARNs .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-IA50hDvEOEDgARNs .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-IA50hDvEOEDgARNs .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-IA50hDvEOEDgARNs .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-IA50hDvEOEDgARNs .marker{fill:#333}#mermaid-svg-IA50hDvEOEDgARNs .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-IA50hDvEOEDgARNs { color: rgba(0, 0, 0, 0.75); font: ; } Netty4HttpRequestHandler Netty4HttpServerTransport RestController BaseRestHandler Rest*Action 内部Action channelRead0 dispatchRequest dispatchRequest tryAllHandlers dispatchRequest handleRequest prepareRequest RestChannelConsumer action.accept(channel) execute(...) sendResponse Netty4HttpRequestHandler Netty4HttpServerTransport RestController BaseRestHandler Rest*Action 内部Action

BaseRestHandlerhandleRequest方法

    @Override
    public final void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
        // 对请求的预处理
        final RestChannelConsumer action = prepareRequest(request, client);

        // validate unconsumed params, but we must exclude params used to format the response
        // use a sorted set so the unconsumed parameters appear in a reliable sorted order
        final SortedSet unconsumedParams =
            request.unconsumedParams().stream().filter(p -> !responseParams().contains(p)).collect(Collectors.toCollection(TreeSet::new));

        // validate the non-response params
        if (!unconsumedParams.isEmpty()) {
            final Set candidateParams = new HashSet();
            candidateParams.addAll(request.consumedParams());
            candidateParams.addAll(responseParams());
            throw new IllegalArgumentException(unrecognized(request, unconsumedParams, candidateParams, "parameter"));
        }

        if (request.hasContent() && request.isContentConsumed() == false) {
            deprecationLogger.deprecated(
                    "request [{} {}] does not support having a body; Elasticsearch 7.x+ will reject such requests",
                    request.method(),
                    request.path());
        }

        usageCount.increment();
        // 实际执行真正的Action
        action.accept(channel);
    }

RestClusterHealthActionprepareRequest

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    // 构造内部请求clusterHealthRequest
    ClusterHealthRequest clusterHealthRequest = clusterHealthRequest(Strings.splitStringByCommaToArray(request.param("index")));
    clusterHealthRequest.local(request.paramAsBoolean("local", clusterHealthRequest.local()));
    clusterHealthRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterHealthRequest.masterNodeTimeout()));
    clusterHealthRequest.timeout(request.paramAsTime("timeout", clusterHealthRequest.timeout()));
    String waitForStatus = request.param("wait_for_status");
    if (waitForStatus != null) {
        clusterHealthRequest.waitForStatus(ClusterHealthStatus.valueOf(waitForStatus.toUpperCase(Locale.ROOT)));
    }
    clusterHealthRequest.waitForNoRelocatingShards(
        request.paramAsBoolean("wait_for_no_relocating_shards", clusterHealthRequest.waitForNoRelocatingShards()));
    clusterHealthRequest.waitForNoInitializingShards(
        request.paramAsBoolean("wait_for_no_initializing_shards", clusterHealthRequest.waitForNoRelocatingShards()));
    if (request.hasParam("wait_for_relocating_shards")) {
        // wait_for_relocating_shards has been removed in favor of wait_for_no_relocating_shards
        throw new IllegalArgumentException("wait_for_relocating_shards has been removed, " +
                                           "use wait_for_no_relocating_shards [true/false] instead");
    }
    String waitForActiveShards = request.param("wait_for_active_shards");
    if (waitForActiveShards != null) {
        clusterHealthRequest.waitForActiveShards(ActiveShardCount.parseString(waitForActiveShards));
    }
    clusterHealthRequest.waitForNodes(request.param("wait_for_nodes", clusterHealthRequest.waitForNodes()));
    if (request.param("wait_for_events") != null) {
        clusterHealthRequest.waitForEvents(Priority.valueOf(request.param("wait_for_events").toUpperCase(Locale.ROOT)));
    }
    // 在AbstractClient中将REST和tcpAction,在ActionModule找到对应的处理类
    return channel -> client.admin().cluster().health(clusterHealthRequest, new RestStatusToXContentListener(channel));
}

AbstractClienthealth

        @Override
        public void health(final ClusterHealthRequest request, final ActionListener listener) {
            execute(ClusterHealthAction.INSTANCE, request, listener);
        }

之后通过NodeClient.executeLocally将请求发出去。开始转入TCP请求处理了。

public  Task executeLocally(GenericAction action, Request request, ActionListener listener) {
        return transportAction(action).execute(request, listener);
}

可以看出REST应该是在内部通信上有包装了一层。为了简化和方便开发人员通过客户端调用。ES的客户端建议使用REST Client

关注
打赏
1663402667
查看更多评论
立即登录/注册

微信扫码登录

0.1160s