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

在这里插入图片描述