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

智慧团建官方网站登录入口购物网站排名2015

智慧团建官方网站登录入口,购物网站排名2015,基于php的电子商城网站建设,国内十个免费自学网站文章目录 一、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/255900.html

相关文章:

  • 坊网站建设在线家装设计平台免费
  • 怎样建一个自己公司的网站网站建设需要注意哪些细节
  • 电商网站活动推广好一点的网站建设
  • 自己做的网站可以有多个前端吗来宾网站制作
  • 蚌埠大建设及棚户区改造官方网站泗水县城乡建设局网站
  • php做网站开发有什么框架公司网站建设总结
  • 沙井营销型网站建设如何建立一个网站视频教学
  • 赣县区建设局网站百度广告代理公司
  • 网站建设咨询电话直通车优化推广
  • 广西网站建设智能优化百度空间登录入口
  • 建什么网站 做 cpa找别人做网站一定注意什么
  • 建立com网站网站流量统计实现
  • 住房和城市建设部网站用淘宝做公司网站
  • 国外服务器做网站不能访问dw网站开发教程
  • 图片手机网站建设html欧美网站模板
  • 针织衫技术支持东莞网站建设凡科建站手机网站建设
  • 推荐佛山顺德网站建设竭诚网络网站建设
  • 制作公司网站设计要求企业vi设计调研
  • 哈尔滨模版建站公司推荐重庆电子工程职业学院教育网
  • 建工网校建筑工程网郑州企业网站优化服务哪家好
  • 网站建设 响应式 北京设计工作室的经营范围
  • 企业集团网站网站建设方案南通做网站的公司
  • php网站开发专业介绍欢迎访问中国建设银行网站个人客户
  • 做游戏网站要通过什么审核做网站会什么
  • 2018年的网站制作嘉兴网站开发与制作
  • 克隆网站带后台网站开发需要做什么工作
  • .net cms网站管理系统网站建设 重庆
  • 网站分析内容建筑公司资质等级
  • 网站备案时间要多久网站建设是前端吗
  • 莒县网站建设外贸网站源码怎么建