当前位置:网站首页>Brpc source code analysis (I) -- the main process of RPC service addition and server startup
Brpc source code analysis (I) -- the main process of RPC service addition and server startup
2022-07-25 11:59:00 【wxj1992】
The usual work used baidu-rpc build rpc service , As a masterpiece of Ge Jun's great God , When there was no open source , This c++ Of rpc The framework has been well received in the factory , Whether it's performance 、 file 、 Code comments are excellent , The internal use range is particularly wide ,17 In open source , The open source version is called brpc, After open source, many large manufacturers have used , At present, it has entered Apache The incubator , The source code and the address of the document are as follows :https://github.com/apache/incubator-brpc.
I am more interested in the bottom things , Therefore, there is a combination of documents and source code to further study this rpc The idea of a framework , Whole brpc Code quantity is not small , I started watching it very early , To be honest, it was really hard at first , But the more I look back, the more I feel that this framework is broad and profound , So I'm going to summarize what I've read into an article , Today, I sorted out part of the first article , It's an essay to see the source code , Blog for the first time , If there is a mistake , I hope you can correct me. .
brpc As a complete rpc frame , Nature also supports being Server And as Client, This article is about being Server How to use and start Server、 Start the internal processing process of the corresponding service .bprc The user-oriented interface is still very friendly , The call is simple , An official post is posted here http Of demo:
Building a rpc Server, To sum up, create a new Server object , Set the parameters , Add your own Service, Then start ,Server It can contain more than one Service, and Service It can also contain multiple method, A specific request is for a method Of , This demo Inside Server There are three Service.
How to use bprc I won't go into that , The official document has a detailed description . Because I came into contact with this framework from business use , So I'm going to start by adding services that are closest to the actual application scenario 、 Start the server and look at the source code .brpc There is a Server class , As a server, this class is used as an entry , from rpc For the construction process of the server , It mainly includes the following three processes :
1. Go to Server Add in Service( Business code )
What I'm talking about here Service, It refers to inheriting from proto The document describes Service Users of Service,brpc Is based on protobuf Of ,protobuf It doesn't contain RPC The implementation of the , But there is RPC The definition of , such as Service,brpc That's what I used , Even if it is not used protobuf News http service , The service interface must also be defined in proto In file , To ensure that all service declarations are focused on proto In file .
Addservice Function has multiple overloads , For example, the above official demo The third call is based on restful mapping A string of , as follows :
It's different Addservice Overloaded calls are AddServiceInternal This internal concrete implementation . in general Addservice It is to specify which services to handle requests from where .
There are three parameters ,protofuf Service Type of service , Is it an internal service (brpc It provides internal services such as monitoring ) The logo of , And the addition of service The option to .
google::protobuf::Service Is a public base class , User defined in proto In the file Service Will be preprocessed into a class that inherits from this base class , Users need to inherit this class to write real business code ( Realization method Corresponding virtual function ), At the framework level, the parent class google::protobuf::Service Handle .AddServiceInternal Is to put Service Inside method Take it out and map it to the specified address , The first call InitializeOnce() Realization server The initialization ( It will only be initialized once , use pthread_once call GlobalInitializeOrDieImpl, Promise to do it only once ), as follows :
brpc It supports multi protocol , And it's the same port , There will be a protocol parsing mechanism to ensure relatively high efficiency , Various protocols are built in , Users can also easily add their own protocol support ,GlobalInitializeOrDieImpl In the function , The most important thing is to register, including http Various protocol support including , Simply put, it specifies the message parsing function of each protocol 、server End use request Processing function 、client End use response Processing function , such as request Processing function , In the picture below http Agreement, for example ,ProcessHttpRequest Is responsible for processing the received http request, This function will handle request Then hand it over to the user-defined function to handle . I won't expand in detail here for the time being , Later articles will explain in detail .
2. Set server parameters
brpc It provides rich parameter settings , Including maximum concurrency , Open or not ssl、 Close idle connection time, etc , Please refer to official documents for details , No more details here , Mainly through Serveroption Type specifies parameters and calls directly set_xxx To set it directly .
3. Start the server
call Start Function to start the server , and Addservice equally ,start There are also overloads of various invocation methods , It's all called in the end StartInternal
stay StartInternal In the function , First of all, some preparations , according to option Made some settings , Include ssl Set and whether to create tls data 、 Start what you need in advance bthread(brpc Use of m:n Threads of thread library ,bthread It's also bprc One of the keys to excellent performance , The following article will introduce in detail ) etc. . Then in the designated ip And port range ( Try constantly within the scope , If you succeed, stop trying ) Start listening on , One server It only supports listening to one port . The main codes are as follows :
Acceptor As the name suggests, it is the receiver of messages ,BuildAcceptor That is to build a receiver , If NULL Call BuildAcceptor
stay BuildAcceptor Inside , The most important ones are as follows :
First, through ListProtocols Get all the agreements supported by registration , Then traverse these protocols , adopt AddHandler add to ,handler Is dealing with message Of , Notice here handler.process All are protocols[i] Inside process_request, That is, the corresponding protocol is used by the server to process the received request , The corresponding one for the client is process_response.
StartAccept The core content is as follows :
brpc Is to use epoll To handle events , It uses edge trigger ,options.on_edge_triggered_events yes epoll The processing function after the arrival of the edge trigger event , That is to say OnNewConnections As the event handler , As the name suggests, it is the processing function used after the arrival of the new connection , There is no talk here OnNewConnections The concrete realization of , Later articles will introduce in detail .
About Socket type , That's right fd The encapsulation of such resources is convenient for use in a multithreaded environment , The official introduction is like this :
and fd The relevant data are in Socket in , yes rpc One of the most complex structures , What's unique about this structure is that it uses 64 Bit SocketId Refer to Socket Object to facilitate use in a multithreaded environment fd.
Socket::Create The function is based on options newly build socket And put id Store in the second parameter , The most important internal operation is to use options.on_edge_triggered_event The function referred to epoll add, On the current server start In the scene of , That is, listening fd On the use of OnNewConnections register epoll Event handling new connections , The startup is now complete , Subsequent waiting epoll Handle the event accordingly .
Be responsible for the first step epoll The event is EventDispatcher, It's distribution epoll event Module , Responsible for the fd Events triggered by the upper edge are distributed to consumers ( Specific business processing functions ), There can be multiple , Run on different bthread On , The specific quantity depends on the parameters , What it does is keep going after starting epoll_wait, get epoll The event is handled by the corresponding function , If it is epoll_in event , call Socket::StartInputEvent, If it is epoll_out, call Socket::HandleEpollOut, The simplified core code is as follows :
StartAccept after , The server is basically started . Back to the initial example , In the end, it will call server.RunUntilAskedToQuit()

RegisterQuitSignalOrDie It's mainly for signal Function registers the exit signal , Once there is an exit signal s_signal_quit Would be true, Thus jump out of the loop and stop server.
brpc As a server, the whole startup process is basically like this , I will write an article later to continue to introduce brpc Some key mechanisms and classes in , And in fd Some details of sending and receiving requests on .
边栏推荐
- Zero-Shot Image Retrieval(零样本跨模态检索)
- Multi-Label Image Classification(多标签图像分类)
- [high concurrency] I summarized the best learning route of concurrent programming with 10 diagrams!! (recommended Collection)
- W5500 is in TCP_ In server mode, you cannot Ping or communicate in the switch / router network.
- What is the global event bus?
- Varest blueprint settings JSON
- Differences in usage between tostring() and new string()
- W5500多节点连接
- Classification parameter stack of JS common built-in object data types
- Arrays in JS
猜你喜欢

什么是全局事件总线?

JDBC summary

Multi-Label Image Classification(多标签图像分类)

W5500 upload temperature and humidity to onenet platform

The first C language program (starting from Hello World)

OneNET平台控制W5500开发板LED灯

return 和 finally的执行顺序 ?各位大佬请看过来,

Attendance system based on w5500

【多模态】《HiT: Hierarchical Transformer with Momentum Contrast for Video-Text Retrieval》ICCV 2021

brpc源码解析(一)—— rpc服务添加以及服务器启动主要过程
随机推荐
教你如何通过MCU配置S2E为TCP Client的工作模式
Transformer变体(Routing Transformer,Linformer,Big Bird)
Innovation and breakthrough! AsiaInfo technology helped a province of China Mobile complete the independent and controllable transformation of its core accounting database
已解决 Files‘ name is invalid or does not exist (1205)
Solved files' name is invalid or doors not exist (1205)
Power BI----这几个技能让报表更具“逼格“
Differences in usage between tostring() and new string()
【多模态】《TransRec: Learning Transferable Recommendation from Mixture-of-Modality Feedback》 Arxiv‘22
软件缺陷的管理
JVM performance tuning methods
Management of software defects
JS interview question: handwriting throttle function
Attendance system based on w5500
小程序image 无法显示base64 图片 解决办法 有效
MySQL historical data supplement new data
Video Caption(跨模态视频摘要/字幕生成)
brpc源码解析(一)—— rpc服务添加以及服务器启动主要过程
The bank's wealth management subsidiary accumulates power to distribute a shares; The rectification of cash management financial products was accelerated
Application and innovation of low code technology in logistics management
JS中的数组