3 回答

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊
那是相當(dāng)基本的。
UnimplementedGreetServiceServer
是具有所有已實(shí)現(xiàn)方法的結(jié)構(gòu)。
當(dāng)我添加pdfpb.UnimplementedGreetServiceServer
時(shí),我可以調(diào)用UnimplementedGreetServiceServer
定義的方法。
就是這樣,如果我在 proto 文件中添加更多 RPC 服務(wù),那么我不需要添加所有導(dǎo)致向前兼容的 RPC 方法。
演示代碼位于:https ://github.com/parthw/fun-coding/tree/main/golang/understanding-grpc-change

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

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