当前位置: 首页 > news >正文

领秀网站建设广州网站改版 网站建设

领秀网站建设,广州网站改版 网站建设,微信上浏览自己做的网站,室内设计网站都有哪些平台文章目录 一、Protobuf二、安装以及使用protoc三、gRPC1.QA2.学习版rpc3.gRPC压缩算法 参考 一、Protobuf Google Protocol Buffers#xff08;protobuf#xff09;是一种语言中立、平台中立的序列化协议#xff0c;旨在高效地将结构化数据进行序列化和反序列化。它主要… 文章目录 一、Protobuf二、安装以及使用protoc三、gRPC1.QA2.学习版rpc3.gRPC压缩算法 参考 一、Protobuf Google Protocol Buffersprotobuf是一种语言中立、平台中立的序列化协议旨在高效地将结构化数据进行序列化和反序列化。它主要用于通信协议、数据存储和其他需要高效编码和解码结构化数据的场景。protobuf 由 Google 开发和开源广泛用于 Google 的内部系统以及众多开源项目和商业应用中。 Protobuf 的用途 1数据序列化 Protobuf 将数据结构化为紧凑的二进制格式适用于网络传输、持久化存储等需要高效数据编码的场景。相较于 XML 和 JSONprotobuf 编码后的数据占用更少的空间解析速度更快。跨语言和跨平台通信 2Protobuf 支持多种编程语言如 C, Java, Python, Go, Ruby 等适用于异构系统间的通信。 数据结构定义在 .proto 文件中不同语言的代码生成器可以从 .proto 文件生成相应语言的类实现数据的编解码。远程过程调用RPC 3Protobuf 可以与 gRPC 结合使用定义和实现高效的 RPC 协议支持流式传输和双向通信。 gRPC 通过 protobuf 定义服务接口和消息格式自动生成客户端和服务端代码简化了分布式系统的开发。 4数据存储 Protobuf 可以用于将数据序列化后存储到文件或数据库中确保数据存储和传输的高效性。由于 protobuf 的二进制格式紧凑特别适合在存储空间有限或网络带宽受限的环境中使用。 5Protobuf 的优点 高效性序列化后的数据格式紧凑占用更少的存储空间和带宽解析速度快。可扩展性支持向后兼容和向前兼容允许在不破坏现有数据格式的情况下添加新字段。多语言支持生成的代码可在多种编程语言中使用便于不同语言系统之间的数据交换。简洁性定义数据结构的 .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 iostreamusing namespace yaojun; int main() {Person p;p.set_name(test);p.set_id(100);p.set_email(940334249qq.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 --versiongrpc以及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代码 #--pluginprotoc-gen-grpcwhich grpc_cpp_plugin指定使用 gRPC 插件 grpc_cpp_plugin 来生成 gRPC 相关代码 $ protoc -I. --grpc_out. --pluginprotoc-gen-grpcwhich grpc_cpp_plugin add.proto#-I.指定 .proto 文件的包含路径。这意味着 protoc 在当前目录下查找 add.proto 文件。 #--cpp_out.指定生成的 C 代码的输出目录为当前目录。生成的 Protocol Buffers 数据结构代码将放在当前目录下。 # 生成add.pb.hadd.pb.cc的protobuffer 代码 protoc -I. --cpp_out. add.proto计算器服务端代码 #include iostream #include grpcpp/grpcpp.h #include add.grpc.pb.husing 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_ptrServer 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 memoryusing calculator::AddRequest; using calculator::AddResponse; using calculator::Calculator; using grpc::Channel; using grpc::ClientContext; using grpc::Status;class CalculatorClient { public:CalculatorClient(std::shared_ptrChannel 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_ptrCalculator::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_COMMANDSON cmake --build build -j5Python客户端 生成客户端代码 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(operand110, operand220)# 调用远程函数response stub.Add(request)print(Sum:, response.result)if __name__ __main__:run() 1.QA 如果我会使用rpc能给我带来什么好处呢 使用rpc可以屏蔽掉底层传输层的协议只关注我们需要调用的函数接口而且grpc性能很好。grpc是跨语言的工具意思是客户端可以是Python实现而服务端可以是C实现。自己实现一个rpc库需要考虑哪些方面 序列化协议选择服务发现和注册负载均衡跨语言性能。 序列化协议例如 Protocol Buffers、MessagePack、JSON 等 2.学习版rpc button_rpctinyrpcmini-tinyrpc 3.gRPC压缩算法 gRPC 支持多种压缩算法开发者可以根据应用需求选择适当的算法。以下是 gRPC 支持的主要压缩算法 gzip gRPC 默认使用的压缩算法。它是一种通用的压缩算法具有较高的压缩比和广泛的支持。identity 这是一种无压缩算法即不进行压缩。如果你希望在 gRPC 中禁用压缩可以选择使用 “identity”。deflate gRPC 支持使用 deflate 算法进行压缩。这是一种流行的压缩算法类似于 gzip但在某些情况下可能表现不同。 参考 从零开始protobuf原理与实战代码详解protobuf codeProtocol Buffer Compiler Installation从零开始学习gRPC实现高性能跨语言微服务【C和Python】Protobuf和gRpc快速实践
http://www.dnsts.com.cn/news/20163.html

相关文章:

  • 外包网站平台备案期间 需要关闭网站
  • dede换网站wordpress 教程 书籍
  • 中国建设银行招投标网站linux主网站设计
  • 网站建设做一个要多久网络营销方案设计
  • 我想自己做一个网站医药公司网站建设
  • 机票便宜 网站建设房山 网站建设
  • 电子商务网站APP怎么建设网站视频教程
  • 注册完域名 如何做网站未来做哪些网站致富
  • 请人做网站需要注意什么镇安县住房和城乡建设部网站
  • 手机建设银行网站进不去合理使用说明
  • 做微电影模板下载网站seo营销课程培训
  • 网络游戏制作软件seo基础优化包括哪些内容
  • 做网站需要注意青岛市专业做网站的吗
  • 有个找人做任务赚返佣的网站seo网站描述之间用什么标点符号
  • 免费企业网站空间上海申远装饰公司官网
  • 跨境电商网站设计怎么搜索网站搜索量
  • 苗木企业网站建设源代码 园林网站源码程序 苗圃花卉网站制作源码工业设计公司取名
  • 顺德 网站设计主题猫仿虎嗅wordpress
  • 免费主页空间的网站网站集群系统建设
  • 肇庆网站建设方案咨询邢台市建设局安全监督管理网站
  • 如何在自己电脑上建设网站网络营销推广公司策划方案
  • 用网站模板做网站个人主题网站设计
  • 网站安全证书wordpress 开发
  • 文化馆网站建设意义东莞市城乡和住房建设局
  • 做论坛网站需要多少钱天眼查官网在线查询
  • 最高级网站建设廊坊开发区规划建设局网站
  • 跨境电商导购网站建设深圳app建设公司
  • 湖南网站设计案例百度招聘平台
  • 建立企业网站的缺点动态的网站大概多少钱
  • 网站你应该明白什么意思吗网页设计与制作简答题答案