• 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 main(int 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.Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) {
            LOG(ERROR) << "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_type(brpc::COMPRESS_TYPE_GZIP);
            
            stub.SayHello(&cntl, &request, &response, NULL);
            if (!cntl.Failed()) {
                LOG(INFO) << "Received response from " << cntl.remote_side()
                    << " to " << cntl.local_side()
                    << ": " << response.message()
                    << " latency=" << cntl.latency_us() << "us";
            } else {
                LOG(WARNING) << cntl.ErrorText();
            }
            usleep(FLAGS_interval_ms * 1000L);
        }
    
        return 0;
    }
    
    • 响应
    virtual void Echo(google::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_guard(done);
    
            brpc::Controller* cntl =
                static_cast<brpc::Controller*>(cntl_base);
    
            LOG(INFO) << "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_message(request->message());
    
            // 这条语句用于设置响应的压缩格式
            // cntl->set_response_compress_type(brpc::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().append(cntl->request_attachment());
            }
        }
    

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

  • 相关阅读:
    PHP 文件上传七牛云
    Ubuntu Nginx 配置
    Ubuntu Sublime 配置
    Ubuntu Apache 域名配置
    Ubuntu Apache 配置
    LAMP 搭建
    vue函数防抖和节流
    vs code 同步
    常用软件
    vue
  • 原文地址:https://www.cnblogs.com/xiaohaigegede/p/14581107.html
Copyright © 2020-2023  润新知