client.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // A client sending thrift requests to server every 1 second.
  18. #include <gflags/gflags.h>
  19. #include "gen-cpp/echo_types.h"
  20. #include <butil/logging.h>
  21. #include <butil/time.h>
  22. #include <brpc/channel.h>
  23. #include <brpc/thrift_message.h>
  24. #include <bvar/bvar.h>
  25. bvar::LatencyRecorder g_latency_recorder("client");
  26. DEFINE_string(server, "0.0.0.0:8019", "IP Address of server");
  27. DEFINE_string(load_balancer, "", "The algorithm for load balancing");
  28. DEFINE_int32(timeout_ms, 100, "RPC timeout in milliseconds");
  29. DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)");
  30. int main(int argc, char* argv[]) {
  31. // Parse gflags. We recommend you to use gflags as well.
  32. google::ParseCommandLineFlags(&argc, &argv, true);
  33. // A Channel represents a communication line to a Server. Notice that
  34. // Channel is thread-safe and can be shared by all threads in your program.
  35. brpc::Channel channel;
  36. // Initialize the channel, NULL means using default options.
  37. brpc::ChannelOptions options;
  38. options.protocol = brpc::PROTOCOL_THRIFT;
  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. brpc::ThriftStub stub(&channel);
  46. // Send a request and wait for the response every 1 second.
  47. while (!brpc::IsAskedToQuit()) {
  48. brpc::Controller cntl;
  49. example::EchoRequest req;
  50. example::EchoResponse res;
  51. req.__set_data("hello");
  52. req.__set_need_by_proxy(10);
  53. stub.CallMethod("Echo", &cntl, &req, &res, NULL);
  54. if (cntl.Failed()) {
  55. LOG(ERROR) << "Fail to send thrift request, " << cntl.ErrorText();
  56. sleep(1); // Remove this sleep in production code.
  57. } else {
  58. g_latency_recorder << cntl.latency_us();
  59. LOG(INFO) << "Thrift Response: " << res;
  60. }
  61. LOG_EVERY_SECOND(INFO)
  62. << "Sending thrift requests at qps=" << g_latency_recorder.qps(1)
  63. << " latency=" << g_latency_recorder.latency(1);
  64. sleep(1);
  65. }
  66. LOG(INFO) << "EchoClient is going to quit";
  67. return 0;
  68. }