当前位置:网站首页>Protobuf Grpc使用异常 类型有未导出的方法,并且是在不同的软件包中定义
Protobuf Grpc使用异常 类型有未导出的方法,并且是在不同的软件包中定义
2022-08-03 18:55:00 【chutianxia】
Grpc使用异常
类型有未导出的方法,并且是在不同的软件包中定义
导致原因:
导出的pb.go 文件中 GretterServer interface中有私有变量导致的。
// GreeterServer is the server API for Greeter service. // All implementations must embed UnimplementedGreeterServer // for forward compatibility type GreeterServer interface { SayHello(context.Context, *HelloRequest) (*HelloReply, error) mustEmbedUnimplementedGreeterServer() }
proto文件内容:
syntax = "proto3"; package helloworld; option go_package = "./helloworld"; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) { } } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }
异常提示:
无法将 ‘&GreeterServer{}’ (类型 *GreeterServer) 用作类型 GreeterServer 类型无法实现 ‘GreeterServer’,因为类型有未导出的方法,并且是在不同的软件包中定义
运行异常:
.\server.go:33:35: cannot use &GreeterServer{} (value of type *GreeterServer) as type helloworld.GreeterServer in argument to pb.RegisterGreeterServer:
*GreeterServer does not implement helloworld.GreeterServer (missing mustEmbedUnimplementedGreeterServer method)protobuf 和 grpc 版本:
google.golang.org/grpc v1.48.0
google.golang.org/protobuf v1.28.0修复方式: 增加第15行代码内容:*pb.UnimplementedXxxxxServer Xxxxx为proto文件中定义的service名称
package main import ( pb "GoRpc/proto/helloworld" "context" "flag" "google.golang.org/grpc" "net" ) var port string type GreeterServer struct { // 增加此行代码 *pb.UnimplementedGreeterServer } func init() { flag.StringVar(&port, "p", "8001", "port to connect to") flag.Parse() } func (s *GreeterServer) SayHello(ctx context.Context, r *pb.HelloRequest) (*pb.HelloReply, error) { return &pb.HelloReply{ Message: "Hello.world"}, nil } func main() { server := grpc.NewServer() pb.RegisterGreeterServer(server, &GreeterServer{ }) lis, _ := net.Listen("tcp", ":"+port) server.Serve(lis) }
修复问题的原因,查看helloworld_grpc.pb.go 文件。 实际SayHello由UnimplementedGreeterServer 结构实现
// GreeterServer is the server API for Greeter service. // All implementations must embed UnimplementedGreeterServer // for forward compatibility type GreeterServer interface { SayHello(context.Context, *HelloRequest) (*HelloReply, error) mustEmbedUnimplementedGreeterServer() } // UnimplementedGreeterServer must be embedded to have forward compatible implementations. type UnimplementedGreeterServer struct { } func (UnimplementedGreeterServer) SayHello(context.Context, *HelloRequest) (*HelloReply, error) { return nil, status.Errorf(codes.Unimplemented, "method SayHello not implemented") }
边栏推荐
猜你喜欢
随机推荐
读取 resources 目录下的文件路径的九种方式,你知道多少?
POJ 3041 Asteroids(最大匹配数=最小点覆盖)
MySQL超详细安装教程 手把手教你安装MySQL到使用MySQL 最简单的MySQL安装方式,这种方式装,卸载也简单
爬虫之selenium
SQL代码需要供其他人复用,为什么传统的复制代码不可靠?
实时渲染器不止lumion,Chaos Vantage你值得一试
货比四家 version tb1.63
G6尝试 学习
实现博客营销有哪些技巧
高等数学---第十章无穷级数---常数项级数
Shell编程案例
EasyNTS上云网关断电重启后设备离线是什么原因?
MySQL【变量、流程控制与游标】
CC2530_ZigBee+华为云IOT:设计一套属于自己的冷链采集系统
87.(cesium之家)cesium热力图(贴地形)
阿里二面:多线程间的通信方式有几种?举例说明
丙二醇二乙酸酯(Propylene Glycol Diacetate)
PHP Basic Notes-NO.2
PHP基础笔记-NO.1
MPLS的简单应用于实验