在现代分布式系统中,下游服务访问的复杂性日益增加。bRPC通过组合Channels和HTTP/H2访问优化,提供了解决多层级RPC调用、负载均衡和协议兼容性问题的完整方案。本文将深入解析两大核心功能,助力开发者构建高性能服务。
fail_limit
控制最大失败次数(1) ParallelChannel(并行通道)
// 示例:广播请求
class Broadcaster : public CallMapper {
public:
SubCall Map(int channel_index, ...) {
return SubCall(method, request, response->New(), DELETE_RESPONSE);
}
};
// 添加SubChannel
pchan.AddChannel(sub_channel, OWNS_CHANNEL, new Broadcaster, nullptr);
CallMapper
修改请求,ResponseMerger
合并结果controller->sub(i)
(2) SelectiveChannel(选择通道)
// 初始化
brpc::SelectiveChannel schan;
schan.Init("c_murmurhash", &options);
// 动态添加Sub-Channel
schan.AddChannel(new brpc::Channel, nullptr);
(3) PartitionChannel(分库通道)
class MyPartitionParser : public PartitionParser {
public:
bool ParseFromTag(const string& tag, Partition* out) {
// 解析"N/M"格式分库标识
}
};
// 初始化三库分片
PartitionChannel channel;
channel.Init(3, new MyPartitionParser, "bns://node", &options);
PartitionParser
定制(4) DynamicPartitionChannel(动态分库)
DynamicPartitionChannel channel;
channel.Init(new MyPartitionParser, "file://server_list", "rr", &options);
(1) 初始化Channel
brpc::ChannelOptions opt;
opt.protocol = brpc::PROTOCOL_H2; // 或PROTOCOL_HTTP
channel.Init("www.baidu.com", &opt);
(2) GET/POST请求
// GET请求
cntl.http_request().uri() = "https://api.example.com/data";
// POST带JSON body
cntl.http_request().set_method(brpc::HTTP_METHOD_POST);
cntl.request_attachment().append(R"({"key":"value"})");
(1) 协议版本切换
cntl.http_request().set_version(1, 0); // 降级到HTTP/1.0
(2) Header/Query处理
// 获取Content-Type
const string* ct = cntl->http_request().GetHeader("Content-Type");
// 设置URL参数
cntl->http_request().uri().SetQuery("page", "1");
(1) 大文件流式下载
class MyReader : public ProgressiveReader {
public:
butil::Status OnReadOnePart(const void* data, size_t len) override {
// 处理数据分片
}
void OnEndOfMessage(const butil::Status& st) override {
// 释放资源
}
};
cntl.response_will_be_read_progressively();
cntl.ReadProgressiveAttachmentBy(new MyReader);
(2) 压缩与解压
// 请求压缩
cntl.set_request_compress_type(brpc::COMPRESS_TYPE_GZIP);
// 响应解压
if (*cntl->http_response().GetHeader("Content-Encoding") == "gzip") {
brpc::policy::GzipDecompress(cntl->response_attachment(), &uncompressed);
}
// HTTPS自动启用SSL
channel.Init("https://secure.api", &opt);
// 添加认证头
cntl.http_request().SetHeader("Authorization", "Bearer xxxx");
SelectiveChannel
分流到不同服务集群ParallelChannel
并发调用身份验证+业务服务DynamicPartitionChannel
实现3库→4库流量平滑迁移bRPC通过组合Channels和深度HTTP/H2集成,解决了分布式系统中的关键痛点:
brpc documentation