dongnue4923 2015-11-03 10: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:

  1. # basicwebapp/proto
  2. 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
  3. 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:

  1. // Code generated by protoc-gen-go.
  2. // source: CatalogService.proto
  3. // DO NOT EDIT!
  4. /*
  5. Package protos is a generated protocol buffer package.
  6. It is generated from these files:
  7. CatalogService.proto
  8. Product.proto
  9. RecommendationService.proto
  10. It has these top-level messages:
  11. Category
  12. CatalogResponse
  13. CatalogRequest
  14. */
  15. package protos
  16. import proto "github.com/golang/protobuf/proto"
  17. import (
  18. context "golang.org/x/net/context"
  19. grpc "google.golang.org/grpc"
  20. )
  21. // Reference imports to suppress errors if they are not otherwise used.
  22. var _ context.Context
  23. var _ grpc.ClientConn
  24. // Reference imports to suppress errors if they are not otherwise used.
  25. var _ = proto.Marshal
  26. type Category struct {
  27. CategoryName string `protobuf:"bytes,1,opt,name=categoryName" json:"categoryName,omitempty"`
  28. }
  29. func (m *Category) Reset() { *m = Category{} }
  30. func (m *Category) String() string { return proto.CompactTextString(m) }
  31. func (*Category) ProtoMessage() {}
  32. type CatalogResponse struct {
  33. Products []*Product `protobuf:"bytes,1,rep,name=products" json:"products,omitempty"`
  34. }
  35. func (m *CatalogResponse) Reset() { *m = CatalogResponse{} }
  36. func (m *CatalogResponse) String() string { return proto.CompactTextString(m) }
  37. func (*CatalogResponse) ProtoMessage() {}
  38. func (m *CatalogResponse) GetProducts() []*Product {
  39. if m != nil {
  40. return m.Products
  41. }
  42. return nil
  43. }
  44. type CatalogRequest struct {
  45. Category *Category `protobuf:"bytes,1,opt,name=category" json:"category,omitempty"`
  46. }
  47. func (m *CatalogRequest) Reset() { *m = CatalogRequest{} }
  48. func (m *CatalogRequest) String() string { return proto.CompactTextString(m) }
  49. func (*CatalogRequest) ProtoMessage() {}
  50. func (m *CatalogRequest) GetCategory() *Category {
  51. if m != nil {
  52. return m.Category
  53. }
  54. return nil
  55. }
  56. func init() {
  57. }
  58. // Client API for CatalogService service
  59. type CatalogServiceClient interface {
  60. GetProductCatalog(ctx context.Context, in *CatalogRequest, opts ...grpc.CallOption) (*CatalogResponse, error)
  61. }
  62. type catalogServiceClient struct {
  63. cc *grpc.ClientConn
  64. }
  65. func NewCatalogServiceClient(cc *grpc.ClientConn) CatalogServiceClient {
  66. return &catalogServiceClient{cc}
  67. }
  68. func (c *catalogServiceClient) GetProductCatalog(ctx context.Context, in *CatalogRequest, opts ...grpc.CallOption) (*CatalogResponse, error) {
  69. out := new(CatalogResponse)
  70. err := grpc.Invoke(ctx, "/protos.CatalogService/GetProductCatalog", in, out, c.cc, opts...)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return out, nil
  75. }
  76. // Server API for CatalogService service
  77. type CatalogServiceServer interface {
  78. GetProductCatalog(context.Context, *CatalogRequest) (*CatalogResponse, error)
  79. }
  80. func RegisterCatalogServiceServer(s *grpc.Server, srv CatalogServiceServer) {
  81. s.RegisterService(&_CatalogService_serviceDesc, srv)
  82. }
  83. func _CatalogService_GetProductCatalog_Handler(srv interface{}, ctx context.Context, buf []byte) (proto.Message, error) {
  84. in := new(CatalogRequest)
  85. if err := proto.Unmarshal(buf, in); err != nil {
  86. return nil, err
  87. }
  88. out, err := srv.(CatalogServiceServer).GetProductCatalog(ctx, in)
  89. if err != nil {
  90. return nil, err
  91. }
  92. return out, nil
  93. }
  94. var _CatalogService_serviceDesc = grpc.ServiceDesc{
  95. ServiceName: "protos.CatalogService",
  96. HandlerType: (*CatalogServiceServer)(nil),
  97. Methods: []grpc.MethodDesc{
  98. {
  99. MethodName: "GetProductCatalog",
  100. Handler: _CatalogService_GetProductCatalog_Handler,
  101. },
  102. },
  103. Streams: []grpc.StreamDesc{},
  104. }

And this is RecommendationService.pg.go

  1. // Code generated by protoc-gen-go.
  2. // source: RecommendationService.proto
  3. // DO NOT EDIT!
  4. package protos
  5. import proto "github.com/golang/protobuf/proto"
  6. import (
  7. context "golang.org/x/net/context"
  8. grpc "google.golang.org/grpc"
  9. )
  10. // Reference imports to suppress errors if they are not otherwise used.
  11. var _ context.Context
  12. var _ grpc.ClientConn
  13. // Reference imports to suppress errors if they are not otherwise used.
  14. var _ = proto.Marshal
  15. type RecommendationResponse struct {
  16. Result []*RecommendationResponse_Recommendation `protobuf:"bytes,1,rep,name=result" json:"result,omitempty"`
  17. }
  18. func (m *RecommendationResponse) Reset() { *m = RecommendationResponse{} }
  19. func (m *RecommendationResponse) String() string { return proto.CompactTextString(m) }
  20. func (*RecommendationResponse) ProtoMessage() {}
  21. func (m *RecommendationResponse) GetResult() []*RecommendationResponse_Recommendation {
  22. if m != nil {
  23. return m.Result
  24. }
  25. return nil
  26. }
  27. type RecommendationResponse_Recommendation struct {
  28. Rating int32 `protobuf:"varint,1,opt,name=rating" json:"rating,omitempty"`
  29. Productid int32 `protobuf:"varint,2,opt,name=productid" json:"productid,omitempty"`
  30. }
  31. func (m *RecommendationResponse_Recommendation) Reset() { *m = RecommendationResponse_Recommendation{} }
  32. func (m *RecommendationResponse_Recommendation) String() string { return proto.CompactTextString(m) }
  33. func (*RecommendationResponse_Recommendation) ProtoMessage() {}
  34. func init() {
  35. }
  36. // Client API for RecommendationService service
  37. type RecommendationServiceClient interface {
  38. GetRecommendations(ctx context.Context, in *Product, opts ...grpc.CallOption) (*RecommendationResponse, error)
  39. }
  40. type recommendationServiceClient struct {
  41. cc *grpc.ClientConn
  42. }
  43. func NewRecommendationServiceClient(cc *grpc.ClientConn) RecommendationServiceClient {
  44. return &recommendationServiceClient{cc}
  45. }
  46. func (c *recommendationServiceClient) GetRecommendations(ctx context.Context, in *Product, opts ...grpc.CallOption) (*RecommendationResponse, error) {
  47. out := new(RecommendationResponse)
  48. err := grpc.Invoke(ctx, "/protos.RecommendationService/GetRecommendations", in, out, c.cc, opts...)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return out, nil
  53. }
  54. // Server API for RecommendationService service
  55. type RecommendationServiceServer interface {
  56. GetRecommendations(context.Context, *Product) (*RecommendationResponse, error)
  57. }
  58. func RegisterRecommendationServiceServer(s *grpc.Server, srv RecommendationServiceServer) {
  59. s.RegisterService(&_RecommendationService_serviceDesc, srv)
  60. }
  61. func _RecommendationService_GetRecommendations_Handler(srv interface{}, ctx context.Context, buf []byte) (proto.Message, error) {
  62. in := new(Product)
  63. if err := proto.Unmarshal(buf, in); err != nil {
  64. return nil, err
  65. }
  66. out, err := srv.(RecommendationServiceServer).GetRecommendations(ctx, in)
  67. if err != nil {
  68. return nil, err
  69. }
  70. return out, nil
  71. }
  72. var _RecommendationService_serviceDesc = grpc.ServiceDesc{
  73. ServiceName: "protos.RecommendationService",
  74. HandlerType: (*RecommendationServiceServer)(nil),
  75. Methods: []grpc.MethodDesc{
  76. {
  77. MethodName: "GetRecommendations",
  78. Handler: _RecommendationService_GetRecommendations_Handler,
  79. },
  80. },
  81. Streams: []grpc.StreamDesc{},
  82. }

展开全部

  • 写回答

3条回答 默认 最新

  • dongtiao0279 2015-11-03 13: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 odoo17在制造模块或采购模块良品与次品如何分流和在质检模块下如何开发
  • ¥15 Qt音乐播放器的音乐文件相对路径怎么写
  • ¥15 VB.NET利用摄像头拍照的程序
  • ¥15 linux下vscode设置不了字连体
  • ¥20 游戏mod是如何制作的
  • ¥15 关于#hadoop#的问题:按照老师上课讲的步骤写的
  • ¥20 有人会用这个工具箱吗 付fei咨询
  • ¥30 成都市武侯区住宅小区兴趣点
  • ¥15 Windows软实时
  • ¥15 自有服务器搭建网络隧道并且负载均衡
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部