brpc 使用压缩方式传递数据

brpc 是支持压缩方式传递数据的,支持的压缩格式有四种:

  COMPRESS_TYPE_SNAPPY = 1,
  COMPRESS_TYPE_GZIP = 2,
  COMPRESS_TYPE_ZLIB = 3,
  COMPRESS_TYPE_LZ4 = 4

brpc 的请求和响应都支持压缩

set_request_compress_type)
set_response_compress_type)

example

请求

int mainint argc, char* argv[]) {
    // Parse gflags. We recommend you to use gflags as well.
    GFLAGS_NS::ParseCommandLineFlags&argc, &argv, true);
    if FLAGS_gzip) {
        GFLAGS_NS::SetCommandLineOption"http_body_compress_threshold", 0);
    }
    
    // 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.timeout_ms = FLAGS_timeout_ms/*milliseconds*/;
    options.max_retry = FLAGS_max_retry;
    if channel.InitFLAGS_server.c_str), FLAGS_load_balancer.c_str), &options) != 0) {
        LOGERROR) << "Fail to initialize channel";
        return -1;
    }

    helloworld::Greeter_Stub stub&channel);

    // Send a request and wait for the response every 1 second.
    while !brpc::IsAskedToQuit)) {
        // We will receive response synchronously, safe to put variables
        // on stack.
        helloworld::HelloRequest request;
        helloworld::HelloReply response;
        brpc::Controller cntl;

        request.set_name"grpc_req_from_brpc");

        // 这条语句用与设置请求压缩格式,解压的时候不用关心,brpc会自动处理
        cntl.set_request_compress_typebrpc::COMPRESS_TYPE_GZIP);
        
        stub.SayHello&cntl, &request, &response, NULL);
        if !cntl.Failed)) {
            LOGINFO) << "Received response from " << cntl.remote_side)
                << " to " << cntl.local_side)
                << ": " << response.message)
                << " latency=" << cntl.latency_us) << "us";
        } else {
            LOGWARNING) << cntl.ErrorText);
        }
        usleepFLAGS_interval_ms * 1000L);
    }

    return 0;
}

响应

virtual void Echogoogle::protobuf::RpcController* cntl_base,
                      const EchoRequest* request,
                      EchoResponse* response,
                      google::protobuf::Closure* done) {
        // This object helps you to call done->Run) in RAII style. If you need
        // to process the request asynchronously, pass done_guard.release).
        brpc::ClosureGuard done_guarddone);

        brpc::Controller* cntl =
            static_cast<brpc::Controller*>cntl_base);

        LOGINFO) << "Received request[log_id=" << cntl->log_id) 
                  << "] from " << cntl->remote_side) 
                  << " to " << cntl->local_side)
                  << ": " << request->message)
                  << " attached=" << cntl->request_attachment) << ")";

        // Fill response.
        response->set_messagerequest->message));

        // 这条语句用于设置响应的压缩格式
        // cntl->set_response_compress_typebrpc::COMPRESS_TYPE_GZIP);

        if FLAGS_echo_attachment) {
            // Set attachment which is wired to network directly instead of
            // being serialized into protobuf messages.
            cntl->response_attachment).appendcntl->request_attachment));
        }
    }

资料:https://github.com/apache/incubator-brpc/blob/master/docs/cn/client.md

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注