当前位置:网站首页>ES 源码 API调用链路源码分析
ES 源码 API调用链路源码分析
2022-07-31 04:45:00 【水的精神】
关于API调用链路,我们先从熟悉的MVC三层架构说起。做开发的比较熟悉spring springMVC ,我们熟悉的套路是,请求先到 controller层,然后controller去调用service层,然后结果返回。通常,我们还有tomcat作为服务器,来接收请求,接收到的请求调用controller层。用户的请求,例如http请求,一般是先到tomcat服务,然后调用程序的controller。
然后再看es是怎么玩的,es没有用spring spingMVC这些,只用了Guice来做容器管理,es并没有选择使用类似于tomcat的服务器来接收请求,而是选择netty来实现服务器,完成请求的接收与发送。这就对应了下图的RestController,这一层这是和tomcat有相同的作用。而下图的BaseRestHandler实际上可以理解成来选择controller路由器,它要根据请求中的url路径,来选择一个controller。而下图第三步的RestSearchAction,正是我们之前理解的MVC中的 controller层,不同的功能放在了不同的RestAction里边,实际上一些逻辑也放在这里边。而NodeClient这一层实际上有点service的味道,它是真正去执行逻辑的地方。和传统的服务不同的是,es是一个天然的分布式应用。
Netty4HttpServerTransport.dispatchRequest()
RestController.dispatchRequest().tryAllHandlers()
RestController.dispatchRequest() 调用方法里边的wrappedHandler.handleRequest()
BaseRestHandler.handleRequest() 再调用方法里边的prepareRequest()方法,到这一步,实际上会根据请求来选一个具体的action。prepareRequest()实际上是一个接口,可以看下边的实现类,红框只是一个案例(这就是一个删除索引对应的Action)在prepareRequest里边,将rest请求,转换成es内部认识的请求。

以一个案例来分析(RestDeleteIndexAction)
接着看到的是:
@Overridepublic RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(Strings.splitStringByCommaToArray(request.param("index"))); deleteIndexRequest.timeout(request.paramAsTime("timeout", deleteIndexRequest.timeout())); deleteIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", deleteIndexRequest.masterNodeTimeout())); deleteIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, deleteIndexRequest.indicesOptions())); return channel -> client.admin().indices().delete(deleteIndexRequest, new RestToXContentListener<>(channel));}
跟着第五步的 delete一直向下,走到的是ElasticsearchClient.execute(),这又是一个接口,接着跟到它的实现类,来到AbstractClient.execute(),调用doExecute(),这是一个抽象方法,它的实现类NodeClient.doExecute(),在方法中调用了executeLocally(),这个方法会调用transportAction(action).execute(),可以看到 action被执行了!
接着第6步,点进去transportAction(action).execute()来到了TransportAction.execute() 在这个类中,继续点击execute(),里边调用的是requestFilterChain.proceed(), 在向下走到proceed()里边,调用的是this.action.doExecute(task, request, listener),这个doExecute()是 TransportAction类中的方法,并且这个类是一个抽象类,于是根据doExecute看它的实现类,不同的功能也对应不同的action,还是以删除索引这个为例,

边栏推荐
- 重磅 | 开放原子校源行活动正式启动
- 打造基于ILRuntime热更新的组件化开发
- C语言表白代码?
- Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code
- ClickHouse: Setting up remote connections
- Visual studio shortcuts that improve efficiency, summary (updated from time to time)
- VScode+ESP32 quickly install ESP-IDF plugin
- Safety 20220715
- el-image tag doesn't work after binding click event
- 递归实现汉诺塔问题
猜你喜欢

重磅 | 开放原子校源行活动正式启动

ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法

【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
![[R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters](/img/98/282d8d192f701ad33de48aeeb038b4.png)
[R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters

打造基于ILRuntime热更新的组件化开发

VScode+ESP32快速安装ESP-IDF插件

重磅 | 基金会为白金、黄金、白银捐赠人授牌

Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code

ERROR 1819 (HY000) Your password does not satisfy the current policy requirements

强化学习:从入门到入坑再到拉屎
随机推荐
C语言表白代码?
30 Years of Open Source Community | 2022 Open Atom Global Open Source Summit 30 Years of Open Source Community Special Event Held Successfully
Lua,ILRuntime, HybridCLR(wolong)/huatuo热更新对比分析
MySQL 8.0.30 GA
View source and switch mirrors in two ways: npm and nrm
关于出现大量close_wait状态的理解
【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
Learning DAVID Database (1)
【py脚本】批量二值化处理图像
产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
高等数学---第九章二重积分
MySQL修改root账号密码
[C language] Detailed explanation of operators
el-image标签绑定点击事件后没有有用
STM32HAL库修改Hal_Delay为us级延时
Basic knowledge of mysql (2)
npm、nrm两种方式查看源和切换镜像
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)
Open Source Smart Future | 2022 OpenAtom Global Open Source Summit OpenAtom openEuler sub-forum was successfully held
问题1:给你1-10的列表,实现列表输出,单数在左边,双数在右边。