From ca8ba90292355ccadc549f7a3f9b93517ed5c377 Mon Sep 17 00:00:00 2001 From: Donna Dionne Date: Thu, 5 Mar 2020 12:47:14 -0800 Subject: [PATCH] Adding Proto Reflection and Health Check to basic C++ example server And ensuring the example can be built and ran via bazel, make, and cmake. --- examples/BUILD | 1 + examples/cpp/helloworld/CMakeLists.txt | 4 ++++ examples/cpp/helloworld/greeter_server.cc | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/examples/BUILD b/examples/BUILD index b6458b74b83..5dbde0febe4 100644 --- a/examples/BUILD +++ b/examples/BUILD @@ -114,6 +114,7 @@ cc_binary( deps = [ ":helloworld_cc_grpc", "//:grpc++", + "//:grpc++_reflection", ], ) diff --git a/examples/cpp/helloworld/CMakeLists.txt b/examples/cpp/helloworld/CMakeLists.txt index 384e88a5aac..f158a75bd78 100644 --- a/examples/cpp/helloworld/CMakeLists.txt +++ b/examples/cpp/helloworld/CMakeLists.txt @@ -54,6 +54,7 @@ if(GRPC_AS_SUBMODULE) # After using add_subdirectory, we can now use the grpc targets directly from # this build. set(_PROTOBUF_LIBPROTOBUF libprotobuf) + set(_REFLECTION grpc++_reflection) if(CMAKE_CROSSCOMPILING) find_program(_PROTOBUF_PROTOC protoc) else() @@ -84,6 +85,7 @@ elseif(GRPC_FETCHCONTENT) # Since FetchContent uses add_subdirectory under the hood, we can use # the grpc targets directly from this build. set(_PROTOBUF_LIBPROTOBUF libprotobuf) + set(_REFLECTION grpc++_reflection) set(_PROTOBUF_PROTOC $) set(_GRPC_GRPCPP_UNSECURE grpc++_unsecure) if(CMAKE_CROSSCOMPILING) @@ -102,6 +104,7 @@ else() message(STATUS "Using protobuf ${protobuf_VERSION}") set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf) + set(_REFLECTION gRPC::grpc++_reflection) if(CMAKE_CROSSCOMPILING) find_program(_PROTOBUF_PROTOC protoc) else() @@ -151,6 +154,7 @@ foreach(_target ${hw_proto_srcs} ${hw_grpc_srcs}) target_link_libraries(${_target} + ${_REFLECTION} ${_GRPC_GRPCPP_UNSECURE} ${_PROTOBUF_LIBPROTOBUF}) endforeach() diff --git a/examples/cpp/helloworld/greeter_server.cc b/examples/cpp/helloworld/greeter_server.cc index f36ad906a29..cb8f92cf20b 100644 --- a/examples/cpp/helloworld/greeter_server.cc +++ b/examples/cpp/helloworld/greeter_server.cc @@ -21,6 +21,8 @@ #include #include +#include +#include #ifdef BAZEL_BUILD #include "examples/protos/helloworld.grpc.pb.h" @@ -50,6 +52,8 @@ void RunServer() { std::string server_address("0.0.0.0:50051"); GreeterServiceImpl service; + grpc::EnableDefaultHealthCheckService(true); + grpc::reflection::InitProtoReflectionServerBuilderPlugin(); ServerBuilder builder; // Listen on the given address without any authentication mechanism. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());