Protoc无法创建服务。

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

Protoc doesn't create service

问题

我只会翻译文本内容,不会执行代码。以下是翻译的结果:

我只是尝试使用协议缓冲区生成 Golang 代码,但无法使用 protoc 命令生成服务。

  1. syntax = "proto3";
  2. package greet;
  3. option go_package="./greet/greetpb";
  4. service GreetService{}

上面是我尝试转换为 Golang 的虚拟 proto 文件。但是在我的系统中,它无法将 service GreetService{} 这一行识别为服务。

我在 Windows 系统上使用以下版本的 protoc:

  1. PS D:\Study\grpc\go-grpc-course> protoc --version
  2. libprotoc 3.17.3

使用以下命令生成代码:

  1. protoc .\greet\greetpb\greet.proto --go_out=.

下面是上述命令生成的 Golang 代码:

  1. // Code generated by protoc-gen-go. DO NOT EDIT.
  2. // versions:
  3. // protoc-gen-go v1.26.0
  4. // protoc v3.17.3
  5. // source: greet/greetpb/greet.proto
  6. package greetpb
  7. import (
  8. protoreflect "google.golang.org/protobuf/reflect/protoreflect"
  9. protoimpl "google.golang.org/protobuf/runtime/protoimpl"
  10. reflect "reflect"
  11. )
  12. const (
  13. // Verify that this generated code is sufficiently up-to-date.
  14. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
  15. // Verify that runtime/protoimpl is sufficiently up-to-date.
  16. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
  17. )
  18. var File_greet_greetpb_greet_proto protoreflect.FileDescriptor
  19. var file_greet_greetpb_greet_proto_rawDesc = []byte{
  20. 0x0a, 0x19, 0x67, 0x72, 0x65, 0x65, 0x74, 0x2f, 0x67, 0x72, 0x65, 0x65, 0x74, 0x70, 0x62, 0x2f,
  21. 0x67, 0x72, 0x65, 0x65, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x67, 0x72, 0x65,
  22. 0x65, 0x74, 0x32, 0x0e, 0x0a, 0x0c, 0x47, 0x72, 0x65, 0x65, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69,
  23. 0x63, 0x65, 0x42, 0x11, 0x5a, 0x0f, 0x2e, 0x2f, 0x67, 0x72, 0x65, 0x65, 0x74, 0x2f, 0x67, 0x72,
  24. 0x65, 0x65, 0x74, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
  25. }
  26. var file_greet_greetpb_greet_proto_goTypes = []interface{}{}
  27. var file_greet_greetpb_greet_proto_depIdxs = []int32{
  28. 0, // [0:0] is the sub-list for method output_type
  29. 0, // [0:0] is the sub-list for method input_type
  30. 0, // [0:0] is the sub-list for extension type_name
  31. 0, // [0:0] is the sub-list for extension extendee
  32. 0, // [0:0] is the sub-list for field type_name
  33. }
  34. func init() { file_greet_greetpb_greet_proto_init() }
  35. func file_greet_greetpb_greet_proto_init() {
  36. if File_greet_greetpb_greet_proto != nil {
  37. return
  38. }
  39. type x struct{}
  40. out := protoimpl.TypeBuilder{
  41. File: protoimpl.DescBuilder{
  42. GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
  43. RawDescriptor: file_greet_greetpb_greet_proto_rawDesc,
  44. NumEnums: 0,
  45. NumMessages: 0,
  46. NumExtensions: 0,
  47. NumServices: 1,
  48. },
  49. GoTypes: file_greet_greetpb_greet_proto_goTypes,
  50. DependencyIndexes: file_greet_greetpb_greet_proto_depIdxs,
  51. }.Build()
  52. File_greet_greetpb_greet_proto = out.File
  53. file_greet_greetpb_greet_proto_rawDesc = nil
  54. file_greet_greetpb_greet_proto_goTypes = nil
  55. file_greet_greetpb_greet_proto_depIdxs = nil
  56. }

后面的 Go 代码中没有创建任何服务。我是否做错了什么,还是这是某种 proto 问题?任何帮助将不胜感激。

英文:

I am just trying to generate code for golang using protocol buffer. But unable to generate the service using protoc command.

  1. syntax = "proto3";
  2. package greet;
  3. option go_package="./greet/greetpb";
  4. service GreetService{}

Above is a dummy proto file that I am trying to convert into golang. But unable to generate the correct file as in my system it doesn't recognize service GreetService{}
line as a service.

I am using the following version of protoc on windows system:

  1. PS D:\Study\grpc\go-grpc-course> protoc --version
  2. libprotoc 3.17.3

Using below command to generate the code:

> protoc .\greet\greetpb\greet.proto --go_out=.

Below is the go code generated by above command:

  1. // Code generated by protoc-gen-go. DO NOT EDIT.
  2. // versions:
  3. // protoc-gen-go v1.26.0
  4. // protoc v3.17.3
  5. // source: greet/greetpb/greet.proto
  6. package greetpb
  7. import (
  8. protoreflect "google.golang.org/protobuf/reflect/protoreflect"
  9. protoimpl "google.golang.org/protobuf/runtime/protoimpl"
  10. reflect "reflect"
  11. )
  12. const (
  13. // Verify that this generated code is sufficiently up-to-date.
  14. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
  15. // Verify that runtime/protoimpl is sufficiently up-to-date.
  16. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
  17. )
  18. var File_greet_greetpb_greet_proto protoreflect.FileDescriptor
  19. var file_greet_greetpb_greet_proto_rawDesc = []byte{
  20. 0x0a, 0x19, 0x67, 0x72, 0x65, 0x65, 0x74, 0x2f, 0x67, 0x72, 0x65, 0x65, 0x74, 0x70, 0x62, 0x2f,
  21. 0x67, 0x72, 0x65, 0x65, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x67, 0x72, 0x65,
  22. 0x65, 0x74, 0x32, 0x0e, 0x0a, 0x0c, 0x47, 0x72, 0x65, 0x65, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69,
  23. 0x63, 0x65, 0x42, 0x11, 0x5a, 0x0f, 0x2e, 0x2f, 0x67, 0x72, 0x65, 0x65, 0x74, 0x2f, 0x67, 0x72,
  24. 0x65, 0x65, 0x74, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
  25. }
  26. var file_greet_greetpb_greet_proto_goTypes = []interface{}{}
  27. var file_greet_greetpb_greet_proto_depIdxs = []int32{
  28. 0, // [0:0] is the sub-list for method output_type
  29. 0, // [0:0] is the sub-list for method input_type
  30. 0, // [0:0] is the sub-list for extension type_name
  31. 0, // [0:0] is the sub-list for extension extendee
  32. 0, // [0:0] is the sub-list for field type_name
  33. }
  34. func init() { file_greet_greetpb_greet_proto_init() }
  35. func file_greet_greetpb_greet_proto_init() {
  36. if File_greet_greetpb_greet_proto != nil {
  37. return
  38. }
  39. type x struct{}
  40. out := protoimpl.TypeBuilder{
  41. File: protoimpl.DescBuilder{
  42. GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
  43. RawDescriptor: file_greet_greetpb_greet_proto_rawDesc,
  44. NumEnums: 0,
  45. NumMessages: 0,
  46. NumExtensions: 0,
  47. NumServices: 1,
  48. },
  49. GoTypes: file_greet_greetpb_greet_proto_goTypes,
  50. DependencyIndexes: file_greet_greetpb_greet_proto_depIdxs,
  51. }.Build()
  52. File_greet_greetpb_greet_proto = out.File
  53. file_greet_greetpb_greet_proto_rawDesc = nil
  54. file_greet_greetpb_greet_proto_goTypes = nil
  55. file_greet_greetpb_greet_proto_depIdxs = nil
  56. }

There aren't any services created in the later go code. Am I doing something wrong or is this some proto issue. Any help is appreciated.

答案1

得分: 6

你似乎在使用protoc时忘记添加go-grpc_out参数了。这个参数用于生成gRPC代码。请尝试以下命令:

  1. protoc greet.proto --go_out=. --go-grpc_out=.

这也在gRPC快速入门指南的"重新生成gRPC代码"部分中有描述。

英文:

You seem to forgot to add the go-grpc_out argument when using protoc. This argument is for generating the gRPC code. Please try the following:

  1. protoc greet.proto --go_out=. --go-grpc_out=.

This is also described in the gRPC quickstart - section "Regenerate gRPC code"

huangapple
  • 本文由 发表于 2021年6月22日 13:42:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/68078115.html
匿名

发表评论

匿名网友

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

确定