【C++版本】protobuf与gRPC

server/2024/10/21 15:33:40/

文章目录

  • 一、Protobuf
  • 二、安装以及使用protoc
  • 三、gRPC
    • 1.Q&A
    • 2.学习版rpc
    • 3.gRPC压缩算法
  • 参考

一、Protobuf

在这里插入图片描述

Google Protocol Buffers(protobuf)是一种语言中立、平台中立的序列化协议,旨在高效地将结构化数据进行序列化和反序列化。它主要用于通信协议、数据存储和其他需要高效编码和解码结构化数据的场景。protobuf 由 Google 开发和开源,广泛用于 Google 的内部系统以及众多开源项目和商业应用中。

Protobuf 的用途
(1)数据序列化:

  • Protobuf 将数据结构化为紧凑的二进制格式,适用于网络传输、持久化存储等需要高效数据编码的场景。
  • 相较于 XML 和 JSON,protobuf 编码后的数据占用更少的空间,解析速度更快。
  • 跨语言和跨平台通信:

(2)Protobuf 支持多种编程语言,如 C++, Java, Python, Go, Ruby 等,适用于异构系统间的通信。

  • 数据结构定义在 .proto 文件中,不同语言的代码生成器可以从 .proto 文件生成相应语言的类,实现数据的编解码。
  • 远程过程调用(RPC):

(3)Protobuf 可以与 gRPC 结合使用,定义和实现高效的 RPC 协议,支持流式传输和双向通信。

  • gRPC 通过 protobuf 定义服务接口和消息格式,自动生成客户端和服务端代码,简化了分布式系统的开发。

(4)数据存储:

  • Protobuf 可以用于将数据序列化后存储到文件或数据库中,确保数据存储和传输的高效性。
  • 由于 protobuf 的二进制格式紧凑,特别适合在存储空间有限或网络带宽受限的环境中使用。

(5)Protobuf 的优点

  • 高效性:序列化后的数据格式紧凑,占用更少的存储空间和带宽,解析速度快。
  • 可扩展性:支持向后兼容和向前兼容,允许在不破坏现有数据格式的情况下添加新字段。
  • 多语言支持:生成的代码可在多种编程语言中使用,便于不同语言系统之间的数据交换。
  • 简洁性:定义数据结构的 .proto 文件简单直观,便于维护和管理。

protobuffer C++基础见

二、安装以及使用protoc

$ apt install -y protobuf-compiler
$ protoc --version  # Ensure compiler version is 3+

定义person.proto文件

//person.proto
package yaojun;message Person {required string name = 1;required int32 id = 2;optional string email = 3;
}

语法规则,字段定义:

每个字段有三部分:修饰符、类型和字段名,以及一个唯一的编号。
修饰符:
required:表示字段是必需的,消息必须包含该字段,否则解析消息时会报错。
optional:表示字段是可选的,消息中可以包含也可以不包含该字段。
repeated(示例中未使用):表示字段可以重复零次或多次,通常用于列表或数组。
类型:
string:表示字符串类型。
int32:表示32位整数类型。
字段名和编号:
每个字段有一个唯一的编号,用于标识字段。这些编号在消息的二进制表示中非常重要,用于解码数据。
编号必须是正整数,且在同一消息类型中必须唯一。

在这个例子中:

  • required string name = 1;:定义了一个必需的字符串字段 name,编号为 1。
  • required int32 id = 2;:定义了一个必需的 32 位整数字段 id,编号为 2。
  • optional string email = 3;:定义了一个可选的字符串字段 email,编号为 3。
~/code/PremiumProject/protobuf main 
 protoc --proto_path=.  --cpp_out=. person.proto

代码:

// yaojun_person.cpp
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "google/protobuf/text_format.h"
#include "person.pb.h"
#include <fstream>
#include <iostream>using namespace yaojun;
int main() {Person p;p.set_name("test");p.set_id(100);p.set_email("940334249@qq.com");// 将pb二进制信息保存到字符串, 序列化std::string str;p.SerializeToString(&str);std::cout << "str: [" << str << "]" << std::endl;// 将pb文本信息写入文件std::ofstream fw;fw.open("./Person.txt", std::ios::out | std::ios::binary);google::protobuf::io::OstreamOutputStream *output =new google::protobuf::io::OstreamOutputStream(&fw);google::protobuf::TextFormat::Print(p, output);delete output;fw.close();// 将pb文本信息保存到字符串std::string str1;google::protobuf::TextFormat::PrintToString(p, &str1);std::cout << "str1: [" << str1 << "]" << std::endl;// 反序列化Person p1;p1.ParseFromString(str);std::cout << "name:" << p1.name() << ",email:" << p1.email()<< ",id:" << p1.id() << std::endl;return 0;
}

更好的例子见

三、gRPC

安装

~/code/PremiumProject/protobuf main 
 sudo apt-get install -y protobuf-compiler-grpc查看版本
 grpc_cpp_plugin --version

grpc以及grpc++开发库

~/code/PremiumProject/grpc main 
 apt-get install -y libgrpc++-dev

定义一个rpc method:

syntax = "proto3";package calculator;service Calculator {rpc Add (AddRequest) returns (AddResponse);
}message AddRequest {int32 operand1 = 1;int32 operand2 = 2;
}message AddResponse {int32 result = 1;
}

解释:

syntax = “proto3”;
这行代码指定了使用 Protocol Buffers 的第 3 版语法(proto3)。proto3 是 Protocol Buffers 的一种更简洁和现代化的语法,与 proto2 相比,简化了许多功能和语法。

package calculator;
这行代码定义了包名 calculator。包名用于在生成代码时为不同的消息和服务提供命名空间,避免命名冲突。

service Calculator {
rpc Add (AddRequest) returns (AddResponse);
}
这段代码定义了一个名为 Calculator 的 gRPC 服务。
service 关键字用于定义一个服务,服务包含一个或多个远程过程调用(RPC)方法。
在这个例子中,Calculator 服务包含一个名为 Add 的 RPC 方法。
Add 方法接受一个 AddRequest 消息作为输入参数,并返回一个 AddResponse 消息作为结果。
rpc 关键字用于定义一个远程过程调用。

message AddRequest {
int32 operand1 = 1;
int32 operand2 = 2;
}
这段代码定义了一个名为 AddRequest 的消息类型。
message 关键字用于定义消息类型。
AddRequest 消息包含两个字段:operand1 和 operand2,都是 int32 类型。
每个字段有一个唯一的编号,用于在消息的二进制表示中标识字段。

message AddResponse {
int32 result = 1;
}
这段代码定义了一个名为 AddResponse 的消息类型。
AddResponse 消息包含一个字段:result,类型为 int32。
字段 result 的编号是 1。

构建:

#-I=.:指定 .proto 文件的包含路径。这意味着 protoc 在当前目录下查找 add.proto 文件。
#生成add.grpc.pb.h和add.grpc.pb.cc的gRPC代码
#--plugin=protoc-gen-grpc=which grpc_cpp_plugin``:指定使用 gRPC 插件 grpc_cpp_plugin 来生成 gRPC 相关代码
$ protoc -I=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` add.proto#-I=.:指定 .proto 文件的包含路径。这意味着 protoc 在当前目录下查找 add.proto 文件。
#--cpp_out=.:指定生成的 C++ 代码的输出目录为当前目录。生成的 Protocol Buffers 数据结构代码将放在当前目录下。
# 生成add.pb.h,add.pb.cc的protobuffer 代码
protoc -I=. --cpp_out=. add.proto

计算器服务端代码:

#include <iostream>
#include <grpcpp/grpcpp.h>
#include "add.grpc.pb.h"using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using calculator::Calculator;
using calculator::AddRequest;
using calculator::AddResponse;class CalculatorServiceImpl final : public Calculator::Service {
public:Status Add(ServerContext* context, const AddRequest* request, AddResponse* response) override {int result = request->operand1() + request->operand2();response->set_result(result);return Status::OK;}
};void RunServer() {std::string server_address("0.0.0.0:50052");CalculatorServiceImpl 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;
}

计算器客户端代码:

#include "add.grpc.pb.h"
#include <grpcpp/grpcpp.h>
#include <iostream>
#include <memory>using calculator::AddRequest;
using calculator::AddResponse;
using calculator::Calculator;
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;class CalculatorClient {
public:CalculatorClient(std::shared_ptr<Channel> channel): stub_(Calculator::NewStub(channel)) {}int Add(int operand1, int operand2) {AddRequest request;request.set_operand1(operand1);request.set_operand2(operand2);AddResponse response;ClientContext context;Status status = stub_->Add(&context, request, &response);if (status.ok()) {return response.result();} else {std::cout << "RPC failed: " << status.error_code() << ": "<< status.error_message() << std::endl;return -1;}}private:std::unique_ptr<Calculator::Stub> stub_;
};int main() {CalculatorClient calculator(grpc::CreateChannel("localhost:50052", grpc::InsecureChannelCredentials()));int result = calculator.Add(10, 20);if (result >= 0) {std::cout << "Result: " << result << std::endl;}return 0;
}

编译:
cmake in ubuntu20.04

cmake_minimum_required(VERSION 3.5)project(server)
cmake_minimum_required(VERSION 3.27)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)# find_package(gRPC CONFIG  REQUIRED)
find_package(Protobuf REQUIRED)find_package(PkgConfig REQUIRED)
pkg_search_module(GRPC REQUIRED grpc)
pkg_search_module(GRPCPP REQUIRED grpc++)find_program(_PROTOBUF_PROTOC protoc)
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)add_executable(server calculator_service.cpp add.grpc.pb.cc add.grpc.pb.h add.pb.cc add.pb.h)
add_executable(client calculator_client.cpp add.grpc.pb.cc add.grpc.pb.h add.pb.cc add.pb.h)target_link_libraries(server grpc++ grpc protobuf::libprotobuf)
target_link_libraries(client grpc++ grpc protobuf::libprotobuf)
cmake -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
cmake --build build -j5

Python客户端

生成客户端代码

pip3 install grpcio-tools
python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. add.proto

调用客户端代码

import grpc
import add_pb2
import add_pb2_grpcdef run():channel = grpc.insecure_channel('localhost:50052')  # 假设服务器运行在本地的 50051 端口stub = add_pb2_grpc.CalculatorStub(channel)# 构造请求request = add_pb2.AddRequest(operand1=10, operand2=20)# 调用远程函数response = stub.Add(request)print("Sum:", response.result)if __name__ == '__main__':run()

1.Q&A

  1. 如果我会使用rpc,能给我带来什么好处呢?
    使用rpc可以屏蔽掉底层传输层的协议,只关注我们需要调用的函数接口,而且grpc性能很好。grpc是跨语言的工具,意思是客户端可以是Python实现,而服务端可以是C++实现。
  2. 自己实现一个rpc库需要考虑哪些方面?
    序列化协议选择,服务发现和注册,负载均衡,跨语言,性能。
    序列化协议:例如 Protocol Buffers、MessagePack、JSON 等

2.学习版rpc

  • button_rpc
  • tinyrpc
  • mini-tinyrpc

3.gRPC压缩算法

gRPC 支持多种压缩算法,开发者可以根据应用需求选择适当的算法。以下是 gRPC 支持的主要压缩算法:

  1. gzip: gRPC 默认使用的压缩算法。它是一种通用的压缩算法,具有较高的压缩比和广泛的支持。
  2. identity: 这是一种无压缩算法,即不进行压缩。如果你希望在 gRPC 中禁用压缩,可以选择使用 “identity”。
  3. deflate: gRPC 支持使用 deflate 算法进行压缩。这是一种流行的压缩算法,类似于 gzip,但在某些情况下可能表现不同。

参考

  • 从零开始:protobuf原理与实战代码详解
  • protobuf code
  • Protocol Buffer Compiler Installation
  • 从零开始学习gRPC:实现高性能跨语言微服务【C++和Python】
  • Protobuf和gRpc快速实践

http://www.ppmy.cn/server/93552.html

相关文章

[Git场景]常用工作场景演练

场景1:开发到一半的代码&#xff0c;还没提交&#xff0c;git拉下 对方的代码&#xff0c;但是其中有一个 commit 不想要怎么做 在 Git 中&#xff0c;如果你想拉取远程分支的代码&#xff0c;但不想要某个特定的提交&#xff0c;可以使用以下方法来解决&#xff1a; 方法1&a…

Can we Deploy Web Application in Azure OpenAI of Production Level

题意&#xff1a;我们可以在Azure OpenAI中部署生产级别的Web应用程序吗 问题背景&#xff1a; I have created azure ai search service and used Text split skillset and made index. I also deployed a web Application but have a question that If I want to create to …

docker国内可用镜像源+++

修改配置文件 vim /etc/docker/daemon.json {"registry-mirrors": ["https://registry.dockermirror.com", #特别适合中国宝宝的镜像源"https://tkcnyb3h.mirror.aliyuncs.com", "https://registry.docker-cn.com","https://do…

八股文真的有作用吗?

八股文在实际工作中的作用&#xff1a;助力、阻力还是空谈&#xff1f; 无论如何讨论&#xff0c;八股文都是面试中不可或缺的一环。一般来说&#xff0c;准入门槛是由“招聘”这个“游戏”的设计者设计的&#xff0c;旨在快速高效地筛选出合适的人选。 但是我还是要说&#x…

浅学爬虫-python爬虫基础

介绍与应用 Python爬虫是指利用Python编写程序从互联网上自动获取信息的技术。爬虫广泛应用于数据收集、价格监控、内容聚合、市场分析等领域。其基本原理是模拟浏览器发送HTTP请求获取网页数据&#xff0c;并通过解析HTML来提取所需的信息。 基本工具 Python中有许多强大的…

解析顺序表【数据结构】

1.线性表 线性表&#xff08;linear list&#xff09;是n个具有相同特性的数据元素的有线序列。线性表是一种在实际中广泛使用的数据结构&#xff0c;常见的线性表有&#xff1a;顺序表、链表、栈、队列、字符串… 线性表在逻辑上是线性结构&#xff0c;也就是说是连续的一条线…

linux常见面试题(三)

18 什么事SQL注入 由于程序员的水平及经验参差不齐&#xff0c;大部分程序员在编写代码的时候&#xff0c;没有对用户输入数据的合法性进行判断。 ​ 应用程序存在安全隐患。用户可以提交一段数据库查询代码&#xff0c;根据程序返回的结果&#xff0c;获得某些他想得知的数据…

log4j2漏洞练习

log4j2 是Apache的一个java日志框架&#xff0c;我们借助它进行日志相关操作管理&#xff0c;然而在2021年末log4j2爆出了远程代码执行漏洞&#xff0c;属于严重等级的漏洞。apache log4j通过定义每一条日志信息的级别能够更加细致地控制日志生成地过程&#xff0c;受影响的版本…