21 Star 112 Fork 50

搜狗开源 / srpc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README_cn.md 7.85 KB
一键复制 编辑 原始数据 按行查看 历史
Charles 提交于 2024-04-07 16:46 . Fix typo (#375)

English version   |   Wiki:SRPC架构介绍

srpc-logo

NEW !!! 👉 SRPC tools : 一个帮你快速构建Workflow和SRPC项目的小工具.

Introduction

SRPC是全搜狗业务线上使用的企业级RPC系统,目前每天承载上百亿的请求量,涵盖搜广推及其他类型业务。

底层基于Sogou C++ Workflow,是高性能、低延迟、轻量级RPC系统的极佳选择。且加入了AOP面向切面的模块,支持Metrics(监控指标)和Trace(链路追踪)功能,可上报到多种云原生系统,包括OpenTelemetry。

主要功能和示例:

  • 支持多种RPC协议:SRPCbRPCThrifttRPC
  • 支持多种操作系统:Linux / MacOS / Windows
  • 支持多种IDL格式:ProtobufThrift
  • 支持多种数据布局,使用上完全透明:JsonProtobufThrift Binary
  • 支持多种压缩,框架自动解压:gzipzlibsnappylz4
  • 支持多种通信协议:tcp, udp, sctp, tcp ssl
  • 可以通过http+json实现跨语言
  • 可以使用Workflow串并联任务流,打通计算及其他异步资源
  • 完美兼容Workflow所有功能:命名服务体系、upstream、其他组件等
  • 链路追踪功能:上报TracingOpenTelemetry
  • 监控功能:上报Metrics到OpenTelemetry和Prometheus

Installation

SRPC是Debian Linux和Fedora的自带安装包,因此可以通过源码安装,和系统自带安装包安装。

具体参考:Linux、MacOS、Windows安装和编译指南

Quick Start

我们通过几个步骤快速了解如何使用。

更详细的用法可以参考更多文档,和官方教程

1. example.proto

syntax = "proto3";//这里proto2和proto3都可以,srpc都支持

message EchoRequest {
    string message = 1;
    string name = 2;
};

message EchoResponse {
    string message = 1;
};

service Example {
    rpc Echo(EchoRequest) returns (EchoResponse);
};

2. generate code

protoc example.proto --cpp_out=./ --proto_path=./
srpc_generator protobuf ./example.proto ./

3. server.cc

#include <stdio.h>
#include <signal.h>
#include "example.srpc.h"

using namespace srpc;

class ExampleServiceImpl : public Example::Service
{
public:
    void Echo(EchoRequest *request, EchoResponse *response, RPCContext *ctx) override
    {
        response->set_message("Hi, " + request->name());
        printf("get_req:\n%s\nset_resp:\n%s\n",
                request->DebugString().c_str(), response->DebugString().c_str());
    }
};

void sig_handler(int signo) { }

int main()
{
    signal(SIGINT, sig_handler);
    signal(SIGTERM, sig_handler);

    SRPCServer server_tcp;
    SRPCHttpServer server_http;

    ExampleServiceImpl impl;
    server_tcp.add_service(&impl);
    server_http.add_service(&impl);

    server_tcp.start(1412);
    server_http.start(8811);
    getchar(); // press "Enter" to end.
    server_http.stop();
    server_tcp.stop();

    return 0;
}

4. client.cc

#include <stdio.h>
#include "example.srpc.h"

using namespace srpc;

int main()
{
    Example::SRPCClient client("127.0.0.1", 1412);
    EchoRequest req;
    req.set_message("Hello, srpc!");
    req.set_name("workflow");

    client.Echo(&req, [](EchoResponse *response, RPCContext *ctx) {
        if (ctx->success())
            printf("%s\n", response->DebugString().c_str());
        else
            printf("status[%d] error[%d] errmsg:%s\n",
                    ctx->get_status_code(), ctx->get_error(), ctx->get_errmsg());
    });

    getchar(); // press "Enter" to end.
    return 0;
}

5. make

在Linux系统下的编译示例如下,其他平台建议到tutorial目录下使用完整的cmake文件协助解决编译依赖问题。

g++ -o server server.cc example.pb.cc -std=c++11 -lsrpc
g++ -o client client.cc example.pb.cc -std=c++11 -lsrpc

6. run

终端1:

./server

终端2:

./client

也可以用CURL发送http请求:

curl 127.0.0.1:8811/Example/Echo -H 'Content-Type: application/json' -d '{message:"from curl",name:"CURL"}'

终端1输出:

get_req:
message: "Hello, srpc!"
name: "workflow"

set_resp:
message: "Hi, workflow"

get_req:
message: "from curl"
name: "CURL"

set_resp:
message: "Hi, CURL"

终端2输出:

message: "Hi, workflow"

CURL收到的回复:

{"message":"Hi, CURL"}

Benchmark

  • CPU 2-chip/8-core/32-processor Intel(R) Xeon(R) CPU E5-2630 v3 @2.40GHz
  • Memory all 128G
  • 10 Gigabit Ethernet
  • BAIDU brpc-client使用连接池pooled模式

跨机单client→单server在不同并发的QPS

Client = 1
ClientThread = 64, 128, 256, 512, 1024
RequestSize = 32
Duration = 20s
Server = 1
ServerIOThread = 16
ServerHandlerThread = 16

IMG

跨机多client→单server在不同client进程数的QPS

Client = 1, 2, 4, 8, 16
ClientThread = 32
RequestSize = 32
Duration = 20s
Server = 1
ServerIOThread = 16
ServerHandlerThread = 16

IMG

同机单client→单server在不同并发下的QPS

Client = 1
ClientThread = 1, 2, 4, 8, 16, 32, 64, 128, 256
RequestSize = 1024
Duration = 20s
Server = 1
ServerIOThread = 16
ServerHandlerThread = 16

IMG

同机单client→单server在不同请求大小下的QPS

Client = 1
ClientThread = 100
RequestSize = 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768
Duration = 20s
Server = 1
ServerIOThread = 16
ServerHandlerThread = 16

IMG

同机单client→单server在固定QPS下的延时CDF

Client = 1
ClientThread = 50
ClientQPS = 10000
RequestSize = 1024
Duration = 20s
Server = 1
ServerIOThread = 16
ServerHandlerThread = 16
Outiler = 1%

IMG

跨机多client→单server在固定QPS下的延时CDF

Client = 32
ClientThread = 16
ClientQPS = 2500
RequestSize = 512
Duration = 20s
Server = 1
ServerIOThread = 16
ServerHandlerThread = 16
Outiler = 1%

IMG

与我们联系

C++
1
https://gitee.com/sogou/srpc.git
git@gitee.com:sogou/srpc.git
sogou
srpc
srpc
master

搜索帮助