C++ 使用 gRPC
安装 gRPC
git clone --recurse-submodules -b v1.34.0 https://github.com/grpc/grpc
cd grpc
mkdir -p cmake/build
pushd cmake/build
cmake -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=$(pwd)/install ../..
make -j
make install
popd
生成 grpc 代码
export PATH=<安装grpc的路径>/bin:$PATH
PROTO_PATH=<你的proto文件所在的路径>
protoc -I ./src --grpc_out=./src/ --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` $PROTO_PATH
protoc -I ./src --cpp_out=./src// $PROTO_PATH
服务端程序
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "helloworld.grpc.pb.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using HelloWorld::Greeter;
using HelloWorld::HelloReply;
using HelloWorld::HelloRequest;
class GreeterServiceImpl final : public Greeter::Service
{
Status SayHello(ServerContext *context,
const HelloRequest *request,
HelloReply *reply) override
{
std::string prefix("Hi!");
reply->set_message(prefix + request->text());
return Status::OK;
}
};
void Serve()
{
std::string address("0.0.0.0:50051");
GreeterServiceImpl serve;
ServerBuilder builder;
builder.AddListeningPort(address, grpc::InsecureServerCredentials());
builder.RegisterService(&serve);
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << address << std::endl;
server->Wait();
}
int main(int argc, char** argv)
{
Serve();
return 0;
}
客户端
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#ifdef BAZEL_BUILD
#include "helloworld.grpc.pb.h"
#else
#include "helloworld.grpc.pb.h"
#endif
using grpc::Channel;
using grpc::ChannelInterface;
using grpc::ClientContext;
using grpc::Status;
using HelloWorld::Greeter;
using HelloWorld::HelloReply;
using HelloWorld::HelloRequest;
class HelloWorldClient
{
public:
HelloWorldClient(std::shared_ptr<ChannelInterface> channel)
: stub_(Greeter::NewStub(channel)) {}
std::string SayHello(const std::string &text)
{
HelloRequest request;
request.set_text(text);
HelloReply reply;
ClientContext context;
Status status = stub_->SayHello(&context, request, &reply);
if (status.ok())
{
return reply.message();
}
std::cerr << status.error_code() << ": " << status.error_message() << std::endl;
return "RPC failed";
}
private:
std::unique_ptr<Greeter::Stub> stub_;
};
int main(int argc, char** argv)
{
std::string target_str = "localhost:50051";
HelloWorldClient helloworld_client(grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
std::string text("Hello World");
std::string reply = helloworld_client.SayHello(text);
std::cout << "Received: " << reply << std::endl;
return 0;
}
Makefile 的编写
grpc 可以使用 pkg-config 添加依赖,因此我们可以把安装后 .pc 文件所在路径添加到 pkg-config 的环境变量中。使用 export PKG_CONFIG_PATH=<你安装grpc后pkgconfig文件夹所在的路径>
。
GRPC_INSTALL_PATH = $(dirname $(pwd))
export PKG_CONFIG_PATH=$(GRPC_INSTALL_PATH)/grpc/cmake/build/install/lib/pkgconfig
HOST_SYSTEM = $(shell uname | cut -f 1 -d_)
SYSTEM ?= $(HOST_SYSTEM)
CXX = g++
CPPFLAGS += `pkg-config --cflags protobuf grpc`
CXXFLAGS += -std=c++11
ifeq ($(SYSTEM),Darwin)
LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++`\
-pthread\
-lgrpc++_reflection\
-ldl
else
LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++`\
-pthread\
-Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
-ldl
endif
all: helloworld.server helloworld.client
helloworld.server: helloworld.pb.o helloworld.grpc.pb.o helloworld.server.o
$(CXX) $^ $(LDFLAGS) -o $@
helloworld.client: helloworld.pb.o helloworld.grpc.pb.o helloworld.client.o
$(CXX) $^ $(LDFLAGS) -o $@
clean:
rm -f *.o helloworld.server helloworld.client
如何使用
在 Makefile
所在的文件夹执行如下命令。
make -j
./helloworld.server
# 新建一个终端执行下面的命令
./helloworld.client