我试图实现一个来自互联网的示例,用于与 go 进行 gRPC 集成。 https://dzone.com/articles/writing-a-microservice-in-golang-which-communicate
因此,在生成的文件中,没有 RegisterServer 方法,也没有服务内部方法的处理程序。
我的repository-service.proto文件
package service;
option go_package = "grpc_test/internal/gRPC/service";
import "grpc_test/internal/proto-files/domain/repository.proto";
//RepositoryService Definition
service RepositoryService {
rpc add (domain.Repository) returns (AddRepositoryResponse);
}
message AddRepositoryResponse {
domain.Repository addedRepository = 1;
Error error = 2;
}
message Error {
string code = 1;
string message = 2;
}
生成的.pb.go文件
// source: grpc_test/internal/proto-files/service/repository-service.proto
package service
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
domain "grpc_test/internal/gRPC/domain"
math "math"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
type AddRepositoryResponse struct {
AddedRepository *domain.Repository `protobuf:"bytes,1,opt,name=addedRepository,proto3" json:"addedRepository,omitempty"`
Error *Error `protobuf:"bytes,2,opt,name=error,proto3" json:"error,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *AddRepositoryResponse) Reset() { *m = AddRepositoryResponse{} }
func (m *AddRepositoryResponse) String() string { return proto.CompactTextString(m) }
func (*AddRepositoryResponse) ProtoMessage() {}
func (*AddRepositoryResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_621a2418b03aa6f6, []int{0}
}
func (m *AddRepositoryResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_AddRepositoryResponse.Unmarshal(m, b)
}
func (m *AddRepositoryResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_AddRepositoryResponse.Marshal(b, m, deterministic)
}
func (m *AddRepositoryResponse) XXX_Merge(src proto.Message) {
xxx_messageInfo_AddRepositoryResponse.Merge(m, src)
}
func (m *AddRepositoryResponse) XXX_Size() int {
return xxx_messageInfo_AddRepositoryResponse.Size(m)
}
func (m *AddRepositoryResponse) XXX_DiscardUnknown() {
xxx_messageInfo_AddRepositoryResponse.DiscardUnknown(m)
}
var xxx_messageInfo_AddRepositoryResponse proto.InternalMessageInfo
func (m *AddRepositoryResponse) GetAddedRepository() *domain.Repository {
if m != nil {
return m.AddedRepository
}
return nil
}
func (m *AddRepositoryResponse) GetError() *Error {
if m != nil {
return m.Error
}
return nil
}
type Error struct {
Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"`
Message string `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *Error) Reset() { *m = Error{} }
func (m *Error) String() string { return proto.CompactTextString(m) }
func (*Error) ProtoMessage() {}
func (*Error) Descriptor() ([]byte, []int) {
return fileDescriptor_621a2418b03aa6f6, []int{1}
}
func (m *Error) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Error.Unmarshal(m, b)
}
func (m *Error) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Error.Marshal(b, m, deterministic)
}
func (m *Error) XXX_Merge(src proto.Message) {
xxx_messageInfo_Error.Merge(m, src)
}
func (m *Error) XXX_Size() int {
return xxx_messageInfo_Error.Size(m)
}
func (m *Error) XXX_DiscardUnknown() {
xxx_messageInfo_Error.DiscardUnknown(m)
}
var xxx_messageInfo_Error proto.InternalMessageInfo
func (m *Error) GetCode() string {
if m != nil {
return m.Code
}
return ""
}
func (m *Error) GetMessage() string {
if m != nil {
return m.Message
}
return ""
}
func init() {
proto.RegisterType((*AddRepositoryResponse)(nil), "service.AddRepositoryResponse")
proto.RegisterType((*Error)(nil), "service.Error")
}
func init() {
proto.RegisterFile("grpc_test/internal/proto-files/service/repository-service.proto", fileDescriptor_621a2418b03aa6f6)
}
var fileDescriptor_621a2418b03aa6f6 = []byte{
// 248 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xb2, 0x4f, 0x2f, 0x2a, 0x48,
0x8e, 0x2f, 0x49, 0x2d, 0x2e, 0xd1, 0xcf, 0xcc, 0x2b, 0x49, 0x2d, 0xca, 0x4b, 0xcc, 0xd1, 0x2f,
0x28, 0xca, 0x2f, 0xc9, 0xd7, 0x4d, 0xcb, 0xcc, 0x49, 0x2d, 0xd6, 0x2f, 0x4e, 0x2d, 0x2a, 0xcb,
0x4c, 0x4e, 0xd5, 0x2f, 0x4a, 0x2d, 0xc8, 0x2f, 0xce, 0x2c, 0xc9, 0x2f, 0xaa, 0xd4, 0x85, 0x0a,
0xe9, 0x81, 0x95, 0x09, 0xb1, 0x43, 0xb9, 0x52, 0x66, 0x04, 0x4c, 0x4a, 0xc9, 0xcf, 0x4d, 0xcc,
0xcc, 0x43, 0x32, 0x08, 0x62, 0x80, 0x52, 0x35, 0x97, 0xa8, 0x63, 0x4a, 0x4a, 0x10, 0x5c, 0x38,
0x28, 0xb5, 0xb8, 0x20, 0x3f, 0xaf, 0x38, 0x55, 0xc8, 0x86, 0x8b, 0x3f, 0x31, 0x25, 0x25, 0x15,
0x49, 0x4a, 0x82, 0x51, 0x81, 0x51, 0x83, 0xdb, 0x48, 0x48, 0x0f, 0x62, 0x96, 0x1e, 0x92, 0x26,
0x74, 0xa5, 0x42, 0x2a, 0x5c, 0xac, 0xa9, 0x45, 0x45, 0xf9, 0x45, 0x12, 0x4c, 0x60, 0x3d, 0x7c,
0x7a, 0x30, 0x67, 0xbb, 0x82, 0x44, 0x83, 0x20, 0x92, 0x4a, 0xa6, 0x5c, 0xac, 0x60, 0xbe, 0x90,
0x10, 0x17, 0x4b, 0x72, 0x7e, 0x4a, 0x2a, 0xd8, 0x06, 0xce, 0x20, 0x30, 0x5b, 0x48, 0x82, 0x8b,
0x3d, 0x37, 0xb5, 0xb8, 0x38, 0x31, 0x3d, 0x15, 0x6c, 0x08, 0x67, 0x10, 0x8c, 0x6b, 0xe4, 0xc7,
0x25, 0x88, 0xb0, 0x2a, 0x18, 0x62, 0xb0, 0x90, 0x25, 0x17, 0x73, 0x62, 0x4a, 0x8a, 0x10, 0x16,
0xd7, 0x49, 0xc9, 0xc1, 0x6d, 0xc7, 0xea, 0x55, 0x27, 0xc5, 0x28, 0x79, 0x2c, 0xa1, 0x97, 0x1e,
0x14, 0xe0, 0x0c, 0x8b, 0x80, 0x24, 0x36, 0x70, 0x68, 0x19, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff,
0x27, 0x43, 0x03, 0x2a, 0xb1, 0x01, 0x00, 0x00,
}
理想情况下它应该有“RegisterRepositoryServiceServer”方法,但它没有生成,似乎我在安装过程中错过了一些东西。
生成 stub 的命令是
protoc -I $GOPATH/src --go_out=plugins=gRPC:$GOPATH/src $GOPATH/src/.proto 文件的路径
任何帮助将不胜感激。谢谢
最佳答案
该问题是由于在 protoc
命令行中使用 gRPC
而不是 grpc
引起的。尝试使用:
$ protoc -I$GOPATH/src --go_out=plugins=grpc:$GOPATH/src PATH_TO_PROTO_FILE
关于go - RegisterService 和处理程序方法未生成 gRPC 和 go,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58953006/