21 Star 113 Fork 50

搜狗开源 / srpc

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

English version

03 - RPC Server

  • 每一个Server对应一个端口
  • 每一个Server对应一个确定的网络通信协议
  • 每一个Service可以添加到任意的Server里
  • 每一个Server可以拥有任意的Service,但在当前Server里ServiceName必须唯一
  • 不同IDL的Service是可以放进同一个Server中的

示例

下面我们通过一个具体例子来呈现

  • 沿用上面的ExampleServiceImplService
  • 首先,我们创建1个RPC Server、需要确定协议
  • 然后,我们可以创建任意个数的Service实例、任意不同proto形成的Service,把这些Service通过add_service()接口添加到Server里
  • 最后,通过Server的start或者serve开启服务,处理即将到来的rpc请求
  • 想像一下,我们也可以从Example::Service派生更多的功能的rpcEcho不同实现的Service
  • 想像一下,我们可以在N个不同的端口创建N个不同的RPC Server、代表着不同的协议
  • 想像一下,我们可以把同一个ServiceIMPL实例add_service到不同的Server上,我们也可以把不同的ServiceIMPL实例add_service到同一个Server上
  • 想像一下,我们可以用同一个ExampleServiceImpl,在三个不同端口、同时服务于BRPC-STD、SRPC-STD、SRPC-Http
  • 甚至,我们可以将1个PB的ExampleServiceImpl和1个Thrift的AnotherThriftServiceImpladd_service到同一个SRPC-STD Server,两种IDL在同一个端口上完美工作!
int main()
{
    SRPCServer server_srpc;
    SRPCHttpServer server_srpc_http;
    BRPCServer server_brpc;
    ThriftServer server_thrift;
    TRPCServer server_trpc;
    TRPCHttpServer server_trpc_http;

    ExampleServiceImpl impl_pb;
    AnotherThriftServiceImpl impl_thrift;

    server_srpc.add_service(&impl_pb);
    server_srpc.add_service(&impl_thrift);
    server_srpc_http.add_service(&impl_pb);
    server_srpc_http.add_service(&impl_thrift);
    server_brpc.add_service(&impl_pb);
    server_thrift.add_service(&impl_thrift);
    server_trpc.add_service(&impl_pb);
    server_trpc_http.add_service(&impl_thrift);

    server_srpc.start(1412);
    server_srpc_http.start(8811);
    server_brpc.start(2020);
    server_thrift.start(9090);
    server_trpc.start(2022);
    server_trpc_http.start(8822);
    
    getchar();
    server_trpc_http.stop();
    server_trpc.stop();
    server_thrift.stop();
    server_brpc.stop();
    server_srpc_http.stop();
    server_srpc.stop();

    return 0;
}
C++
1
https://gitee.com/sogou/srpc.git
git@gitee.com:sogou/srpc.git
sogou
srpc
srpc
master

搜索帮助