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/

相关推荐
gobeyye1 天前
spring loC&DI 详解
java·spring·rpc
陈亦康1 天前
Armeria gPRC 高级特性 - 装饰器、无框架请求、阻塞处理器、Nacos集成、负载均衡、rpc异常处理、文档服务......
kotlin·grpc·armeria
余生H2 天前
前端的全栈混合之路Meteor篇:RPC方法注册及调用
前端·rpc·node.js·全栈
TravisBytes4 天前
深入掌握 Protobuf 与 RPC 的高效结合:实现C++工程中的高效通信
c++·分布式·rpc
问道飞鱼4 天前
远程过程调用RPC知识科普
网络·网络协议·rpc
专注代码七年6 天前
Docker 安装 ClickHouse 教程
clickhouse·docker·rpc
九考6 天前
RPC 服务器不可用。 (异常来自 HRESULT0x800706BA)
rpc
I nedd more power6 天前
springcloud为什么采用Http而非RPC
http·spring cloud·rpc
陆沙6 天前
python-rpc-windows服务器C#项目远程调用Linux服务器上的python脚本
服务器·python·rpc
Java程序之猿6 天前
Thingsboard 网关实战 modbus通信 rpc下发控制指令
网络·网络协议·rpc