最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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轉(zhuǎn)成Qt 庫 的 grpc客戶端
ii. qtprotobufgen.exe 將proto轉(zhuǎn)成帶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) 客戶端調(diào)用代碼

#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服務(wù)端

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,調(diào)用函數(shù)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 幫助文檔可知道, 現(xiàn)在Qt6.5只封裝支持了客戶端,服務(wù)端暫未支持;

在這里插入圖片描述

到此這篇關(guān)于Qt6.5 grpc組件使用 + golang grpc server示例的文章就介紹到這了,更多相關(guān) golang grpc server內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Golang中由零值和gob庫特性引起B(yǎng)UG解析

    Golang中由零值和gob庫特性引起B(yǎng)UG解析

    這篇文章主要為大家介紹了Golang中由零值和gob庫特性引起B(yǎng)UG解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • Go?1.22版本新特性前瞻

    Go?1.22版本新特性前瞻

    這篇文章主要為大家介紹了Go?1.22版本新特性前瞻,包含語言的變化,編譯器、運行時與工具鏈等應(yīng)用對比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • golang網(wǎng)絡(luò)數(shù)據(jù)包捕獲庫gopacket詳解

    golang網(wǎng)絡(luò)數(shù)據(jù)包捕獲庫gopacket詳解

    gopacket/pcap是Go語言網(wǎng)絡(luò)數(shù)據(jù)包捕獲庫,支持實時捕獲、過濾、解析pcap文件及接口統(tǒng)計,結(jié)合layers包可分析DNS流量,處理CNAME、多IP等復(fù)雜情況,需注意權(quán)限和性能優(yōu)化,適用于網(wǎng)絡(luò)監(jiān)控與安全工具開發(fā)
    2025-07-07
  • GoLand+CPolar 遠程開發(fā)實戰(zhàn)指南:在家也能連公司服務(wù)器寫代碼?

    GoLand+CPolar 遠程開發(fā)實戰(zhàn)指南:在家也能連公司服務(wù)器寫代碼?

    本文詳細介紹如何利用GoLand和CPolar實現(xiàn)高效的遠程開發(fā)方案,從安裝配置GoLand和CPolar到設(shè)置遠程連接,再到創(chuàng)建固定TCP地址,確保開發(fā)者在不同地點也能保持本地開發(fā)的效率,感興趣的朋友跟隨小編一起看看吧
    2026-05-05
  • go語言中GOPATH GOROOT的作用和設(shè)置方式

    go語言中GOPATH GOROOT的作用和設(shè)置方式

    這篇文章主要介紹了go語言中GOPATH GOROOT的作用和設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • 使用Go語言實現(xiàn)常見hash算法

    使用Go語言實現(xiàn)常見hash算法

    這篇文章主要為大家詳細介紹了使語言實現(xiàn)各種常見hash算法的相關(guān)知識,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的小伙伴可以參考下
    2024-01-01
  • 淺析go語言如何實現(xiàn)協(xié)程的搶占式調(diào)度的

    淺析go語言如何實現(xiàn)協(xié)程的搶占式調(diào)度的

    go語言通過GMP模型實現(xiàn)協(xié)程并發(fā),為了避免單協(xié)程持續(xù)持有線程導(dǎo)致線程隊列中的其他協(xié)程饑餓問題,設(shè)計者提出了一個搶占式調(diào)度機制,本文會基于一個簡單的代碼示例對搶占式調(diào)度過程進行深入講解剖析
    2024-04-04
  • 一文詳解下劃線字段在golang結(jié)構(gòu)體中的應(yīng)用

    一文詳解下劃線字段在golang結(jié)構(gòu)體中的應(yīng)用

    這篇文章主要為大家詳細介紹了下劃線字段在golang結(jié)構(gòu)體中應(yīng)用的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-08-08
  • Golang中Gin框架的使用入門教程

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

    這篇文章主要為大家詳細介紹了Golang中Gin框架的使用教程,文中通過簡單的示例為大家講解了Gin框架的安裝與使用,感興趣的小伙伴開業(yè)跟隨小編一起學(xué)習(xí)一下
    2022-10-10
  • Golang實現(xiàn)自己的Redis(TCP篇)實例探究

    Golang實現(xiàn)自己的Redis(TCP篇)實例探究

    這篇文章主要介紹了Golang實現(xiàn)自己的Redis(TCP篇)實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01

最新評論

万宁市| 闵行区| 嫩江县| 绥江县| 龙井市| 旬阳县| 苗栗县| 鄂托克前旗| 塘沽区| 黔南| 阳城县| 平陆县| 泽州县| 和田县| 渝北区| 雅安市| 常山县| 灵台县| 叶城县| 宣恩县| 静安区| 九龙县| 保德县| 宁都县| 岗巴县| 闻喜县| 菏泽市| 黄陵县| 忻州市| 红安县| 乌鲁木齐市| 青阳县| 宜宾县| 林芝县| 安康市| 河东区| 延寿县| 桂阳县| 南澳县| 山阳县| 离岛区|