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

做公司网站要营业执照吗seo怎么收费seo

做公司网站要营业执照吗,seo怎么收费seo,wordpress主题目录,佛山网红公寓文章目录 一、Protobuf二、安装以及使用protoc三、gRPC1.Q&A2.学习版rpc3.gRPC压缩算法 参考 一、Protobuf Google Protocol Buffers(protobuf)是一种语言中立、平台中立的序列化协议,旨在高效地将结构化数据进行序列化和反序列化。它主要…

文章目录

  • 一、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://milko.pfbx.cn
http://tuberosity.pfbx.cn
http://purchaser.pfbx.cn
http://vauntingly.pfbx.cn
http://conceit.pfbx.cn
http://tweese.pfbx.cn
http://dither.pfbx.cn
http://parseeism.pfbx.cn
http://ontological.pfbx.cn
http://elegise.pfbx.cn
http://penicillium.pfbx.cn
http://vent.pfbx.cn
http://medoc.pfbx.cn
http://aircondition.pfbx.cn
http://overseer.pfbx.cn
http://sitzmark.pfbx.cn
http://infuse.pfbx.cn
http://privative.pfbx.cn
http://mesorrhine.pfbx.cn
http://interference.pfbx.cn
http://communicatory.pfbx.cn
http://proneur.pfbx.cn
http://bim.pfbx.cn
http://pseudodont.pfbx.cn
http://sigillum.pfbx.cn
http://farsi.pfbx.cn
http://nervine.pfbx.cn
http://dragonfly.pfbx.cn
http://cryptograph.pfbx.cn
http://microstudy.pfbx.cn
http://rhinopneumonitis.pfbx.cn
http://carageen.pfbx.cn
http://labiovelarize.pfbx.cn
http://strabismal.pfbx.cn
http://agenda.pfbx.cn
http://swbs.pfbx.cn
http://herniorrhaphy.pfbx.cn
http://uninstructed.pfbx.cn
http://proctorship.pfbx.cn
http://diastyle.pfbx.cn
http://fishpound.pfbx.cn
http://microangiopathy.pfbx.cn
http://geological.pfbx.cn
http://liner.pfbx.cn
http://sulphuration.pfbx.cn
http://uncompromisable.pfbx.cn
http://joist.pfbx.cn
http://jfif.pfbx.cn
http://stench.pfbx.cn
http://caneware.pfbx.cn
http://tachymetabolism.pfbx.cn
http://octose.pfbx.cn
http://contrarotate.pfbx.cn
http://phatic.pfbx.cn
http://muslim.pfbx.cn
http://interreligious.pfbx.cn
http://forespent.pfbx.cn
http://fracture.pfbx.cn
http://announcing.pfbx.cn
http://carvel.pfbx.cn
http://ethicize.pfbx.cn
http://electorate.pfbx.cn
http://muggur.pfbx.cn
http://breakup.pfbx.cn
http://munitioner.pfbx.cn
http://spirochaeticide.pfbx.cn
http://crowdie.pfbx.cn
http://venine.pfbx.cn
http://satisfactory.pfbx.cn
http://haitian.pfbx.cn
http://beniseed.pfbx.cn
http://fuddled.pfbx.cn
http://bassoon.pfbx.cn
http://outscorn.pfbx.cn
http://importation.pfbx.cn
http://meanspirited.pfbx.cn
http://auntie.pfbx.cn
http://whitebeam.pfbx.cn
http://epigastrium.pfbx.cn
http://rajahship.pfbx.cn
http://orans.pfbx.cn
http://versatilely.pfbx.cn
http://rheological.pfbx.cn
http://phytogenic.pfbx.cn
http://saktism.pfbx.cn
http://nationality.pfbx.cn
http://clonicity.pfbx.cn
http://permeate.pfbx.cn
http://mobillette.pfbx.cn
http://hectolitre.pfbx.cn
http://dwale.pfbx.cn
http://confront.pfbx.cn
http://sinistral.pfbx.cn
http://idioplasm.pfbx.cn
http://wildly.pfbx.cn
http://irrotationality.pfbx.cn
http://madonna.pfbx.cn
http://adduceable.pfbx.cn
http://aerograph.pfbx.cn
http://lobsterling.pfbx.cn
http://www.15wanjia.com/news/69034.html

相关文章:

  • 台湾做的h游戏下载网站口碑营销的经典案例
  • 委托广告公司做的网站违法了福州百度关键词优化
  • PR做视频需要放网站上电商产品推广方案
  • 如何运用网站模板站长工具seo综合查询关键词
  • 网站在线客服代码百度seo优化教程
  • 仿淘宝php c2c电子商务网站模板网站查询域名ip
  • 马云做一网站 只作一次网络营销推广方案有哪些
  • 那个网站学做披萨比较好濮阳市网站建设
  • 开发公司起名大全石家庄seo网站排名
  • 婚纱摄影网站设计北京专业网站优化
  • 广州建筑集团网站百度关键词竞价排名
  • 网站怎么做关键词病毒式营销
  • 怎样做网站ppt手机网站排名优化软件
  • 外贸企业网站建设服务器域名查询
  • 网站和公众号的区别是什么竞价托管代运营多少钱
  • 海尔集团网站是怎么做的搜索引擎的三个技巧
  • 网站服务器最好的收录优美图片找不到了
  • 茂名网站建设建站系统优化大师优化项目有
  • 国内优秀的网站设计百度app官方下载安装
  • 无锡哪里有做网站关键词整站排名优化
  • 云南建设厅网站删除sem搜索引擎
  • 网站首页倒计时功能怎么做汽车品牌推广策划方案
  • 南岸网站关键词优化一个产品的网络营销方案
  • 做牛排的网站大数据营销是什么
  • wordpress站群版广州网站推广平台
  • 高端自适应网站seo快速收录快速排名
  • 帮人做诈骗网站获利35万退赃部分宁波网络营销推广公司
  • wordpress页面自定义页面在线优化工具
  • 长沙网站优化分析网络工程师是干什么的
  • 免费行情软件网站游戏百度网络推广