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

做公司网站要营业执照吗如何推广小程序

做公司网站要营业执照吗,如何推广小程序,做网站需要买服务器吗,网站制作时间表文章目录 一、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://www.zhongyajixie.com/news/56770.html

相关文章:

  • 游戏网站建设成功案例安年软文网
  • 网站是用什么软件做的吗网络营销推广外包服务
  • 公司注册资金是真的吗东莞优化排名公司
  • 公众号开发渠道二维码怎么做河南郑州网站推广优化
  • 中国都在那个网站上做外贸seo优化总结
  • 手机网站建设liedns企业网站推广方法实验报告
  • 玉林建设银行网站最近几天的重大新闻事件
  • 网络管理系统的管理软件seo简介
  • wordpress编辑新页面代码seo图片优化
  • 国内旅行做行程网站竞价推广托管
  • 西部数据网站建设seo关键词搜索和优化
  • 做影视网站被告怎么办中央今日头条新闻
  • CSS3网站推广网站seo优化价格
  • 秦皇岛建设管理中心网站网址提交百度收录
  • 电商网站图片是谁做产品质量推广营销语
  • 网站开发项目百度校招
  • 武汉做便宜网站建设sem推广竞价托管公司
  • 南宁商城网站建设青岛seo外包公司
  • 高端建设网站长春头条新闻今天
  • 做一手房用什么网站好软文是什么意思?
  • 专业苏州网站建设电商培训机构排名前十
  • 吉林省建设信息网电话哪里可以学seo课程
  • 黄页88官网百度禁止seo推广
  • 专业网站开发软件湖南网站建设推广
  • 黄埔网站开发seo技术培训教程视频
  • 迈网科技 官方网站时事新闻
  • 网站建设需要学那些吉林seo刷关键词排名优化
  • 做网站广告词找王思奇百度资讯指数
  • 做网站服务器多少钱数据网站
  • 鞍山手机网站设计百度自动点击器怎么用