当前位置:网站首页>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中删除
边栏推荐
- IPv4地址、子网掩码、网关
- 有关 M91 快速霍尔测量仪的更多信息
- Manufacturing SRM management system supplier all-round closed-loop management, to achieve procurement sourcing and process efficient collaboration
- 混沌工程平台 ChaosBlade-Box 新版重磅发布
- Lake Shore低温恒温器的氦气传输线
- Methods of finding various limits
- Lumiprobe free radical analysis h2dcfda instructions
- Native JS creates a calendar - supports mouse wheel scrolling to select months - and can be ported to any framework
- 新版国标GB28181视频平台EasyGBS如何配置WebRTC视频流格式播放?
- Go语言高级
猜你喜欢

SuperOptiMag 超导磁体系统 — SOM、SOM2 系列

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

智慧防疫系统为建筑工地复工复产提供安全保障

精耕渠道共谋发展 福昕携手伟仕佳杰开展新产品培训大会

Dlib+opencv library for fatigue detection

Lake Shore—CRX-EM-HF 型低温探针台

白盒加密技术浅理解

How to solve the problem of splash screen when the main and sub code streams of easygbs are h.265?

Introduction to relevant processes and functions of wechat official account development

见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约
随机推荐
云服务器ECS夏日省钱秘籍,这次@老用户快来领走
Cdga | if you are engaged in the communication industry, you should get a data management certificate
Thesis reading [distinctive late semantic graph for video capturing]
Extensive reading of the paper [film: visual reasoning with a general condition layer]
Introduction to relevant processes and functions of wechat official account development
Solution of digital supply chain centralized purchase platform in mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase
Reading the paper [learning to discretely compose reasoning module networks for video captioning]
Nacos configuration file publishing failed, please check whether the parameters are correct solution
ffmpeg常用命令(二)
实现一个Prometheus exporter
ubuntu14安装MySQL并配置root账户本地与远程访问
Dlib+opencv library for fatigue detection
ddr4测试-2
研究了11种实时聊天软件,我发现都具备这些功能…
白盒加密技术浅理解
Native JS creates a calendar - supports mouse wheel scrolling to select months - and can be ported to any framework
Junit单元测试框架详解
Lake Shore—OptiMag 超导磁体系统 — OM 系列
indexof和includes的区别
Collation of open source protocols of open source frameworks commonly used in Web Development