QGrpcHttp2Channel Class
QGrpcHttp2Channel 类为通信提供了 HTTP/2 传输层。 gRPC™通信的传输层。更多
头文件: | #include <QGrpcHttp2Channel> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Grpc) target_link_libraries(mytarget PRIVATE Qt6::Grpc) |
自 | Qt 6.5 |
在 QML 中: | GrpcHttp2Channel |
继承: | QAbstractGrpcChannel |
公共函数
QGrpcHttp2Channel(const QUrl &hostUri) | |
QGrpcHttp2Channel(const QUrl &hostUri, const QGrpcChannelOptions &options) | |
virtual | ~QGrpcHttp2Channel() override |
QUrl | hostUri() const |
详细说明
QGrpcHttp2Channel 类实现了QAbstractGrpcChannel ,可通过 HTTP/2 框架进行通信。 gRPC™通过HTTP/2 框架进行通信。
HTTP/2 与其前身 HTTP/1.1 相比具有多项优势,因此 QGrpcHttp2Channel 非常适合需要高效通信的高性能实时应用程序,通过使用多路复用 TCP 连接,不会牺牲安全性或可靠性。
通过使用包含所需定制功能的QGrpcChannelOptions 构建该通道,可以定制SSL支持、定制serializationFormat 或其他选项。
传输方案
QGrpcHttp2Channel 实现会根据所提供的hostUri
、scheme 和选项选择不同的传输方式。适用的标准如下:
方案 | 描述 | 默认端口 | 要求 | 示例 |
---|---|---|---|---|
http | 通过 TCP 的未加密 HTTP/2 | 80 | 无 | http://localhost |
https | 通过 TCP 的 TLS 加密 HTTP/2 | 443 | QSslSocket 支持AND(schemeOR sslConfiguration) | https://localhost |
unix | 文件系统路径中的 Unix 域套接字 | ✗ | QLocalSocket 支持和方案 | unix:///tmp/grpc.socket |
内容类型
gRPC over HTTP/2 中的内容类型决定了消息的序列化格式。它必须以application/grpc
开头,并可包含后缀。格式遵循此方案:
"content-type": "application/grpc" [("+proto" / "+json" / {custom})]
例如
application/grpc+proto
指定 Protobuf 编码。application/grpc+json
指定 JSON 编码。
可以通过在元数据中指定content-type
或直接设置serializationFormat 来配置序列化格式。默认情况下,使用application/grpc
content-type。
要使用content-type
元数据配置 QGrpcHttp2Channel 的 JSON 序列化格式:
auto jsonChannel = std::make_shared<QGrpcHttp2Channel>( QUrl("http://localhost:50051"_L1), QGrpcChannelOptions().setMetadata({ { "content-type"_ba, "application/grpc+json"_ba }, }) );
对于自定义序列化器和content-type
,可以直接设置序列化格式:
class DummySerializer : public QAbstractProtobufSerializer { ... }; QGrpcSerializationFormat dummyFormat("dummy", std::make_shared<DummySerializer>());
auto dummyChannel = std::make_shared<QGrpcHttp2Channel>( QUrl("http://localhost:50051"_L1), QGrpcChannelOptions().setSerializationFormat(dummyFormat) );
这将使用DummySerializer
对带有dummy
后缀的消息进行编码和解码。对于 HTTP/2 传输,这将导致application/grpc+dummy
content-type。
注意: 自定义序列化器要求服务器支持指定格式。
另请参阅 QAbstractGrpcChannel,QGrpcChannelOptions, 和QGrpcSerializationFormat 。
成员函数文档
[explicit]
QGrpcHttp2Channel::QGrpcHttp2Channel(const QUrl &hostUri)
用hostUri 构建 QGrpcHttp2Channel。更多信息请参阅Transportation scheme 部分。
[explicit]
QGrpcHttp2Channel::QGrpcHttp2Channel(const QUrl &hostUri, const QGrpcChannelOptions &options)
用hostUri 和options 构造 QGrpcHttp2Channel。更多信息请参阅Transportation scheme 部分。
[override virtual noexcept]
QGrpcHttp2Channel::~QGrpcHttp2Channel()
销毁QGrpcHttp2Channel 对象。
QUrl QGrpcHttp2Channel::hostUri() const
返回该频道的主机 URI。
© 2025 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.