RPC Client

  • Each Client corresponds to one specific target/one specific cluster
  • Each Client corresponds to one specific network communication protocol
  • Each Client corresponds to one specific IDL

Sample

You can follow the detailed example below:

  • Following the above example, the client is relatively simple and you can call the method directly.
  • Use Example::XXXClient to create a client instance of some RPC. The IP+port or URL of the target is required.
  • With the client instance, directly call the rpc function Echo. This is an asynchronous request, and the callback function will be invoked after the request is completed.
  • For the usage of the RPC Context, please check RPC Context.
  1. #include <stdio.h>
  2. #include "example.srpc.h"
  3. using namespace srpc;
  4. int main()
  5. {
  6. Example::SRPCClient client("127.0.0.1", 1412);
  7. EchoRequest req;
  8. req.set_message("Hello, sogou rpc!");
  9. req.set_name("Li Yingxin");
  10. client.Echo(&req, [](EchoResponse *response, RPCContext *ctx) {
  11. if (ctx->success())
  12. printf("%s\n", response->DebugString().c_str());
  13. else
  14. printf("status[%d] error[%d] errmsg:%s\n",
  15. ctx->get_status_code(), ctx->get_error(), ctx->get_errmsg());
  16. });
  17. pause();
  18. return 0;
  19. }