如何修复grpc方法处理程序的问题

huangapple go评论105阅读模式
英文:

How to fix this issue with grpc method handler

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我对golang还不熟悉,想通过实验grpc代码来更好地理解它。为了这样做,我按照这里显示的示例进行操作:

https://devicharan.wordpress.com/

源代码在这里:
https://github.com/devicharan/basicwebapp

不幸的是,当我运行这段代码并进行go build时,出现了以下错误消息:

# basicwebapp/proto
proto/CatalogService.pb.go:126: cannot use _CatalogService_GetProductCatalog_Handler (type func(interface {},   context.Context, []byte) (proto.Message, error)) as type grpc.methodHandler in field value
proto/RecommendationService.pb.go:99: cannot use _RecommendationService_GetRecommendations_Handler (type func(interface {}, context.Context, []byte) (proto.Message, error)) as type grpc.methodHandler in field value

我不知道这是什么意思,或者我需要更改什么才能开始找到解决方法。这是代码本身的问题还是我的Go配置有问题?另外,有没有人可以推荐一个好的Go调试器?

这是CatalogService.pb.go的代码:

// Code generated by protoc-gen-go.
// source: CatalogService.proto
// DO NOT EDIT!

/*
Package protos is a generated protocol buffer package.

It is generated from these files:
	CatalogService.proto
	Product.proto
	RecommendationService.proto

It has these top-level messages:
	Category
	CatalogResponse
	CatalogRequest
*/
package protos

import proto "github.com/golang/protobuf/proto"

import (
	context "golang.org/x/net/context"
	grpc "google.golang.org/grpc"
)

// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal

type Category struct {
	CategoryName string `protobuf:"bytes,1,opt,name=categoryName" json:"categoryName,omitempty"`
}

func (m *Category) Reset()         { *m = Category{} }
func (m *Category) String() string { return proto.CompactTextString(m) }
func (*Category) ProtoMessage()    {}

type CatalogResponse struct {
	Products []*Product `protobuf:"bytes,1,rep,name=products" json:"products,omitempty"`
}

func (m *CatalogResponse) Reset()         { *m = CatalogResponse{} }
func (m *CatalogResponse) String() string { return proto.CompactTextString(m) }
func (*CatalogResponse) ProtoMessage()    {}

func (m *CatalogResponse) GetProducts() []*Product {
	if m != nil {
		return m.Products
	}
	return nil
}

type CatalogRequest struct {
	Category *Category `protobuf:"bytes,1,opt,name=category" json:"category,omitempty"`
}

func (m *CatalogRequest) Reset()         { *m = CatalogRequest{} }
func (m *CatalogRequest) String() string { return proto.CompactTextString(m) }
func (*CatalogRequest) ProtoMessage()    {}

func (m *CatalogRequest) GetCategory() *Category {
	if m != nil {
		return m.Category
	}
	return nil
}

func init() {
}

// CatalogService服务的客户端API

type CatalogServiceClient interface {
	GetProductCatalog(ctx context.Context, in *CatalogRequest, opts ...grpc.CallOption) (*CatalogResponse, error)
}

type catalogServiceClient struct {
	cc *grpc.ClientConn
}

func NewCatalogServiceClient(cc *grpc.ClientConn) CatalogServiceClient {
	return &catalogServiceClient{cc}
}

func (c *catalogServiceClient) GetProductCatalog(ctx context.Context, in *CatalogRequest, opts ...grpc.CallOption) (*CatalogResponse, error) {
	out := new(CatalogResponse)
	err := grpc.Invoke(ctx, "/protos.CatalogService/GetProductCatalog", in, out, c.cc, opts...)
	if err != nil {
		return nil, err
	}
	return out, nil
}

// CatalogService服务的服务器API

type CatalogServiceServer interface {
	GetProductCatalog(context.Context, *CatalogRequest) (*CatalogResponse, error)
}

func RegisterCatalogServiceServer(s *grpc.Server, srv CatalogServiceServer) {
	s.RegisterService(&_CatalogService_serviceDesc, srv)
}

func _CatalogService_GetProductCatalog_Handler(srv interface{}, ctx context.Context, buf []byte) (proto.Message, error) {
	in := new(CatalogRequest)
	if err := proto.Unmarshal(buf, in); err != nil {
		return nil, err
	}
	out, err := srv.(CatalogServiceServer).GetProductCatalog(ctx, in)
	if err != nil {
		return nil, err
	}
	return out, nil
}

var _CatalogService_serviceDesc = grpc.ServiceDesc{
	ServiceName: "protos.CatalogService",
	HandlerType: (*CatalogServiceServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "GetProductCatalog",
			Handler:    _CatalogService_GetProductCatalog_Handler,
		},
	},
	Streams: []grpc.StreamDesc{},
}

这是RecommendationService.pg.go的代码:

// Code generated by protoc-gen-go.
// source: RecommendationService.proto
// DO NOT EDIT!

package protos

import proto "github.com/golang/protobuf/proto"

import (
	context "golang.org/x/net/context"
	grpc "google.golang.org/grpc"
)

// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal

type RecommendationResponse struct {
	Result []*RecommendationResponse_Recommendation `protobuf:"bytes,1,rep,name=result" json:"result,omitempty"`
}

func (m *RecommendationResponse) Reset()         { *m = RecommendationResponse{} }
func (m *RecommendationResponse) String() string { return proto.CompactTextString(m) }
func (*RecommendationResponse) ProtoMessage()    {}

func (m *RecommendationResponse) GetResult() []*RecommendationResponse_Recommendation {
	if m != nil {
		return m.Result
	}
	return nil
}

type RecommendationResponse_Recommendation struct {
	Rating    int32 `protobuf:"varint,1,opt,name=rating" json:"rating,omitempty"`
	Productid int32 `protobuf:"varint,2,opt,name=productid" json:"productid,omitempty"`
}

func (m *RecommendationResponse_Recommendation) Reset()         { *m = RecommendationResponse_Recommendation{} }
func (m *RecommendationResponse_Recommendation) String() string { return proto.CompactTextString(m) }
func (*RecommendationResponse_Recommendation) ProtoMessage()    {}

func init() {
}

// RecommendationService服务的客户端API

type RecommendationServiceClient interface {
	GetRecommendations(ctx context.Context, in *Product, opts ...grpc.CallOption) (*RecommendationResponse, error)
}

type recommendationServiceClient struct {
	cc *grpc.ClientConn
}

func NewRecommendationServiceClient(cc *grpc.ClientConn) RecommendationServiceClient {
	return &recommendationServiceClient{cc}
}

func (c *recommendationServiceClient) GetRecommendations(ctx context.Context, in *Product, opts ...grpc.CallOption) (*RecommendationResponse, error) {
	out := new(RecommendationResponse)
	err := grpc.Invoke(ctx, "/protos.RecommendationService/GetRecommendations", in, out, c.cc, opts...)
	if err != nil {
		return nil, err
	}
	return out, nil
}

// RecommendationService服务的服务器API

type RecommendationServiceServer interface {
	GetRecommendations(context.Context, *Product) (*RecommendationResponse, error)
}

func RegisterRecommendationServiceServer(s *grpc.Server, srv RecommendationServiceServer) {
	s.RegisterService(&_RecommendationService_serviceDesc, srv)
}

func _RecommendationService_GetRecommendations_Handler(srv interface{}, ctx context.Context, buf []byte) (proto.Message, error) {
	in := new(Product)
	if err := proto.Unmarshal(buf, in); err != nil {
		return nil, err
	}
	out, err := srv.(RecommendationServiceServer).GetRecommendations(ctx, in)
	if err != nil {
		return nil, err
	}
	return out, nil
}

var _RecommendationService_serviceDesc = grpc.ServiceDesc{
	ServiceName: "protos.RecommendationService",
	HandlerType: (*RecommendationServiceServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "GetRecommendations",
			Handler:    _RecommendationService_GetRecommendations_Handler,
		},
	},
	Streams: []grpc.StreamDesc{},
}
英文:

I'm new to golang and wanted to experiment with grpc code to get a better understanding of it. In order to do so I followed the example shown here:

https://devicharan.wordpress.com/

The source code is here:
https://github.com/devicharan/basicwebapp

Unfortunately, when I run this code and do a go build I get an error message with the following:

# basicwebapp/proto
proto/CatalogService.pb.go:126: cannot use _CatalogService_GetProductCatalog_Handler (type func(interface {},   context.Context, []byte) (proto.Message, error)) as type grpc.methodHandler in field value
proto/RecommendationService.pb.go:99: cannot use _RecommendationService_GetRecommendations_Handler (type func(interface {}, context.Context, []byte) (proto.Message, error)) as type grpc.methodHandler in field value

I don't know what this means or what I need to change in order to begin finding a fix. Is it a problem with the code itself or with my Go configuration? Also, is there a good debugger for Go that someone can recommend?

Here is the code for CatalogService.pb.go:

// Code generated by protoc-gen-go.
// source: CatalogService.proto
// DO NOT EDIT!

/*
Package protos is a generated protocol buffer package.

It is generated from these files:
	CatalogService.proto
	Product.proto
	RecommendationService.proto

It has these top-level messages:
	Category
	CatalogResponse
	CatalogRequest
*/
package protos

import proto "github.com/golang/protobuf/proto"

import (
	context "golang.org/x/net/context"
	grpc "google.golang.org/grpc"
)

// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal

type Category struct {
	CategoryName string `protobuf:"bytes,1,opt,name=categoryName" json:"categoryName,omitempty"`
}

func (m *Category) Reset()         { *m = Category{} }
func (m *Category) String() string { return proto.CompactTextString(m) }
func (*Category) ProtoMessage()    {}

type CatalogResponse struct {
	Products []*Product `protobuf:"bytes,1,rep,name=products" json:"products,omitempty"`
}

func (m *CatalogResponse) Reset()         { *m = CatalogResponse{} }
func (m *CatalogResponse) String() string { return proto.CompactTextString(m) }
func (*CatalogResponse) ProtoMessage()    {}

func (m *CatalogResponse) GetProducts() []*Product {
	if m != nil {
		return m.Products
	}
	return nil
}

type CatalogRequest struct {
	Category *Category `protobuf:"bytes,1,opt,name=category" json:"category,omitempty"`
}

func (m *CatalogRequest) Reset()         { *m = CatalogRequest{} }
func (m *CatalogRequest) String() string { return proto.CompactTextString(m) }
func (*CatalogRequest) ProtoMessage()    {}

func (m *CatalogRequest) GetCategory() *Category {
	if m != nil {
		return m.Category
	}
	return nil
}

func init() {
}

// Client API for CatalogService service

type CatalogServiceClient interface {
	GetProductCatalog(ctx context.Context, in *CatalogRequest, opts ...grpc.CallOption) (*CatalogResponse, error)
}

type catalogServiceClient struct {
	cc *grpc.ClientConn
}

func NewCatalogServiceClient(cc *grpc.ClientConn) CatalogServiceClient {
	return &catalogServiceClient{cc}
}

func (c *catalogServiceClient) GetProductCatalog(ctx context.Context, in *CatalogRequest, opts ...grpc.CallOption) (*CatalogResponse, error) {
	out := new(CatalogResponse)
	err := grpc.Invoke(ctx, "/protos.CatalogService/GetProductCatalog", in, out, c.cc, opts...)
	if err != nil {
		return nil, err
	}
	return out, nil
}

// Server API for CatalogService service

type CatalogServiceServer interface {
	GetProductCatalog(context.Context, *CatalogRequest) (*CatalogResponse, error)
}

func RegisterCatalogServiceServer(s *grpc.Server, srv CatalogServiceServer) {
	s.RegisterService(&_CatalogService_serviceDesc, srv)
}

func _CatalogService_GetProductCatalog_Handler(srv interface{}, ctx context.Context, buf []byte) (proto.Message, error) {
	in := new(CatalogRequest)
	if err := proto.Unmarshal(buf, in); err != nil {
		return nil, err
	}
	out, err := srv.(CatalogServiceServer).GetProductCatalog(ctx, in)
	if err != nil {
		return nil, err
	}
	return out, nil
}

var _CatalogService_serviceDesc = grpc.ServiceDesc{
	ServiceName: "protos.CatalogService",
	HandlerType: (*CatalogServiceServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "GetProductCatalog",
			Handler:    _CatalogService_GetProductCatalog_Handler,
		},
	},
	Streams: []grpc.StreamDesc{},
}

And this is RecommendationService.pg.go

// Code generated by protoc-gen-go.
// source: RecommendationService.proto
// DO NOT EDIT!

package protos

import proto "github.com/golang/protobuf/proto"

import (
	context "golang.org/x/net/context"
	grpc "google.golang.org/grpc"
)

// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal

type RecommendationResponse struct {
	Result []*RecommendationResponse_Recommendation `protobuf:"bytes,1,rep,name=result" json:"result,omitempty"`
}

func (m *RecommendationResponse) Reset()         { *m = RecommendationResponse{} }
func (m *RecommendationResponse) String() string { return proto.CompactTextString(m) }
func (*RecommendationResponse) ProtoMessage()    {}

func (m *RecommendationResponse) GetResult() []*RecommendationResponse_Recommendation {
	if m != nil {
		return m.Result
	}
	return nil
}

type RecommendationResponse_Recommendation struct {
	Rating    int32 `protobuf:"varint,1,opt,name=rating" json:"rating,omitempty"`
	Productid int32 `protobuf:"varint,2,opt,name=productid" json:"productid,omitempty"`
}

func (m *RecommendationResponse_Recommendation) Reset()         { *m = RecommendationResponse_Recommendation{} }
func (m *RecommendationResponse_Recommendation) String() string { return proto.CompactTextString(m) }
func (*RecommendationResponse_Recommendation) ProtoMessage()    {}

func init() {
}

// Client API for RecommendationService service

type RecommendationServiceClient interface {
	GetRecommendations(ctx context.Context, in *Product, opts ...grpc.CallOption) (*RecommendationResponse, error)
}

type recommendationServiceClient struct {
	cc *grpc.ClientConn
}

func NewRecommendationServiceClient(cc *grpc.ClientConn) RecommendationServiceClient {
	return &recommendationServiceClient{cc}
}

func (c *recommendationServiceClient) GetRecommendations(ctx context.Context, in *Product, opts ...grpc.CallOption) (*RecommendationResponse, error) {
	out := new(RecommendationResponse)
	err := grpc.Invoke(ctx, "/protos.RecommendationService/GetRecommendations", in, out, c.cc, opts...)
	if err != nil {
		return nil, err
	}
	return out, nil
}

// Server API for RecommendationService service

type RecommendationServiceServer interface {
	GetRecommendations(context.Context, *Product) (*RecommendationResponse, error)
}

func RegisterRecommendationServiceServer(s *grpc.Server, srv RecommendationServiceServer) {
	s.RegisterService(&_RecommendationService_serviceDesc, srv)
}

func _RecommendationService_GetRecommendations_Handler(srv interface{}, ctx context.Context, buf []byte) (proto.Message, error) {
	in := new(Product)
	if err := proto.Unmarshal(buf, in); err != nil {
		return nil, err
	}
	out, err := srv.(RecommendationServiceServer).GetRecommendations(ctx, in)
	if err != nil {
		return nil, err
	}
	return out, nil
}

var _RecommendationService_serviceDesc = grpc.ServiceDesc{
	ServiceName: "protos.RecommendationService",
	HandlerType: (*RecommendationServiceServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "GetRecommendations",
			Handler:    _RecommendationService_GetRecommendations_Handler,
		},
	},
	Streams: []grpc.StreamDesc{},
}

答案1

得分: 9

基本上,你的 protoc-gen-go 不匹配 grpc 的版本。因此,将它们都同步到最新版本并重新安装 protoc-gen-go 将解决这个问题:

go get -u github.com/golang/protobuf/
cd github.com/golang/protobuf/
make

go get -u github.com/grpc/grpc-go
英文:

Basically your protoc-gen-go doesn't match the version of grpc. So sync them both to the latest version and reinstall protoc-gen-go will resolve the issue:

go get -u github.com/golang/protobuf/
cd github.com/golang/protobuf/
make

go get -u github.com/grpc/grpc-go

答案2

得分: 2

对于遇到相同问题的人,我所做的是简单地改变了构建proto文件的方式。博客页面上有一条评论强调了一些遗漏的步骤,我按照评论中的指示执行了以下protoc命令来从proto文件生成代码:

protoc --go_out=plugins=grpc:. *.proto

我在包含proto文件的目录中运行了这个命令,然后对main.go文件进行了go build,现在一切都正常工作了。

英文:

For anyone running into the same problem, what I did was simply change how I built the proto files. There is a comment on the blog page that highlights some missing steps, I followed it and did the following protoc command to generate code from the proto files:

protoc --go_out=plugins=grpc:. *.proto

I ran this command in the directory with my proto files and then did a go build on my main.go file, now everything is working fine.

答案3

得分: 0

我遇到了同样的问题,重新安装或者使用新的命令似乎没有起作用。

然而,如果你将处理程序定义中的

codec grpc.Codec, buf []byte

改为:

dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor

并且将

codec.Unmarshal(buf, in)

改为

dec(in)

(都在你的 .pb.go 文件的处理程序定义中)

似乎可以解决问题。

当然,修改这个文件并不是最佳选择,每次重新编译都会覆盖你的更改,但至少在我弄清楚我搞砸了什么之前,这是一个解决方法。

英文:

I am having the same issue and reinstalling or the new command doesn't seem to work.

However: If you change

codec grpc.Codec, buf []byte

in the Handler Definition into:

dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor

and

codec.Unmarshal(buf, in) 

into

dec(in)

(All in the Handler Definition in your .pb.go file)

it seems to work.

Changing this file is of course not optimal and every time you recompile it will override your changes, but at least there is a workaround until I figure out what I messed up.

huangapple
  • 本文由 发表于 2015年11月4日 02:12:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/33506221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定