我是 GRPC 新手,無法解決一個問題。當應用程式已經運行時是否可以建立 protobuff 檔案?
例如,我從使用者收到一個 json
,如下所示:
"protobuf_file": "protobuf.proto", // File will be also recieved from user "service_name": "Unary", "method_name": "GetServerResponse", "request_data_serializer_name": "MessageRequest", "body": "grpc_request_data.json", // File will be also recieved from user
這裡我有一個 .proto
檔案、服務名稱、方法和訊息,以及另一個有資料的 json 來填入訊息。
現在我必須打開連接並使用提供的數據調用所需的方法。
TY!
附註.proto
檔案(來自取得說明指南)將會是:
syntax = "proto3"; package protobuf_all_modes; service Unary { rpc GetServerResponse(MessageRequest) returns (MessageResponse) {} } message MessageRequest { string message = 1; } message MessageResponse { string message = 1; int32 random_int32 = 2; }
第二個 json
將會是:
{ "message": "hello World!" }
我不知道要尋找解決方案。如有任何建議,將不勝感激
正確答案
如果有人遇到同樣的問題,有一個非常好的庫- https:// pkg.go.dev/github.com/jhump/[email protected]/dynamic 和一個子包https://pkg.go.dev/github。 com/jhump/[電子郵件受保護]/dynamic/grpcdynamic
程式碼片段將是:
parser
func NewGrpcObject(operation *BaseOperation) *GrpcObject { fns, err := protoparse.ResolveFilenames([]string{"./"}, operation.ProtoFile) // prase .proto file if err != nil { log.Error(err) } parser := protoparse.Parser{} fds, err := parser.ParseFiles(fns...) if err != nil { log.Error(err) } descriptor := fds[0] // In my case there will be only one .proto pkg := descriptor.GetPackage() serviceDescriptor := descriptor.FindService(pkg + "." + operation.ServiceName) // name of service descriptor will be with package name first methodDescriptor := serviceDescriptor.FindMethodByName(operation.MethodName) requestDescriptor := methodDescriptor.GetInputType() // You can get types for request and response responseDescriptor := methodDescriptor.GetOutputType() return &GrpcObject{ RequestDesc: requestDescriptor, MethodDesc: methodDescriptor, ResponseDesc: responseDescriptor, } }
caller
// connect conn, _ := grpc.Dial(operation.Host, grpc.WithTransportCredentials(credentials.NewTLS(c.TLSClientConfig.NewTLSConfig()))) stub = grpcdynamic.NewStub(conn) // call message := dynamic.NewMessage(operation.GrpcObject.RequestDesc) // from parser message.UnmarshalJSON(operation.Body) // here a JSON to fill message resp, err := stub.InvokeRpc(ctx, operation.GrpcObject.MethodDesc, message) if err != nil { // handle err } respMessage := dynamic.NewMessage(operation.GrpcObject.ResponseDesc) // descriptor from parser respMessage.ConvertFrom(resp) // convert message from raw response
以上是GO 和 GRPC:「在飛行中」創建 protobuff 類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Go語言的核心特性包括垃圾回收、靜態鏈接和並發支持。 1.Go語言的並發模型通過goroutine和channel實現高效並發編程。 2.接口和多態性通過實現接口方法,使得不同類型可以統一處理。 3.基本用法展示了函數定義和調用的高效性。 4.高級用法中,切片提供了動態調整大小的強大功能。 5.常見錯誤如競態條件可以通過gotest-race檢測並解決。 6.性能優化通過sync.Pool重用對象,減少垃圾回收壓力。

Go語言在構建高效且可擴展的系統中表現出色,其優勢包括:1.高性能:編譯成機器碼,運行速度快;2.並發編程:通過goroutines和channels簡化多任務處理;3.簡潔性:語法簡潔,降低學習和維護成本;4.跨平台:支持跨平台編譯,方便部署。

關於SQL查詢結果排序的疑惑學習SQL的過程中,常常會遇到一些令人困惑的問題。最近,筆者在閱讀《MICK-SQL基礎�...

golang ...

Go語言中如何對比並處理三個結構體在Go語言編程中,有時需要對比兩個結構體的差異,並將這些差異應用到第�...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Dreamweaver CS6
視覺化網頁開發工具