RPC Server

  • Each server corresponds to one port
  • Each server corresponds to one specific network communication protocol
  • One service may be added into multiple Servers
  • One Server may have one or more Services, but the ServiceName must be unique within that Server
  • Services from different IDLs can be added into the same Server

Sample

You can follow the detailed example below:

  • Follow the above ExampleServiceImpl Service
  • First, create an RPC Server and determine the proto file.
  • Then, create any number of Service instances and any number of Services for different protocols, and add these services to the Server through the add_service()interface.
  • Finally, use start() or serve() to start the services in the Server and handle the upcoming rpc requests through the Server.
  • Imagine that we can also derive more Serivce from Example::Service, which have different implementations of rpc Echo.
  • Imagine that we can create N different RPC Servers on N different ports, serving on different network protocols.
  • Imagine that we can use add_service() to add the same ServiceIMPL instance on different Servers, or we can use add_service() to add different ServiceIMPL instances on the same server.
  • Imagine that we can use the same ExampleServiceImpl, serving BPRC-STD, SogouRPC-STD, SogouRPC-Http at three different ports at the same time.
  • And we can use add_service() to add one ExampleServiceImpl related to Protobuf IDL and one AnotherThriftServiceImpl related to Thrift IDL to the same SogouRPC-STD Server, and the two IDLs work perfectly on the same port!
  1. int main()
  2. {
  3. SRPCServer server_srpc;
  4. SRPCHttpServer server_srpc_http;
  5. BRPCServer server_brpc;
  6. ThriftServer server_thrift;
  7. ExampleServiceImpl impl_pb;
  8. AnotherThriftServiceImpl impl_thrift;
  9. server_srpc.add_service(&impl_pb);
  10. server_srpc.add_service(&impl_thrift);
  11. server_srpc_http.add_service(&impl_pb);
  12. server_srpc_http.add_service(&impl_thrift);
  13. server_brpc.add_service(&impl_pb);
  14. server_thrift.add_service(&impl_thrift);
  15. server_srpc.start(1412);
  16. server_srpc_http.start(8811);
  17. server_brpc.start(2020);
  18. server_thrift.start(9090);
  19. pause();
  20. server_thrift.stop();
  21. server_brpc.stop();
  22. server_srpc_http.stop();
  23. server_srpc.stop();
  24. return 0;
  25. }