Qt6.5 grpc组件使用 + golang grpc server示例详解
作者:听我一言 发布时间:2024-02-07 21:25:12
1. 资料
1) Protobuf 开发文档
https://protobuf.dev/
2) protobuf安装指南
https://grpc.io/docs/protoc-installation/
3) protoc 下载
https://github.com/protocolbuffers/protobuf/releases/tag/v23.1
2. Qt grpc 组件 & 工具
1) Qt6.5 安装目录下 xx\Qt\6.5.0\mingw_64\bin
i. qtgrpcgen.exe 将proto转成Qt 库 的 grpc客户端
ii. qtprotobufgen.exe 将proto转成带Qt封装的 的 protobuf接口
2) 指令使用
helloworld.proto 文件
syntax = "proto3";
package helloworld;
message HelloRequest {
string name = 1;
}
message HelloResponse {
string message = 1;
}
service HelloService {
rpc SayHello (HelloRequest) returns (HelloResponse) {}
}
option go_package = "proto/helloworld";
1) 生成Qt封装的protobuf接口
protoc.exe --plugin=protoc-gen-qtprotobuf=E:\qt599\Qt\6.5.0\mingw_64\bin\qtprotobufgen.exe -I “D:/workspace/Qt/grpc_test/common” --qtprotobuf_out=“D:/workspace/Qt/grpc_test/common” “D:/workspace/Qt/grpc_test/common/helloworld.proto”
2) 生成Qt grpc客户端
protoc --plugin=protoc-gen-qtgrpc=E:/qt599/Qt/6.5.0/mingw_64/bin/qtgrpcgen.exe --qtgrpc_out=“D:/workspace/Qt/grpc_test/common” -I “D:/workspace/Qt/grpc_test/common” “D:/workspace/Qt/grpc_test/common/helloworld.proto”
3) 客户端调用代码
#include <QCoreApplication>
#include <QGrpcInsecureChannelCredentials>
#include "helloworld_client.grpc.qpb.h"
#include <QGrpcHttp2Channel>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
helloworld::HelloService::Client client;
auto channel = std::shared_ptr<QAbstractGrpcChannel>(new QGrpcHttp2Channel(
QUrl("http://localhost:50051",
QUrl::StrictMode),
QGrpcInsecureChannelCredentials()
| QGrpcInsecureCallCredentials()));
client.attachChannel(channel);
helloworld::HelloRequest req;
helloworld::HelloResponse rep;
req.setName("gray");
QGrpcStatus status = client.SayHello(req, &rep);
qDebug() << "Request Result: " << status.code() << status.message();
qDebug() << "REP : " << rep.message();
return a.exec();
}
3. Golang服务端
1) 生成golang 带grpc接口文件
protoc.exe -I D:/workspace/Qt/grpc_test/common --proto_path=“D:/workspace/grpc/protoc/include/google/protobuf” --plugin=protoc-gen-go=D:/workspace/grpc/protoc/bin/protoc-gen-go.exe --go_out=plugins=grpc:D:/workspace/Qt/grpc_test/common D:/workspace/Qt/grpc_test/common/helloworld.proto
2) 示例代码
再创建一个main.go,调用函数RunServer即可
package proto
import (
context "context"
"flag"
"fmt"
"log"
"net"
grpc "google.golang.org/grpc"
)
var (
port = flag.Int("port", 50051, "The server port")
)
type Server struct {
HelloServiceServer
}
// SayHello implements helloworld.GreeterServer
func (s *Server) SayHello(ctx context.Context, in *HelloRequest) (*HelloResponse, error) {
fmt.Printf("Received: %v\n", in.GetName())
return &HelloResponse{Message: "Hello " + in.GetName()}, nil
}
func RunServer() error {
flag.Parse()
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
RegisterHelloServiceServer(s, &Server{})
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
return err
}
4. 介绍一下Qt6.5支持哪些grpc功能
由Qt6.5 帮助文档可知道, 现在Qt6.5只封装支持了客户端,服务端暂未支持;
来源:https://blog.csdn.net/halo_hsuh/article/details/130815730
猜你喜欢
- 一、安装1、安装easy_install工具wget http://peak.telecommunity.com/dist/ez_setup
- 我们可以把表里每一个横行的数据,看成是不同的元组。在理解了这个概念后,昨天我们学了不少的namedtuple类,是否也能把元组转换成name
- Django 基本命令本节主要是为了让您了解一些django最基本的命令,请尝试着记住它们,并且多多练习下1. 新建一个 django pr
- 我们选择一种问题的解决办法,通常需要考虑到想要达到的效果,还有最重要的是这个办法本身的优缺点有哪些,与其他的方法对比哪一个更好。之前小编之前
- 此文刊登在《程序员》三月期,有删改提到安全问题,首先想到应付这些问题的应该是系统管理员以及后台开发工程师们,而前端开发工程师似乎离这些问题很
- 最近无意看到网上有人使用Python编写几十行代码生成图像验证码,感觉很是繁琐,这里为各位朋友推荐两种方法,使用4行Python代码即可生成
- Jquery练习表单验证 <body> <form action="" method="po
- 开发自定义Web框架接收web服务器的动态资源请求,给web服务器提供处理动态资源请求的服务。根据请求资源路径的后缀名进行判断:如果请求资源
- scipy.optimize.curve_fit解读相比numpy库的多项式拟合函数polyfit,scipy.optimize模块中的函数
- 模型训练时GPU利用率太低的原因最近在训练SSD模型时发现GPU的利用率只有8%,而CPU的利用率却非常高。后来了解到,一般使用CPU进行数
- txt文件转换为XML很多目标检测的模型都是默认需要VOC的文件输入格式手上数据label是txt文件。为了避免不必要的bug,还是选择转换
- 这篇论坛文章详细的讲解了使用SQL Server 2008管理非结构化数据的具体方法,更多内容请参考下文:microsoft SQL Ser
- 集成 FCKEditor v2.6(当前为最新版本)的基本步骤如下:1. 下载FCKeditor 2.6 基本文件(Main Code)。将
- 我就废话不多说了,大家还是直接看代码吧~n = input("1st enter:")print(n)print(typ
- 一、获取二叉树的深度就是二叉树最后的层次,如下图:实现代码:def getheight(self): &n
- Git跟其他版本控制系统一样,可以打标签(tag)标记一个版本号。发布一个版本时,我们通常先在版本库中打一个标签,这样,就唯一确定了打标签时
- 一.Sobel算子Sobel算子是一种用于边缘检测的离散微分算子,它结合了高斯平滑和微分求导。该算子用于计算图像明暗程度近似值,根据图像边缘
- 作为一个学完Python基础知识的测试,暗喜终于可以像RD们自己写脚本处理任何场景吧,如何优雅地写出来代码,接下来开启进阶版的Python。
- 错误现象:autopep8无法运行,运行后vscode右下角提示Error: Command failed: autopep8 c:\Use
- Python对于json数据键值对遍历Python中可以使用json模块来解析JSON格式的数据,将其转换成Python中的字典或者列表对象