3 回答

TA貢獻1784條經驗 獲得超8個贊
那是相當基本的。
UnimplementedGreetServiceServer
是具有所有已實現方法的結構。
當我添加pdfpb.UnimplementedGreetServiceServer
時,我可以調用UnimplementedGreetServiceServer
定義的方法。
就是這樣,如果我在 proto 文件中添加更多 RPC 服務,那么我不需要添加所有導致向前兼容的 RPC 方法。
演示代碼位于:https ://github.com/parthw/fun-coding/tree/main/golang/understanding-grpc-change

TA貢獻1851條經驗 獲得超4個贊
此錯誤來自較新版本的protoc-gen-grpc-go編譯器。服務器實現現在必須是前向兼容的。
在此更改之前,每當您注冊服務器實現時,您都會執行以下操作:
pb.RegisterFooBarServiceServer(
server,
&FooBarServer{}, // or whatever you use to construct the server impl
)
如果您的服務器缺少一些方法實現,這將導致編譯時錯誤。
使用較新的 proto 編譯器版本,前向兼容性變為 opt-out,這意味著兩件事:
您現在必須 embed UnimplementedFooBarServiceServer,如錯誤消息所示。正如我所說,當您沒有顯式實現新方法時,這不會產生編譯器錯誤(這就是前向兼容性的含義)。codes.Unimplemented盡管如果您嘗試調用您沒有(或忘記)顯式實現的 RPC,它將導致運行時錯誤。
您仍然可以通過嵌入UnsafeFooBarServiceServer(帶Unsafe前綴)來選擇退出前向兼容性。此接口僅聲明mustEmbedUnimplementedFooBarServiceServer()使問題中的錯誤消失的方法,而不會放棄編譯器錯誤,以防您沒有顯式實現新的處理程序。
例如:
// Implements the grpc FooBarServiceServer
type FooBarService struct {
grpc.UnsafeFooBarServiceServer // consciously opt-out of forward compatibility
// other fields
}
您還可以通過在protoc-gen-grpc-go插件(source)上設置選項來生成沒有前向兼容性的代碼:
protoc --go-grpc_out=require_unimplemented_servers=false:.
注意:.after--go-grpc_out選項用于設置路徑元素。

TA貢獻1845條經驗 獲得超8個贊
對于任何仍然有問題的人,如Github IssuemustEmbededUnimplementedServiceServer中所建議的那樣。最好的解決方案就是更新您的 ServerStruct。
前任。
type AuthenticationServiceServer struct {
}
至。
type AuthenticationServiceServer struct {
service.UnimplementedAuthenticationServiceServer
}
這將解決 Go 在執行此操作時拋出的異常。
grpcService.RegisterAuthenticationServiceServer(grpcServer, controller.AuthenticationServiceServer{})
- 3 回答
- 0 關注
- 1084 瀏覽
添加回答
舉報