dongnue4923 2015-11-03 18:12
浏览 372
已采纳

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

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{},
}
  • 写回答

3条回答 默认 最新

  • dongtiao0279 2015-11-03 21:36
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误