go - 如何使用 grpc 方法处理程序解决此问题

标签 go rpc grpc

我是 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() {
}

// 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{},
}

这是 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{},
}

最佳答案

基本上你的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

关于go - 如何使用 grpc 方法处理程序解决此问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33506221/

相关文章:

arrays - 在 Go 中,如何将 [16]float32 转换为 *float32

javascript - NodeJS gRPC : "Method handler expected but not provided"

ruby - 如何在 gRPC ruby​​ 客户端中传递哈希对象

ssl - 带有自签名证书的 GRPC ssl

go - 解析包含名称的完整电子邮件地址

go - 连接器 : Error connecting to a physical device

go - 使用 golang 进行 NTP 检测,有效负载为空

java - 有多少 GWT 服务

wordpress - 在 WP 中向自定义帖子类型 slug 添加斜杠

GWT RPC 响应头