client.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2014 Baidu, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // A client sending requests to server every 1 second.
  15. #include <gflags/gflags.h>
  16. #include <butil/logging.h>
  17. #include <butil/time.h>
  18. #include <brpc/channel.h>
  19. #include "echo.pb.h"
  20. DEFINE_string(attachment, "foo", "Carry this along with requests");
  21. DEFINE_string(protocol, "baidu_std", "Protocol type. Defined in protocol/brpc/options.proto");
  22. DEFINE_string(connection_type, "", "Connection type. Available values: single, pooled, short");
  23. DEFINE_string(server, "0.0.0.0:8000", "IP Address of server");
  24. DEFINE_string(load_balancer, "", "The algorithm for load balancing");
  25. DEFINE_int32(timeout_ms, 100, "RPC timeout in milliseconds");
  26. DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)");
  27. DEFINE_int32(interval_ms, 1000, "Milliseconds between consecutive requests");
  28. DEFINE_string(http_content_type, "application/json", "Content type of http request");
  29. int main(int argc, char* argv[]) {
  30. // Parse gflags. We recommend you to use gflags as well.
  31. google::ParseCommandLineFlags(&argc, &argv, true);
  32. // A Channel represents a communication line to a Server. Notice that
  33. // Channel is thread-safe and can be shared by all threads in your program.
  34. brpc::Channel channel;
  35. // Initialize the channel, NULL means using default options.
  36. brpc::ChannelOptions options;
  37. options.protocol = FLAGS_protocol;
  38. options.connection_type = FLAGS_connection_type;
  39. options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/;
  40. options.max_retry = FLAGS_max_retry;
  41. if (channel.Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) {
  42. LOG(ERROR) << "Fail to initialize channel";
  43. return -1;
  44. }
  45. // Normally, you should not call a Channel directly, but instead construct
  46. // a stub Service wrapping it. stub can be shared by all threads as well.
  47. example::EchoService_Stub stub(&channel);
  48. // Send a request and wait for the response every 1 second.
  49. int log_id = 0;
  50. while (!brpc::IsAskedToQuit()) {
  51. // We will receive response synchronously, safe to put variables
  52. // on stack.
  53. example::EchoRequest request;
  54. example::EchoResponse response;
  55. brpc::Controller cntl;
  56. request.set_message("hello world");
  57. cntl.set_log_id(log_id ++); // set by user
  58. if (FLAGS_protocol != "http" && FLAGS_protocol != "h2c") {
  59. // Set attachment which is wired to network directly instead of
  60. // being serialized into protobuf messages.
  61. cntl.request_attachment().append(FLAGS_attachment);
  62. } else {
  63. cntl.http_request().set_content_type(FLAGS_http_content_type);
  64. }
  65. // Because `done'(last parameter) is NULL, this function waits until
  66. // the response comes back or error occurs(including timedout).
  67. stub.Echo(&cntl, &request, &response, NULL);
  68. if (!cntl.Failed()) {
  69. if (cntl.response_attachment().empty()) {
  70. LOG(INFO) << "Received response from " << cntl.remote_side()
  71. << ": " << response.message()
  72. << " latency=" << cntl.latency_us() << "us";
  73. } else {
  74. LOG(INFO) << "Received response from " << cntl.remote_side()
  75. << " to " << cntl.local_side()
  76. << ": " << response.message() << " (attached="
  77. << cntl.response_attachment() << ")"
  78. << " latency=" << cntl.latency_us() << "us";
  79. }
  80. } else {
  81. LOG(WARNING) << cntl.ErrorText();
  82. }
  83. usleep(FLAGS_interval_ms * 1000L);
  84. }
  85. LOG(INFO) << "EchoClient is going to quit";
  86. return 0;
  87. }