gRPC学习

gRPC基本概念

  • gRPC是什么?

gRPC是一种高性能、开源的远程过程调用(RPC)框架,由Google开发并开源。gRPC使用Google开发的Protocol Buffers(ProtoBuf)作为其默认的数据序列化和接口定义语言。

  • 那RPC又是什么?

RPC是一种编程模型,它使得像本地方法调用一样调用远程计算机上的方法。通过RPC可以将不同的服务或不同的应用程序连接起来,使它们能够相互通信和交换数据,就像它们在同一台机器上运行一样。

安装gRPC

见https://blog.csdn.net/you_fathe/article/details/128192504

其中包含CMake、gcc/g++、gRPC、protobuf。

定义和实现 gRPC 服务

首先,定义一个.proto文件(例如,greeter.proto),用于定义服务和消息格式

cpp 复制代码
syntax = "proto3";

package example;

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

使用protoc编译器,根据.proto文件生成C++代码

cpp 复制代码
protoc -I ./ --cpp_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` greeter.proto

服务端

cpp 复制代码
#include <iostream>
#include <memory>
#include <grpcpp/grpcpp.h>
#include "greeter.grpc.pb.h"

using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using example::Greeter;
using example::HelloRequest;
using example::HelloReply;

class GreeterServiceImpl final : public Greeter::Service {
  Status SayHello(ServerContext* context, const HelloRequest* request,
                  HelloReply* reply) override {
    std::string name = request->name();
    std::string message = "Hello, " + name + "!";
    reply->set_message(message);
    return Status::OK;
  }
};

void RunServer() {
  std::string server_address("0.0.0.0:50051");
  GreeterServiceImpl service;

  ServerBuilder builder;
  builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  builder.RegisterService(&service);

  std::unique_ptr<Server> server(builder.BuildAndStart());
  std::cout << "Server listening on " << server_address << std::endl;
  server->Wait();
}

int main() {
  RunServer();
  return 0;
}

客户端

cpp 复制代码
#include <iostream>
#include <memory>
#include <grpcpp/grpcpp.h>
#include "greeter.grpc.pb.h"

using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using example::Greeter;
using example::HelloRequest;
using example::HelloReply;

class GreeterClient {
public:
  GreeterClient(std::shared_ptr<Channel> channel)
    : stub_(Greeter::NewStub(channel)) {}

  std::string SayHello(const std::string& name) {
    HelloRequest request;
    request.set_name(name);

    HelloReply reply;
    ClientContext context;

    Status status = stub_->SayHello(&context, request, &reply);

    if (status.ok()) {
      return reply.message();
    } else {
      return "RPC failed.";
    }
  }

private:
  std::unique_ptr<Greeter::Stub> stub_;
};

int main() {
  std::string server_address("localhost:50051");
  GreeterClient greeter(grpc::CreateChannel(server_address, grpc::InsecureChannelCredentials()));
  
  std::string user("Alice");
  std::string reply = greeter.SayHello(user);
  std::cout << "Server replied: " << reply << std::endl;

  return 0;
}

分别编译

cpp 复制代码
g++ -std=c++11 -o server greeter.pb.cc greeter.grpc.pb.cc server.cpp `pkg-config --cflags --libs protobuf grpc++ grpc`
g++ -std=c++11 -o client greeter.pb.cc greeter.grpc.pb.cc client.cpp `pkg-config --cflags --libs protobuf grpc++ grpc`

报错

cpp 复制代码
Perhaps you should add the directory containing `openssl.pc'
to the PKG_CONFIG_PATH environment variable
Package 'openssl', required by 'grpc', not found

解决

cpp 复制代码
sudo apt install libssl-dev
export PKG_CONFIG_PATH=/usr/lib/pkgconfig:$PKG_CONFIG_PATH

再进行编译,并分别运行服务端和客户端

cpp 复制代码
./server
./client

成功啦

cpp 复制代码
root@sjn:/home# ./server
Server listening on 0.0.0.0:50051

root@sjn:/home# ./client
Server replied: Hello, Alice!

https://grpc.io/docs/languages/cpp/basics/

相关推荐
阿正的梦工坊17 小时前
深入理解 JSON-RPC 2.0:轻量级远程调用协议详解
rpc·json
进化中的刀盾兽3 天前
从零实现 C++ Json-Rpc(十):把消息交给真实连接——MuduoConnection 与 ConnectionFactory
c++·rpc
骑着蜗牛撵大象3276 天前
飞牛fnOS部署Damselfly:Compose编排、照片目录与7100固定访问
java·docker·rpc·dubbo·fnos·照片管理·damselfly
我不会起名字3227 天前
gRPC 拦截器写错:metadata 透传与超时注入的 3 个坑
微服务·go·grpc·拦截器
syagain_zsx7 天前
BRpc 二次封装:Channel 管理、服务管理与回调工厂
c++·学习·程序人生·rpc·谷歌
mister_guo7 天前
HTTP 和 RPC:从一次干饭说清它们的区别
网络协议·http·rpc
zzu123zsw8 天前
面对多 RPC 扇入超时崩溃:深入 asyncTool 核心 DAG 编排与线程池隔离防护
java·网络·spring boot·网络协议·rpc·并发编程
syagain_zsx8 天前
BRpc 搭建简单 rpc 服务器
服务器·c++·网络协议·rpc·protobuf·库
换元不配限9 天前
Android 接入 protobuf 实战:从 Gradle 配置到避开 R8 混淆坑
android·grpc·序列化·protobuf·protocolbuffers·二进制序列化·.proto
SatanII9 天前
K8s 存储与配置管理|Volume、ConfigMap、Secret 核心笔记
笔记·rpc·kubernetes