Qt6.5 grpc组件使用 + golang grpc server示例详解

 更新时间:2023年05月27日 09:52:47   作者:听我一言  
这篇文章主要介绍了Qt6.5 grpc组件使用+golang grpc server示例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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只封装支持了客户端,服务端暂未支持;

在这里插入图片描述

到此这篇关于Qt6.5 grpc组件使用 + golang grpc server示例的文章就介绍到这了,更多相关 golang grpc server内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • go如何优雅关闭Graceful Shutdown服务

    go如何优雅关闭Graceful Shutdown服务

    这篇文章主要为大家介绍了go优雅关闭Graceful Shutdown服务详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • 使用golang如何优雅的关机或重启操作示例

    使用golang如何优雅的关机或重启操作示例

    这篇文章主要为大家介绍了使用golang如何优雅的关机或重启操作示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2022-04-04
  • Go语言中的基础数据类型使用实例

    Go语言中的基础数据类型使用实例

    这篇文章主要为大家介绍了Go中的基础数据类型使用示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • Golang算法之田忌赛马问题实现方法分析

    Golang算法之田忌赛马问题实现方法分析

    这篇文章主要介绍了Golang算法之田忌赛马问题实现方法,结合具体实例形式分析了基于Go语言的田忌赛马问题原理与算法实现技巧,需要的朋友可以参考下
    2017-02-02
  • golang socket断点续传大文件的实现方法

    golang socket断点续传大文件的实现方法

    今天小编就为大家分享一篇golang socket断点续传大文件的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • Golang 实现 Redis系列(六)如何实现 pipeline 模式的 redis 客户端

    Golang 实现 Redis系列(六)如何实现 pipeline 模式的 redis 客户端

    pipeline 模式的 redis 客户端需要有两个后台协程负责 tcp 通信,调用方通过 channel 向后台协程发送指令,并阻塞等待直到收到响应,本文是使用 golang 实现 redis 系列的第六篇, 将介绍如何实现一个 Pipeline 模式的 Redis 客户端。
    2021-07-07
  • Golang实践之Error创建和处理详解

    Golang实践之Error创建和处理详解

    在 C#、Java 等语言中常常使用 try...catch的方式来捕获异常,但是在Golang 对于错误处理有不同的方式,像网上也有很多对 error 处理的最佳实践的文章,其中很多其实就是对 error 的统一封装,使用规范进行约束,本文主要是记录自己对处理 Error 的一些认识和学习
    2023-09-09
  • Golang Mutex错过会后悔的重要知识点分享

    Golang Mutex错过会后悔的重要知识点分享

    互斥锁 Mutex 是并发控制的一个基本手段,是为了避免并发竞争建立的并发控制机制,本文主要为大家整理了一些Mutex的相关知识点,希望对大家有所帮助
    2023-07-07
  • Go语言实现二分查找方法示例

    Go语言实现二分查找方法示例

    这篇文章主要为大家介绍了Go语言实现二分查找方法示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • GO的range具体使用

    GO的range具体使用

    GO语言的for…range 能做什么呢?golang的for…range是go 身的语法,可以用来遍历数据结构,本文就详细的来介绍一下具体使用,感兴趣的可以了解一下
    2021-10-10

最新评论