长沙企业建站,做示意图的网站,安顺公司做网站,wordpress主题 添加自定义菜单protobuf
protobuf是由谷歌推出的二进制序列化与反序列化库对象。也是著名GRPC的底层依赖#xff0c;它独立于平台及语言的序列化与反序列化标准库。
相关网址
protobuf IDL描述protobuf 开源库grpc-知乎grpc官方示例
安装protobuf可以使用vcpkg进行简易安装依赖#xff…protobuf
protobuf是由谷歌推出的二进制序列化与反序列化库对象。也是著名GRPC的底层依赖它独立于平台及语言的序列化与反序列化标准库。
相关网址
protobuf IDL描述protobuf 开源库grpc-知乎grpc官方示例
安装protobuf可以使用vcpkg进行简易安装依赖protoc.exe则会安装在installed\x64-windows\tools\protobuf目录下protbuf是谷歌推出的grpc的基础序列化包括QtGrpc也是依赖的此模块作为基础。
.proto是接口描述文件它抽象在平台之外提供了许多语言的支持这个支持是由谷歌推出。 所以你可以说protobuf是跨平台跨语言的一直序列化格式至于支持到什么程序就要看谷歌的支持了。相对于json、xml之类的文本序列化格式具有更高的效率具有差不多程度的跨语言以及平台特性。
安装指令
vcpkg install protobuf protobuf:x64-windows // 安装protobuf
vcpkg install protobuf[zlib] protobuf[zlib]:x64-windows // 安装protobuf zlib压缩
vcpkg install grpc:x64-windows // 安装grpcprotoc指令
// protobuf
// proto IDL生成cpp代码文件 --cpp_out必须是已经存在的目录 proto dir则是存放.proto文件的目录 *.proto IDL接口文件
protoc --proto_path[input: proto dir] --cpp_out[output: cxx dir] [*.proto]
protoc --proto_path./. --cpp_out./ user.proto// grpc
// proto IDL生成cpp grpc代码文件 --grpc_out是grpc文件目录 --cpp_out cxx代码文件目录 --plugin 插件模式及插件软件 *.proto IDL接口文件
protoc --grpc_out[output: grpc dir] --cpp_out[output: cxx dir] --pluginprotoc-gen-grpc[input: pligin] [*.proto]
.\protoc.exe --grpc_out./ --cpp_out./ --pluginprotoc-gen-grpcgrpc_cpp_plugin.exe .\serve.protouser.proto 文件示例
syntax proto2;package jie;message Person {optional string name 1;optional int32 id 2;optional string email 3;enum PhoneType {MOBILE 0;HOME 1;WORK 2;}message PhoneNumber {optional string number 1;optional PhoneType type 2 [default HOME];}repeated PhoneNumber phones 4;
}message AddressBook {repeated Person people 1;
}vcpkg安装protobuf项目使用的配置示例
cmake_minimum_required(VERSION 3.5)set(CMAKE_TOOLCHAIN_FILE D:/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING Vcpkg toolchain file)project(protobuf_item LANGUAGES CXX)set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)include(D:/vcpkg/scripts/buildsystems/vcpkg.cmake)find_package(protobuf CONFIG REQUIRED)# protbuf提供的根据.proto文件生成对应的头文件与cc文件
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS user.proto)
#protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS EXPORT_MACRO DLL_EXPORT user.proto)
#protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS DESCRIPTORS PROTO_DESCS user.proto)#message()
#message(${PROTO_SRCS})
#message(${PROTO_HDRS})
#message()add_executable(protobuf_item main.cpp ${PROTO_SRCS} ${PROTO_HDRS})target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_BINARY_DIR})target_link_libraries(${PROJECT_NAME} PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)grpc
RPC全称Remote Procedure Call中文译为远程过程调用。通俗地讲使用RPC进行通信调用远程函数就像调用本地函数一样RPC底层会做好数据的序列化与传输从而能使我们更轻松地创建分布式应用和服务。
一般来讲实现一个gRPC服务端和客户端主要分为这几步
安装 protobuf 依赖编写 proto 文件IDL编译 proto 文件生成stub文件编写server端实现我们的接口编写client端测试我们的接口
ps大多数时候我们可以手动使用protoc grpc_cpp_plugin来生成 .proto对应的代码来给程序使用这样能简化grpc配置上带来的复杂操作。当然如果你想通过cmake配置文件生成对应的.proto 代码文件也无可厚非如果你有此想法需要研究cmake中的add_custom_command()指令。
一个serve.proto IDL描述文件示例
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an AS IS BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.syntax proto3;option java_multiple_files true;
option java_package io.grpc.examples.helloworld;
option java_outer_classname HelloWorldProto;
option objc_class_prefix HLW;package jie;// The greeting service definition.
service Greeter {// Sends a greetingrpc SayHello (HelloRequest) returns (HelloReply) {}rpc SayHelloStreamReply (HelloRequest) returns (stream HelloReply) {}
}// The request message containing the users name.
message HelloRequest {string name 1;
}// The response message containing the greetings
message HelloReply {string message 1;
}grpc服务端与客户端cmake配置
cmake_minimum_required(VERSION 3.5)# 设置工具链
set(CMAKE_TOOLCHAIN_FILE D:/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING Vcpkg toolchain file)project(grpc_item LANGUAGES CXX)set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)# 引入vcpkg.cmake子模块
include(D:/vcpkg/scripts/buildsystems/vcpkg.cmake)# 使用gRPC库
find_package(gRPC CONFIG REQUIRED)#protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS user.proto)# 查找到执行的 proto命令行以及grpc_cpp_plugin 插件
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
find_program(_PROTOBUF_PROTOC protoc)# 设置文件路径绝对路径
get_filename_component(hw_proto serve.proto ABSOLUTE)
# 获取文件所在的目录
get_filename_component(hw_proto_path ${hw_proto} PATH)# 设置输出的文件变量
set(hw_proto_srcs ${CMAKE_SOURCE_DIR}/serve.pb.cc)
set(hw_proto_hdrs ${CMAKE_SOURCE_DIR}/serve.pb.h)
set(hw_grpc_srcs ${CMAKE_SOURCE_DIR}/serve.grpc.pb.cc)
set(hw_grpc_hdrs ${CMAKE_SOURCE_DIR}/serve.grpc.pb.h)# 执行命令OUTPUT参数为执行命令后最终输出的文件需要完全匹配 DEPENDS依赖文件依然如此这规则令人感到困惑。但你可以理解为COMMADN最终的输出文件为 OUTPUT参数COMMAND输入文件文则是DEPENDS但组织命令参数时仍需要这些参数。
add_custom_command(OUTPUT ${hw_grpc_srcs} ${hw_grpc_hdrs} ${hw_proto_srcs} ${hw_proto_hdrs}COMMAND ${_PROTOBUF_PROTOC}ARGS --grpc_out ${CMAKE_SOURCE_DIR}--cpp_out ${CMAKE_SOURCE_DIR}-I ${hw_proto_path}--pluginprotoc-gen-grpc${_GRPC_CPP_PLUGIN_EXECUTABLE}${hw_proto}DEPENDS ${hw_proto})add_executable(grpc_item main.cpp${PROTO_SRCS} ${PROTO_HDRS}${hw_grpc_srcs} ${hw_grpc_hdrs}${hw_proto_srcs} ${hw_proto_hdrs}
)#target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_BINARY_DIR})target_link_libraries(${PROJECT_NAME} PRIVATE gRPC::gpr gRPC::grpc gRPC::grpc gRPC::grpc_alts)
grpc服务器代码 #include iostream
#include grpcpp/ext/proto_server_reflection_plugin.h
#include grpcpp/grpcpp.h
#include grpcpp/server.h
#include grpcpp/health_check_service_interface.h
#include serve.grpc.pb.hclass JieRpc : public jie::Greeter::Service {
public:::grpc::Status SayHello(::grpc::ServerContext* context,const ::jie::HelloRequest* request,::jie::HelloReply* response) override {std::cout request-name() std::endl;response-set_message(hello Jie Rpc);return ::grpc::Status::OK;}::grpc::Status SayHelloStreamReply(::grpc::ServerContext* context,const ::jie::HelloRequest* request,::grpc::ServerWriter ::jie::HelloReply* writer) override {return ::grpc::Status::OK;}
};int main()
{JieRpc rpc;grpc::ServerBuilder builder;builder.AddListeningPort(localhost:9000, grpc::InsecureServerCredentials());builder.RegisterService(rpc);std::unique_ptrgrpc::Server server(builder.BuildAndStart());server-Wait();
}grpc客户端代码
#include iostream
#include grpcpp/grpcpp.h
#include serve.grpc.pb.hint main()
{std::unique_ptrjie::Greeter::Stub stu(jie::Greeter::NewStub(grpc::CreateChannel(localhost:9000, grpc::InsecureChannelCredentials())));jie::HelloRequest request;jie::HelloReply reply;request.set_name(hello jie);grpc::ClientContext context;grpc::Status status stu-SayHello(context, request, reply);if (status.ok())std::cout reply.message() std::endl;
}