server.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include <gflags/gflags.h>
  18. #include <butil/logging.h>
  19. #include <brpc/channel.h>
  20. #include <brpc/server.h>
  21. #include "echo.pb.h"
  22. DEFINE_int32(timeout_ms, 100, "RPC timeout in milliseconds");
  23. DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)");
  24. DEFINE_bool(echo_attachment, true, "Echo attachment as well");
  25. DEFINE_int32(port, 8000, "TCP Port of this server");
  26. DEFINE_string(server, "", "The server to connect, localhost:FLAGS_port as default");
  27. DEFINE_string(load_balancer, "", "The algorithm for load balancing");
  28. DEFINE_bool(use_http, false, "Use http protocol to transfer messages");
  29. DEFINE_int32(idle_timeout_s, -1, "Connection will be closed if there is no "
  30. "read/write operations during the last `idle_timeout_s'");
  31. brpc::Channel channel;
  32. // Your implementation of example::EchoService
  33. namespace example {
  34. class CascadeEchoService : public EchoService {
  35. public:
  36. CascadeEchoService() {};
  37. virtual ~CascadeEchoService() {};
  38. virtual void Echo(google::protobuf::RpcController* cntl_base,
  39. const EchoRequest* request,
  40. EchoResponse* response,
  41. google::protobuf::Closure* done) {
  42. // This object helps you to call done->Run() in RAII style. If you need
  43. // to process the request asynchronously, pass done_guard.release().
  44. brpc::ClosureGuard done_guard(done);
  45. brpc::Controller* cntl =
  46. static_cast<brpc::Controller*>(cntl_base);
  47. if (request->depth() > 0) {
  48. CLOGI(cntl) << "I'm about to call myself for another time, depth=" << request->depth();
  49. example::EchoService_Stub stub(&channel);
  50. example::EchoRequest request2;
  51. example::EchoResponse response2;
  52. brpc::Controller cntl2(cntl->inheritable());
  53. request2.set_message(request->message());
  54. request2.set_depth(request->depth() - 1);
  55. cntl2.set_timeout_ms(FLAGS_timeout_ms);
  56. cntl2.set_max_retry(FLAGS_max_retry);
  57. stub.Echo(&cntl2, &request2, &response2, NULL);
  58. if (cntl2.Failed()) {
  59. CLOGE(&cntl2) << "Fail to send EchoRequest, " << cntl2.ErrorText();
  60. cntl->SetFailed(cntl2.ErrorCode(), "%s", cntl2.ErrorText().c_str());
  61. return;
  62. }
  63. response->set_message(response2.message());
  64. } else {
  65. CLOGI(cntl) << "I'm the last call";
  66. response->set_message(request->message());
  67. }
  68. if (FLAGS_echo_attachment && !FLAGS_use_http) {
  69. // Set attachment which is wired to network directly instead of
  70. // being serialized into protobuf messages.
  71. cntl->response_attachment().append(cntl->request_attachment());
  72. }
  73. }
  74. };
  75. } // namespace example
  76. int main(int argc, char* argv[]) {
  77. // Parse gflags. We recommend you to use gflags as well.
  78. GFLAGS_NS::SetUsageMessage("A server that may call itself");
  79. GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
  80. // A Channel represents a communication line to a Server. Notice that
  81. // Channel is thread-safe and can be shared by all threads in your program.
  82. brpc::ChannelOptions coption;
  83. if (FLAGS_use_http) {
  84. coption.protocol = brpc::PROTOCOL_HTTP;
  85. }
  86. // Initialize the channel, NULL means using default options.
  87. // options, see `brpc/channel.h'.
  88. if (FLAGS_server.empty()) {
  89. if (channel.Init("localhost", FLAGS_port, &coption) != 0) {
  90. LOG(ERROR) << "Fail to initialize channel";
  91. return -1;
  92. }
  93. } else {
  94. if (channel.Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &coption) != 0) {
  95. LOG(ERROR) << "Fail to initialize channel";
  96. return -1;
  97. }
  98. }
  99. // Generally you only need one Server.
  100. brpc::Server server;
  101. // For more options see `brpc/server.h'.
  102. brpc::ServerOptions options;
  103. options.idle_timeout_sec = FLAGS_idle_timeout_s;
  104. // Instance of your service.
  105. example::CascadeEchoService echo_service_impl;
  106. // Add the service into server. Notice the second parameter, because the
  107. // service is put on stack, we don't want server to delete it, otherwise
  108. // use brpc::SERVER_OWNS_SERVICE.
  109. if (server.AddService(&echo_service_impl,
  110. brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
  111. LOG(ERROR) << "Fail to add service";
  112. return -1;
  113. }
  114. // Start the server.
  115. if (server.Start(FLAGS_port, &options) != 0) {
  116. LOG(ERROR) << "Fail to start EchoServer";
  117. return -1;
  118. }
  119. // Wait until Ctrl-C is pressed, then Stop() and Join() the server.
  120. server.RunUntilAskedToQuit();
  121. return 0;
  122. }