当前位置:网站首页>brpc理解
brpc理解
2022-07-01 18:45:00 【lcyw】
我们只有三个用户类:Server、Channel和Controller,分别对应server端,client端和调整参数集合。
不需要推敲诸如“Client怎么初始化”,“XXXManager有什么用”,“Context和Controller的关系是什么”之类的问题
要做的很简单:
建服务就#include <baidu/rpc/server.h>, 并按照注释或例子使用server对象
访问服务就#include <baidu/rpc/channel.h>,并按照注释或例子使用Channel对象
想控制一次RPC访问的参数,就看看baidu/rpc/controller.h中到底有些什么。请注意,这个类是Server和Channel公用的,其中分成了三段,分别标记为Client-side,Server-sdie和Both-side methods
一、Channel
Channel可以被所有线程共用,你不需要为每个线程创建独立的Channel,也不需要用锁互斥。不过Channel的创建和Init并不是线程安全的,请确保在Init成功后再被多线程访问,在没有线程访问后再析构。
管理client,配置的线程数、长度连接或放在brpc::ChannelOptions,或通过gflags全局配置
Init函数分为连接一台服务器或服务集群
1.连接一台服务器
// options为NULL时取默认值int Init(EndPoint server_addr_and_port, const ChannelOptions* options);
int Init(const char* server_addr_and_port, const ChannelOptions* options);
int Init(const char* server_addr, int port, const ChannelOptions* options);
这类Init连接的服务器往往有固定的ip地址,不需要命名服务和负载均衡,创建起来相对轻量。
但是请勿频繁创建使用域名的Channel。这需要查询dns,可能最多耗时10秒(查询DNS的默认超时)
2.连接集群
int Init(const char* naming_service_url,
const char* load_balancer_name,
const ChannelOptions* options);
这类Channel需要定期从naming_service_url指定的命名服务中获得服务器列表,并通过load_balancer_name指定的负载均衡算法选择出一台机器发送请求。
你不应该在每次请求前动态地创建此类(连接服务集群的)Channel。因为创建和析构此类Channel牵涉到较多的资源,比如在创建时得访问一次命名服务,
否则便不知道有哪些服务器可选。由于Channel可被多个线程共用,一般也没有必要动态创建。
client设置
(1)brpc:ChannelOptions 用于初始化Channel
(2)brpc::Controller 用于某次RPC中覆盖ChannelOptions中的选项,可根据上下文每次均不同
(3)全局gflags 用于调整一些底层代码的行为
Controller的特点:
一个Controller对应一次RPC,只能有一个使用者,默认线程不安全。
不能被共享,所以不能用共享指针
创建于RPC开始前,析构于RPC结束后有两种模式
a 同步RPC前Controller放栈上,出作用域后自行析构
b 异步RPC前new Controller, done中删除
边栏推荐
- pickle. Load error [attributeerror: can't get attribute 'volatile' on < module '\u main']
- PostgreSQL varchar[] 数组类型操作
- Extensive reading of the paper [film: visual reasoning with a general condition layer]
- Solidity - 合约结构 - 错误(error)- ^0.8.4版本新增
- Go语言高级
- The intelligent epidemic prevention system provides safety guarantee for the resumption of work and production at the construction site
- 使用环信提供的uni-app Demo,快速实现一对一单聊
- [English grammar] Unit1 articles, nouns, pronouns and numerals
- Dom4j parsing XML, XPath retrieving XML
- Lake Shore低温恒温器的氦气传输线
猜你喜欢

XML syntax, constraints

微信公众号开发相关流程及功能介绍

测试自学人必看:软件测试如何找测试项目?

nacos启动失败问题解决与总结

MySQL common graphics management tools | dark horse programmers

ddr4测试-2

Task: denial of service DOS

ECS summer money saving secret, this time @ old users come and take it away

Solution and summary of Nacos startup failure

Solidity - contract structure - error - ^0.8.4 NEW
随机推荐
241. Different Ways to Add Parentheses
PostgreSQL varchar[] 数组类型操作
Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency
Cdga | if you are engaged in the communication industry, you should get a data management certificate
Contos 7 set up SFTP to create users, user groups, and delete users
Witness the times! "The future of Renji collaboration has come" 2022 Hongji ecological partnership conference opens live broadcast reservation
B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
新版国标GB28181视频平台EasyGBS如何配置WebRTC视频流格式播放?
Task: denial of service DOS
Chinese and English instructions human soluble advanced glycation end products receptor (sRAGE) ELISA Kit
Collation of open source protocols of open source frameworks commonly used in Web Development
案例分享:QinQ基本组网配置
见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约
Lake Shore—OptiMag 超导磁体系统 — OM 系列
ffmpeg 音频相关命令
Solidity - 合约结构 - 错误(error)- ^0.8.4版本新增
商业智能BI开发和报表开发有什么本质区别?
【森城市】GIS数据漫谈(一)
[6.24-7.1] review of wonderful technical blog posts in the writing community
transform + asm资料