当前位置:网站首页>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!
- What must be done in graduation season before going to Shanhai
- Lake Shore—CRX-EM-HF 型低温探针台
- 下载(导出)pdf模板文件(比如:审批单),报错:Invalid nested tag *** found, expected closing tag ***
- 云服务器ECS夏日省钱秘籍,这次@老用户快来领走
- [go ~ 0 to 1] day 5 July 1 type alias, custom type, interface, package and initialization function
- web开发常用的开源框架的开源协议整理
- The intelligent epidemic prevention system provides safety guarantee for the resumption of work and production at the construction site
- 赋能「新型中国企业」,SAP Process Automation 落地中国
- Lumiprobe phosphide hexaethylene phosphide specification
猜你喜欢

uni-app微信小程序一键登录获取权限功能

PMP是被取消了吗??

Is PMP cancelled??

Ubuntu14 install MySQL and configure root account local and remote access

Solution of digital supply chain centralized purchase platform in mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase

Shell高级进阶

赋能「新型中国企业」,SAP Process Automation 落地中国

June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries

ddr4测试-2

Solidity - 合约结构 - 错误(error)- ^0.8.4版本新增
随机推荐
案例分享:QinQ基本组网配置
More information about M91 fast hall measuring instrument
M91 fast hall measuring instrument - better measurement in a shorter time
新版国标GB28181视频平台EasyGBS如何配置WebRTC视频流格式播放?
【6.24-7.1】写作社区精彩技术博文回顾
[English grammar] Unit1 articles, nouns, pronouns and numerals
Redis 实现限流的三种方式
Lake Shore—OptiMag 超导磁体系统 — OM 系列
Write it down once Net travel management background CPU Explosion Analysis
The difference between indexof and includes
下载(导出)pdf模板文件(比如:审批单),报错:Invalid nested tag *** found, expected closing tag ***
Three ways for redis to realize current limiting
【To .NET】C#集合类源码解析
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数
DTD建模
Introduction to relevant processes and functions of wechat official account development
241. Different Ways to Add Parentheses
sql查询去重统计的方法总结
宝,运维100+服务器很头疼怎么办?用行云管家!