xds_greeter_server.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. *
  3. * Copyright 2021 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * 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, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <iostream>
  19. #include <memory>
  20. #include <string>
  21. #include "absl/flags/flag.h"
  22. #include "absl/flags/parse.h"
  23. #include "absl/strings/str_cat.h"
  24. #include <grpcpp/ext/admin_services.h>
  25. #include <grpcpp/ext/proto_server_reflection_plugin.h>
  26. #include <grpcpp/grpcpp.h>
  27. #include <grpcpp/health_check_service_interface.h>
  28. #include <grpcpp/xds_server_builder.h>
  29. #ifdef BAZEL_BUILD
  30. #include "examples/protos/helloworld.grpc.pb.h"
  31. #else
  32. #include "helloworld.grpc.pb.h"
  33. #endif
  34. ABSL_FLAG(int32_t, port, 50051, "Server port for service.");
  35. ABSL_FLAG(int32_t, maintenance_port, 50052,
  36. "Server port for maintenance if --secure is used.");
  37. ABSL_FLAG(bool, secure, true, "Secure mode");
  38. using grpc::Server;
  39. using grpc::ServerBuilder;
  40. using grpc::ServerContext;
  41. using grpc::Status;
  42. using helloworld::Greeter;
  43. using helloworld::HelloReply;
  44. using helloworld::HelloRequest;
  45. // Logic and data behind the server's behavior.
  46. class GreeterServiceImpl final : public Greeter::Service {
  47. Status SayHello(ServerContext* context, const HelloRequest* request,
  48. HelloReply* reply) override {
  49. std::string prefix("Hello ");
  50. reply->set_message(prefix + request->name());
  51. return Status::OK;
  52. }
  53. };
  54. void RunServer() {
  55. grpc::EnableDefaultHealthCheckService(true);
  56. grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  57. int port = absl::GetFlag(FLAGS_port);
  58. int maintenance_port = absl::GetFlag(FLAGS_maintenance_port);
  59. grpc::experimental::XdsServerBuilder xds_builder;
  60. ServerBuilder builder;
  61. std::unique_ptr<Server> xds_enabled_server;
  62. std::unique_ptr<Server> server;
  63. GreeterServiceImpl service;
  64. // Register "service" as the instance through which we'll communicate with
  65. // clients. In this case it corresponds to an *synchronous* service.
  66. xds_builder.RegisterService(&service);
  67. if (absl::GetFlag(FLAGS_secure)) {
  68. // Listen on the given address with XdsServerCredentials and a fallback of
  69. // InsecureServerCredentials
  70. xds_builder.AddListeningPort(absl::StrCat("0.0.0.0:", port),
  71. grpc::experimental::XdsServerCredentials(
  72. grpc::InsecureServerCredentials()));
  73. xds_enabled_server = xds_builder.BuildAndStart();
  74. gpr_log(GPR_INFO, "Server starting on 0.0.0.0:%d", port);
  75. grpc::AddAdminServices(&builder);
  76. // For the maintenance server, do not use any authentication mechanism.
  77. builder.AddListeningPort(absl::StrCat("0.0.0.0:", maintenance_port),
  78. grpc::InsecureServerCredentials());
  79. server = builder.BuildAndStart();
  80. gpr_log(GPR_INFO, "Maintenance server listening on 0.0.0.0:%d",
  81. maintenance_port);
  82. } else {
  83. grpc::AddAdminServices(&xds_builder);
  84. // Listen on the given address without any authentication mechanism.
  85. builder.AddListeningPort(absl::StrCat("0.0.0.0:", port),
  86. grpc::InsecureServerCredentials());
  87. server = xds_builder.BuildAndStart();
  88. gpr_log(GPR_INFO, "Server listening on 0.0.0.0:%d", port);
  89. }
  90. // Wait for the server to shutdown. Note that some other thread must be
  91. // responsible for shutting down the server for this call to ever return.
  92. server->Wait();
  93. }
  94. int main(int argc, char** argv) {
  95. absl::ParseCommandLine(argc, argv);
  96. RunServer();
  97. return 0;
  98. }