当前位置:网站首页>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,还是以删除索引这个为例,
边栏推荐
- Notes on the establishment of the company's official website (6): The public security record of the domain name is carried out and the record number is displayed at the bottom of the web page
- Lua,ILRuntime, HybridCLR(wolong)/huatuo热更新对比分析
- 三道leetcode上的oj题
- 聚变云原生,赋能新里程 | 2022开放原子全球开源峰会云原生分论坛圆满召开
- Safety 20220718
- 产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
- ENSP,划分VLAN、静态路由,三层交换机综合配置
- WeChat applet uses cloud functions to update and add cloud database nested array elements
- el-image tag doesn't work after binding click event
- input输入框展示两位小数之precision
猜你喜欢
随机推荐
微信小程序使用云函数更新和添加云数据库嵌套数组元素
Musk talks to the "virtual version" of Musk, how far is the brain-computer interaction technology from us
volatile内存语义以及实现 -volatile写和读对普通变量的影响
C Implementation of Simple Network File Copy
Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code
WPF WPF 】 【 the depth resolution of the template
MySQL based operations
Why don't you programmers make a living off your own projects?And have to work for someone else?
MATLAB/Simulink&&STM32CubeMX工具链完成基于模型的设计开发(MBD)(三)
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
Recursive implementation of the Tower of Hanoi problem
C# 实现PLC的定时器
ERP生产作业控制 金蝶
ClickHouse: Setting up remote connections
(6) Enumeration and annotation
剑指offer专项突击版第15天
Lua,ILRuntime, HybridCLR(wolong)/huatuo hot update comparative analysis
扫雷小游戏——C语言
聚变云原生,赋能新里程 | 2022开放原子全球开源峰会云原生分论坛圆满召开
Unity URP渲染管线摄像机核心机制剖析