c++ 使用 gRPC

本文详细介绍了如何在C++中使用gRPC,包括安装gRPC、生成服务端和客户端代码,以及编写Makefile进行编译。通过示例展示了gRPC在实际项目中的应用。
摘要由CSDN通过智能技术生成

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

全部代码

cpp-grpc-demo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值