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/

相关推荐
老虎海子4 天前
Dockerfile 零基础:指令详解 + Compose 对比 + 前端镜像实战
开发语言·前端·docker·rpc
snow@li4 天前
SpringBoot:依赖注入(DI)全景深度梳理与实战分析/@Autowired、@Resource、@Qualifier
java·spring boot·rpc
snow@li4 天前
SpringBoot:RPC 接口与 Web 接口全景深度分析
spring boot·rpc·架构
IDIOT___IDIOT5 天前
JSON-RPC 2.0 与 MCP 协议:从消息格式到进程通信 大模型MCP
rpc·大模型·json·agent·mcp
阿昌喜欢吃黄桃6 天前
Dubbo服务重启时注册中心下线不及时问题排查与修复记录
java·rpc·dubbo·问题排查
Sam_Deep_Thinking6 天前
用Java 17从0到1写一个mini版RPC,理解远程调用的本质
java·面试·rpc·程序员·系统架构
逸Y 仙X8 天前
跨语言RPC框架Thrift实战
网络·网络协议·rpc·thrift
寒水馨9 天前
macOS下载、安装protobuf-v35.1(附安装包protoc-35.1-osx-universal_binary.zip)
macos·google·grpc·序列化·protobuf·protoc·数据交换
wWYy.10 天前
基于Raft分布式Kv存储:RPC
分布式·rpc
wWYy.10 天前
基于Raft分布式Kv存储:RPC调用
分布式·rpc