当前位置: 首页 > news >正文

【brpc学习实践六】backup request场景案例

应用场景

有时为了保证可用性,需要同时访问两路服务,哪个先返回就取哪个。在brpc中,这有多种做法,根据server是否挂在同一个命名服务内有所区别。

当后端server可以挂在一个命名服务内时

Channel开启backup request。这个Channel会先向其中一个server发送请求,如果在ChannelOptions.backup_request_ms后还没回来,再向另一个server发送。之后哪个先回来就取哪个。在设置了合理的backup_request_ms后,大部分时候只会发一个请求,对后端服务只有一倍压力。

#include <gflags/gflags.h>
#include <butil/logging.h>
#include <butil/time.h>
#include <brpc/channel.h>
#include "echo.pb.h"DEFINE_string(protocol, "baidu_std", "Protocol type. Defined in src/brpc/options.proto");
DEFINE_string(connection_type, "", "Connection type. Available values: single, pooled, short");
DEFINE_string(server, "0.0.0.0:8000", "IP Address of server");
DEFINE_string(load_balancer, "", "The algorithm for load balancing");
DEFINE_int32(timeout_ms, 100, "RPC timeout in milliseconds");
DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)");
DEFINE_int32(backup_request_ms, 2, "Timeout for sending backup request");int main(int argc, char* argv[]) {// Parse gflags. We recommend you to use gflags as well.GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);// A Channel represents a communication line to a Server. Notice that // Channel is thread-safe and can be shared by all threads in your program.brpc::Channel channel;// Initialize the channel, NULL means using default options.brpc::ChannelOptions options;options.protocol = FLAGS_protocol;options.connection_type = FLAGS_connection_type;options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/;options.max_retry = FLAGS_max_retry;// 设置backup_request_ms参数,在超时后会自动向另外一个server重发请求,与首次请求一起,哪个先返回用哪个options.backup_request_ms = FLAGS_backup_request_ms;if (channel.Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) {LOG(ERROR) << "Fail to initialize channel";return -1;}// Normally, you should not call a Channel directly, but instead construct// a stub Service wrapping it. stub can be shared by all threads as well.example::EchoService_Stub stub(&channel);// Send a request and wait for the response every 1 second.int counter = 0;while (!brpc::IsAskedToQuit()) {// We will receive response synchronously, safe to put variables// on stack.example::EchoRequest request;example::EchoResponse response;brpc::Controller cntl;request.set_index(++counter);// Because `done'(last parameter) is NULL, this function waits until// the response comes back or error occurs(including timedout).stub.Echo(&cntl, &request, &response, NULL);if (!cntl.Failed()) {LOG(INFO) << "Received response[index=" << response.index()<< "] from " << cntl.remote_side()<< " to " << cntl.local_side()<< " latency=" << cntl.latency_us() << "us";} else {LOG(WARNING) << cntl.ErrorText();}sleep(1);}LOG(INFO) << "EchoClient is going to quit";return 0;
}
backup_request_ms推荐值

backup_request_ms=2ms可以大约覆盖95.5%的请求,选择backup_request_ms=10ms则可以覆盖99.99%的请求。

当后端server不能挂在一个命名服务内时

方案一、使用SelectiveChannel:

建立一个开启backup request的SelectiveChannel,其中包含两个sub channel。访问这个SelectiveChannel和上面的情况类似,会先访问一个sub channel,如果在ChannelOptions.backup_request_ms后没返回,再访问另一个sub channel。如果一个sub channel对应一个集群,这个方法就是在两个集群间做互备。

#include <gflags/gflags.h>
#include <bthread/bthread.h>
#include <butil/logging.h>
#include <brpc/selective_channel.h>
#include <brpc/parallel_channel.h>
#include "echo.pb.h"DEFINE_int32(thread_num, 50, "Number of threads to send requests");
DEFINE_bool(use_bthread, false, "Use bthread to send requests");
DEFINE_int32(attachment_size, 0, "Carry so many byte attachment along with requests");
DEFINE_int32(request_size, 16, "Bytes of each request");
DEFINE_string(connection_type, "", "Connection type. Available values: single, pooled, short");
DEFINE_string(protocol, "baidu_std", "Protocol type. Defined in src/brpc/options.proto");
DEFINE_string(starting_server, "0.0.0.0:8114", "IP Address of the first server, port of i-th server is `first-port + i'");
DEFINE_string(load_balancer, "rr", "Name of load balancer");
DEFINE_int32
http://www.lryc.cn/news/241725.html

相关文章:

  • el-table导出为excel表格
  • CVE-2022-0543(Redis 沙盒逃逸漏洞)
  • 查swap内存使用
  • Element UI的Tabs 标签页位置导航栏去除线条
  • 【Python 训练营】N_1 验证密码
  • Pinia 和 Vuex 的对比,storeToRefs 的原理
  • Mycat分库分表的操作(配置)
  • android的canvas的clipRegion废弃替代代码
  • KubeSphere 社区双周报 | Fluent Operator 2.6.0 发布 | 2023.11.10-11.23
  • 【通俗易懂】git原理、安装及连接gitlab,github
  • TCP /UDP协议的 socket 调用的过程
  • 外贸独立站外部优化:提升网站可见度与吸引力的策略
  • buildAdmin 后端控制器的代码分析
  • Python丨让简历脱颖而出的关键,居然是“它”!
  • CMake中常见的预定义变量
  • .netcore 获取appsettings
  • 额温枪方案,MS8551,MS8601;MS1112,MS1100
  • 数字图像处理基础-用通俗语言进行超详细的总结
  • 3.3.1详解linux内核链表list_head及其接口应用
  • 发挥云计算潜力:Amazon Lightsail 与 Amazon EC2 的综述
  • 【深度学习】卷积神经网络(CNN)
  • 科普:多领域分布式协同仿真
  • openstack(2)
  • Jmeter 压测保姆级入门教程
  • springboot2.1升级到2.7 actuator丢失部分metrics端点
  • 梦开始的地方——Adobe Premiere Pro
  • Nginx同时支持Http和Https的配置详解
  • 3.2 Windows驱动开发:内核CR3切换读写内存
  • 基于springBoot+Vue的停车管理系统
  • ES开启安全认证