当前位置:网站首页>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中删除
边栏推荐
- XML语法、约束
- 求各种极限的方法
- 【Go ~ 0到1 】 第四天 6月30 defer,结构体,方法
- Lake Shore—OptiMag 超导磁体系统 — OM 系列
- 新版国标GB28181视频平台EasyGBS如何配置WebRTC视频流格式播放?
- [to.Net] C set class source code analysis
- More information about M91 fast hall measuring instrument
- Lumiprobe cell imaging study PKH26 cell membrane labeling kit
- nacos启动失败问题解决与总结
- Learning records of building thingsboard, an Internet of things platform
猜你喜欢

Summary of SQL query de duplication statistics methods

论文阅读【Learning to Discretely Compose Reasoning Module Networks for Video Captioning】

Native JS creates a calendar - supports mouse wheel scrolling to select months - and can be ported to any framework

新版国标GB28181视频平台EasyGBS如何配置WebRTC视频流格式播放?

求各种极限的方法
使用环信提供的uni-app Demo,快速实现一对一单聊

The intelligent epidemic prevention system provides safety guarantee for the resumption of work and production at the construction site

Case sharing: basic networking configuration of QinQ

混沌工程平台 ChaosBlade-Box 新版重磅发布

EasyGBS网络不稳定情况下重复请求视频拉流问题的优化
随机推荐
Specification of lumiprobe reactive dye indocyanine green
混沌工程平台 ChaosBlade-Box 新版重磅发布
实现一个Prometheus exporter
Chaos engineering platform chaosblade box new heavy release
Once the SQL is optimized, the database query speed is increased by 60 times
Love business in Little Red Book
Lumiprobe free radical analysis h2dcfda instructions
原生js打造日程表-支持鼠标滚轮滚动选择月份-可以移植到任何框架中
Dlib+Opencv库实现疲劳检测
如何正确使用Vertx操作Redis(3.9.4带源码分析)
ffmpeg 音频相关命令
赋能「新型中国企业」,SAP Process Automation 落地中国
Intensive cultivation of channels for joint development Fuxin and Weishi Jiajie held a new product training conference
Introduction to relevant processes and functions of wechat official account development
SuperVariMag 超导磁体系统 — SVM 系列
使用环信提供的uni-app Demo,快速实现一对一单聊
torch.nn.functional.interpolate函数
智慧防疫系统为建筑工地复工复产提供安全保障
nacos配置文件发布失败,请检查参数是否正确的解决方案
Lake Shore—OptiMag 超导磁体系统 — OM 系列