xds_greeter_server.cc
Go to the documentation of this file.
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 
19 #include <iostream>
20 #include <memory>
21 #include <string>
22 
23 #include "absl/flags/flag.h"
24 #include "absl/flags/parse.h"
25 #include "absl/strings/str_cat.h"
26 
29 #include <grpcpp/grpcpp.h>
32 
33 #ifdef BAZEL_BUILD
34 #include "examples/protos/helloworld.grpc.pb.h"
35 #else
36 #include "helloworld.grpc.pb.h"
37 #endif
38 
39 ABSL_FLAG(int32_t, port, 50051, "Server port for service.");
40 ABSL_FLAG(int32_t, maintenance_port, 50052,
41  "Server port for maintenance if --secure is used.");
42 ABSL_FLAG(bool, secure, true, "Secure mode");
43 
44 using grpc::Server;
47 using grpc::Status;
51 
52 // Logic and data behind the server's behavior.
53 class GreeterServiceImpl final : public Greeter::Service {
55  HelloReply* reply) override {
56  std::string prefix("Hello ");
57  reply->set_message(prefix + request->name());
58  return Status::OK;
59  }
60 };
61 
62 void RunServer() {
65  int port = absl::GetFlag(FLAGS_port);
66  int maintenance_port = absl::GetFlag(FLAGS_maintenance_port);
67  grpc::experimental::XdsServerBuilder xds_builder;
69  std::unique_ptr<Server> xds_enabled_server;
70  std::unique_ptr<Server> server;
72  // Register "service" as the instance through which we'll communicate with
73  // clients. In this case it corresponds to an *synchronous* service.
74  xds_builder.RegisterService(&service);
75  if (absl::GetFlag(FLAGS_secure)) {
76  // Listen on the given address with XdsServerCredentials and a fallback of
77  // InsecureServerCredentials
78  xds_builder.AddListeningPort(absl::StrCat("0.0.0.0:", port),
81  xds_enabled_server = xds_builder.BuildAndStart();
82  gpr_log(GPR_INFO, "Server starting on 0.0.0.0:%d", port);
84  // For the maintenance server, do not use any authentication mechanism.
85  builder.AddListeningPort(absl::StrCat("0.0.0.0:", maintenance_port),
87  server = builder.BuildAndStart();
88  gpr_log(GPR_INFO, "Maintenance server listening on 0.0.0.0:%d",
89  maintenance_port);
90  } else {
91  grpc::AddAdminServices(&xds_builder);
92  // Listen on the given address without any authentication mechanism.
93  builder.AddListeningPort(absl::StrCat("0.0.0.0:", port),
95  server = xds_builder.BuildAndStart();
96  gpr_log(GPR_INFO, "Server listening on 0.0.0.0:%d", port);
97  }
98 
99  // Wait for the server to shutdown. Note that some other thread must be
100  // responsible for shutting down the server for this call to ever return.
101  server->Wait();
102 }
103 
104 int main(int argc, char** argv) {
105  absl::ParseCommandLine(argc, argv);
106  RunServer();
107  return 0;
108 }
xds_server_builder.h
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
grpc::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
grpc::EnableDefaultHealthCheckService
void EnableDefaultHealthCheckService(bool enable)
Definition: health_check_service.cc:30
proto_server_reflection_plugin.h
benchmark.request
request
Definition: benchmark.py:77
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
framework.rpc.grpc_channelz.Server
Server
Definition: grpc_channelz.py:42
health_check_service_interface.h
hellostreamingworld_pb2.HelloRequest
HelloRequest
Definition: hellostreamingworld_pb2.py:102
absl::ParseCommandLine
std::vector< char * > ParseCommandLine(int argc, char *argv[])
Definition: abseil-cpp/absl/flags/parse.cc:815
OK
@ OK
Definition: cronet_status.h:43
ABSL_FLAG
ABSL_FLAG(int32_t, port, 50051, "Server port for service.")
server
std::unique_ptr< Server > server
Definition: channelz_service_test.cc:330
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
grpc::experimental::XdsServerCredentials
std::shared_ptr< ServerCredentials > XdsServerCredentials(const std::shared_ptr< ServerCredentials > &fallback_credentials)
Definition: xds_server_credentials.cc:48
GreeterServiceImpl::SayHello
Status SayHello(ServerContext *context, const HelloRequest *request, HelloReply *reply) override
Definition: xds_greeter_server.cc:54
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: grpcpp/server_builder.h:86
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpcpp.h
main
int main(int argc, char **argv)
Definition: xds_greeter_server.cc:104
absl::GetFlag
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag< T > &flag)
Definition: abseil-cpp/absl/flags/flag.h:98
helloworld.Greeter
Definition: helloworld.py:32
RunServer
void RunServer()
Definition: xds_greeter_server.cc:62
admin_services.h
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
GreeterServiceImpl
Definition: grpc-helloworld.cc:39
hellostreamingworld_pb2.HelloReply
HelloReply
Definition: hellostreamingworld_pb2.py:109
server
Definition: examples/python/async_streaming/server.py:1
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
grpc::AddAdminServices
void AddAdminServices(grpc::ServerBuilder *builder)
Definition: admin_services.cc:41
grpc::InsecureServerCredentials
std::shared_ptr< ServerCredentials > InsecureServerCredentials()
Definition: insecure_server_credentials.cc:52
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
grpc::reflection::InitProtoReflectionServerBuilderPlugin
void InitProtoReflectionServerBuilderPlugin()
Definition: proto_server_reflection_plugin.cc:66


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:58