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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Golang中由零值和gob库特性引起BUG解析

    Golang中由零值和gob库特性引起BUG解析

    这篇文章主要为大家介绍了Golang中由零值和gob库特性引起BUG解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • Go 1.22版本新特性前瞻

    Go 1.22版本新特性前瞻

    这篇文章主要为大家介绍了Go 1.22版本新特性前瞻,包含语言的变化,编译器、运行时与工具链等应用对比详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • golang网络数据包捕获库gopacket详解

    golang网络数据包捕获库gopacket详解

    gopacket/pcap是Go语言网络数据包捕获库,支持实时捕获、过滤、解析pcap文件及接口统计,结合layers包可分析DNS流量,处理CNAME、多IP等复杂情况,需注意权限和性能优化,适用于网络监控与安全工具开发
    2025-07-07
  • GoLand+CPolar 远程开发实战指南:在家也能连公司服务器写代码?

    GoLand+CPolar 远程开发实战指南:在家也能连公司服务器写代码?

    本文详细介绍如何利用GoLand和CPolar实现高效的远程开发方案,从安装配置GoLand和CPolar到设置远程连接,再到创建固定TCP地址,确保开发者在不同地点也能保持本地开发的效率,感兴趣的朋友跟随小编一起看看吧
    2026-05-05
  • go语言中GOPATH GOROOT的作用和设置方式

    go语言中GOPATH GOROOT的作用和设置方式

    这篇文章主要介绍了go语言中GOPATH GOROOT的作用和设置方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-05-05
  • 使用Go语言实现常见hash算法

    使用Go语言实现常见hash算法

    这篇文章主要为大家详细介绍了使语言实现各种常见hash算法的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,需要的小伙伴可以参考下
    2024-01-01
  • 浅析go语言如何实现协程的抢占式调度的

    浅析go语言如何实现协程的抢占式调度的

    go语言通过GMP模型实现协程并发,为了避免单协程持续持有线程导致线程队列中的其他协程饥饿问题,设计者提出了一个抢占式调度机制,本文会基于一个简单的代码示例对抢占式调度过程进行深入讲解剖析
    2024-04-04
  • 一文详解下划线字段在golang结构体中的应用

    一文详解下划线字段在golang结构体中的应用

    这篇文章主要为大家详细介绍了下划线字段在golang结构体中应用的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-08-08
  • Golang中Gin框架的使用入门教程

    Golang中Gin框架的使用入门教程

    这篇文章主要为大家详细介绍了Golang中Gin框架的使用教程,文中通过简单的示例为大家讲解了Gin框架的安装与使用,感兴趣的小伙伴开业跟随小编一起学习一下
    2022-10-10
  • Golang实现自己的Redis(TCP篇)实例探究

    Golang实现自己的Redis(TCP篇)实例探究

    这篇文章主要介绍了Golang实现自己的Redis(TCP篇)实例探究,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01

最新评论