欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Qt6.5 grpc組件使用 + golang grpc server示例詳解

 更新時間:2023年05月27日 09:52:47   作者:聽我一言  
這篇文章主要介紹了Qt6.5 grpc組件使用+golang grpc server示例,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1. 資料

1) Protobuf 開發(fā)文檔

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) 示例代碼

再創(chuàng)建一個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內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • go如何優(yōu)雅關閉Graceful?Shutdown服務

    go如何優(yōu)雅關閉Graceful?Shutdown服務

    這篇文章主要為大家介紹了go優(yōu)雅關閉Graceful?Shutdown服務詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • 使用golang如何優(yōu)雅的關機或重啟操作示例

    使用golang如何優(yōu)雅的關機或重啟操作示例

    這篇文章主要為大家介紹了使用golang如何優(yōu)雅的關機或重啟操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
    2022-04-04
  • Go語言中的基礎數據類型使用實例

    Go語言中的基礎數據類型使用實例

    這篇文章主要為大家介紹了Go中的基礎數據類型使用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • Golang算法之田忌賽馬問題實現方法分析

    Golang算法之田忌賽馬問題實現方法分析

    這篇文章主要介紹了Golang算法之田忌賽馬問題實現方法,結合具體實例形式分析了基于Go語言的田忌賽馬問題原理與算法實現技巧,需要的朋友可以參考下
    2017-02-02
  • golang socket斷點續(xù)傳大文件的實現方法

    golang socket斷點續(xù)傳大文件的實現方法

    今天小編就為大家分享一篇golang socket斷點續(xù)傳大文件的實現方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Golang 實現 Redis系列(六)如何實現 pipeline 模式的 redis 客戶端

    Golang 實現 Redis系列(六)如何實現 pipeline 模式的 redis 客戶端

    pipeline 模式的 redis 客戶端需要有兩個后臺協程負責 tcp 通信,調用方通過 channel 向后臺協程發(fā)送指令,并阻塞等待直到收到響應,本文是使用 golang 實現 redis 系列的第六篇, 將介紹如何實現一個 Pipeline 模式的 Redis 客戶端。
    2021-07-07
  • Golang實踐之Error創(chuàng)建和處理詳解

    Golang實踐之Error創(chuàng)建和處理詳解

    在 C#、Java 等語言中常常使用 try...catch的方式來捕獲異常,但是在Golang 對于錯誤處理有不同的方式,像網上也有很多對 error 處理的最佳實踐的文章,其中很多其實就是對 error 的統一封裝,使用規(guī)范進行約束,本文主要是記錄自己對處理 Error 的一些認識和學習
    2023-09-09
  • Golang?Mutex錯過會后悔的重要知識點分享

    Golang?Mutex錯過會后悔的重要知識點分享

    互斥鎖?Mutex?是并發(fā)控制的一個基本手段,是為了避免并發(fā)競爭建立的并發(fā)控制機制,本文主要為大家整理了一些Mutex的相關知識點,希望對大家有所幫助
    2023-07-07
  • Go語言實現二分查找方法示例

    Go語言實現二分查找方法示例

    這篇文章主要為大家介紹了Go語言實現二分查找方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • GO的range具體使用

    GO的range具體使用

    GO語言的for…range 能做什么呢?golang的for…range是go 身的語法,可以用來遍歷數據結構,本文就詳細的來介紹一下具體使用,感興趣的可以了解一下
    2021-10-10

最新評論