当前位置:网站首页>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中删除
边栏推荐
- Werewolf killing strategy: do you think I'm easy to cheat? Who do we believe!
- 【To .NET】C#集合类源码解析
- Lean thinking: source, pillar, landing. I understand it after reading this article
- Lake Shore低温恒温器的氦气传输线
- XML语法、约束
- Solidity - 合约结构 - 错误(error)- ^0.8.4版本新增
- 简版拼多多商品数据
- Lumiprobe phosphide hexaethylene phosphide specification
- SuperOptiMag 超导磁体系统 — SOM、SOM2 系列
- uni-app微信小程序一键登录获取权限功能
猜你喜欢
sql查询去重统计的方法总结
【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数
商业智能BI开发和报表开发有什么本质区别?
Parallelism, concurrency and life cycle of threads
Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency
Netease games, radical going to sea
Is PMP cancelled??
见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约
XML syntax, constraints
【pytorch记录】自动混合精度训练 torch.cuda.amp
随机推荐
kubernetes命令入门(namespaces,pods)
简版拼多多商品数据
Nacos configuration file publishing failed, please check whether the parameters are correct solution
Task: denial of service DOS
Boost the development of digital economy and consolidate the base of digital talents - the digital talent competition was successfully held in Kunming
Three ways for redis to realize current limiting
实现一个Prometheus exporter
Solution and summary of Nacos startup failure
【6.24-7.1】写作社区精彩技术博文回顾
PMP是被取消了吗??
案例分享:QinQ基本组网配置
Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency
【To .NET】C#集合类源码解析
ffmpeg常用命令(二)
Solidity - 合约结构 - 错误(error)- ^0.8.4版本新增
【pytorch记录】自动混合精度训练 torch.cuda.amp
Lake Shore - crx-em-hf low temperature probe station
Solidity - contract structure - error - ^0.8.4 NEW
torch.nn.functional.interpolate函数
Go语言高级