Qt GRPC
The Qt GRPC and Qt Protobuf modules together allow you to define data and messages in proto
files, and then use the code generators, which generate code allowing accessors for fields and gRPC services in the Qt framework. Such code allows users to communicate with the server by sending calls or stream messages.
Qt GRPC和Qt Protobuf模块一起允许在proto文件中定义数据和消息,然后使用代码生成器,生成允许访问Qt框架中的字段和GRPC服务的代码。这样的代码允许用户通过发送呼叫或流消息与服务器进行通信。
Overview
概述
gRPC is a cross-platform high performance Remote Procedure Call (RPC) framework, that generates client/server bindings for a lot of languages. Usually, you use it to connect services in a microservices-style architecture or connecting mobile applications and browsers to backend services. The gRPC clients and servers can run and talk to each other in various environments, and you can write in any of gRPC’s supported languages. For more details see gRPC Introduction
gRPC是一个跨平台的高性能远程过程调用(RPC)框架,它为许多语言生成客户端/服务器绑定。通常,使用它来连接微服务风格架构中的服务,或者将移动应用程序和浏览器连接到后端服务。gRPC客户端和服务器可以在各种环境中运行并相互通信,并且可以使用gRPC支持的任何语言进行编写。有关更多详细信息,请参阅gRPC简介
Using the Module
使用模块
Module prerequisites:
模块先决条件:
protoc
, the Google protocol buffers compiler, must be installed to generate code from.proto
specification files. See Protoc Installation.- protoc,谷歌协议缓冲区编译器,必须安装才能从.proto规范文件生成代码。请参阅Protoc安装。
- If you also install the gRPC libraries, you'll be able to use native gRPC channels. See gRPC for C++ for details.
- 如果还安装了gRPC库,将能够使用本机gRPC通道。有关详细信息,请参见gRPC for C++。
To start working with the Qt GRPC functionality you should define required services and messages in a .proto
file. See the helloworld.proto
example:
要开始使用Qt GRPC功能,应该在.proto文件中定义所需的服务和消息。请参阅helloworld.proto示例:
// The service definition.
service Salutation {
// Sends a greeting
rpc SendHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
To add a .proto
file to a Qt-based project and generate the required source code, you should use the protoc
compiler with the qtgrpcgen
and qtprotobufgen
Qt plugins.
要向基于Qt的项目添加.proto文件并生成所需的源代码,应该使用带有qtgrpcgen和qtprotobufgen Qt插件的proto编译器。
- The Qt Protobuf plugin generates classes that you can use to serialize and deserialize their associated protobuf messages.
- QtProtobuf插件生成的类可用于序列化和反序列化其关联的Protobuf消息。
- The Qt GRPC plugin generates gRPC client classes from the proto file.
- Qt GRPC插件从proto文件生成GRPC客户端类。
This processing of proto
files into source code can be automated in CMake using the following commands provided by Qt:
使用Qt提供的以下命令,可以在CMake中自动将原型文件处理为源代码:
As a result, the full example of a CMake project file, that uses Qt GRPC functionality shall be:
因此,使用Qt GRPC功能的CMake项目文件的完整示例应为:
cmake_minimum_required(VERSION 3.16...3.22)
project(MyProject)
find_package(Qt6 REQUIRED COMPONENTS Protobuf Grpc)
qt_standard_project_setup()
qt_add_protobuf(MyProtoMessageLib
PROTO_FILES
path/to/helloworld.proto
PROTO_INCLUDES
path/to/proto/include
)
qt_add_grpc(MyGrpcClient CLIENT
PROTO_FILES
path/to/helloworld.proto
PROTO_INCLUDES
path/to/proto/include
)
qt_add_executable(MyApp main.cpp)
target_link_libraries(MyApp PRIVATE MyGrpcClient MyProtoMessageLib Qt6::Protobuf)
The example above calls the qt_add_grpc()
CMake function to generate a library called MyGrpcClient
.
上面的示例调用qt_add_grpc()CMake函数来生成一个名为MyGrpcClient的库。
Note: if the
.proto
file API contains messages, then theqt_add_protobuf()
CMake function should be called to generate protobuf message classes for the project.注意:如果.proto文件API包含消息,则应调用qt_add_protobuf()CMake函数为项目生成protobuf消息类。
Finally, the example creates a target for an executable called MyApp which links to the MyGrpcClient and MyProtoMessageLib libraries.
最后,该示例为名为MyApp的可执行文件创建了一个目标,该可执行文件链接到MyGrpcClient和MyProtoMessageLib库。
Class Documentation
类文档
CMake API
Licenses and Trademarks
许可证和商标
Qt GRPC is available under commercial licenses from The Qt Company. In addition, it is available under the GNU General Public License, version 3. See Qt Licensing for further details.
Qt GRPC可从Qt公司获得商业许可。此外,它还可以在GNU通用公共许可证第3版下使用。有关更多详细信息,请参阅Qt许可。
gRPC® is a registered trademark of The Linux Foundation. Please see gRPC for more information.
gRPC®是Linux基金会的注册商标。有关详细信息,请参阅gRPC。
Examples
示例
© 2023 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.