1.安装
go get google.golang.org/grpcgo get github.com/golang/protobuf
gogoprotobufgo get github.com/gogo/protobuf/protoc-gen-gofastgo get github.com/gogo/protobuf/protogo get github.com/gogo/protobuf/protoc-gen-gogogo get github.com/gogo/protobuf/gogoproto
- 下载protobuf
- ./configure —prefix=/usr/local/protobuf
- make
- make install
- 配置环境变量
- GOGO_ROOT=${GOPATH}/src/github.com/gogo/protobuf
- PROTOBUF=/usr/local/protobuf
- PATH=$PROTOBUF/bin:$PATH
2.写proto
syntax = "proto3";package containers;import "github.com/gogo/protobuf/gogoproto/gogo.proto";import "google/protobuf/any.proto";import "google/protobuf/empty.proto";import "google/protobuf/timestamp.proto";option go_package = "containers";// Containers provides metadata storage for containers used in the execution// service.service Containers {rpc Get(GetContainerRequest) returns (GetContainerResponse);rpc List(ListContainersRequest) returns (ListContainersResponse);rpc Create(CreateContainerRequest) returns (CreateContainerResponse);rpc Delete(DeleteContainerRequest) returns (google.protobuf.Empty);}message Container {string id = 1;map<string, string> labels = 2;string image = 3;google.protobuf.Any spec = 4;google.protobuf.Timestamp created_at = 5 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];}message GetContainerRequest {string id = 1;}message GetContainerResponse {Container container = 1 [(gogoproto.nullable) = false];}message ListContainersRequest {}message ListContainersResponse {repeated Container containers = 1 [(gogoproto.nullable) = false];}message CreateContainerRequest {Container container = 1 [(gogoproto.nullable) = false];}message CreateContainerResponse {Container container = 1 [(gogoproto.nullable) = false];}message DeleteContainerRequest {string id = 1;}message ListContainerMessage {Container container = 1;}
3.codegen
protoc -I.:${GOPATH}/src --gofast_out=plugins=grpc:. api/services/containers/containers.proto
type Container struct {Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`Labels map[string]string `protobuf:"bytes,2,rep,name=labels,proto3" json:"labels,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`Image string `protobuf:"bytes,3,opt,name=image,proto3" json:"image,omitempty"`Spec *any.Any `protobuf:"bytes,4,opt,name=spec,proto3" json:"spec,omitempty"`CreatedAt time.Time `protobuf:"bytes,5,opt,name=created_at,json=createdAt,proto3,stdtime" json:"created_at"`XXX_NoUnkeyedLiteral struct{} `json:"-"`XXX_unrecognized []byte `json:"-"`XXX_sizecache int32 `json:"-"`}
func init() {proto.RegisterType((*Container)(nil), "containers.Container")proto.RegisterMapType((map[string]string)(nil), "containers.Container.LabelsEntry")proto.RegisterType((*GetContainerRequest)(nil), "containers.GetContainerRequest")proto.RegisterType((*GetContainerResponse)(nil), "containers.GetContainerResponse")proto.RegisterType((*ListContainersRequest)(nil), "containers.ListContainersRequest")proto.RegisterType((*ListContainersResponse)(nil), "containers.ListContainersResponse")proto.RegisterType((*CreateContainerRequest)(nil), "containers.CreateContainerRequest")proto.RegisterType((*CreateContainerResponse)(nil), "containers.CreateContainerResponse")proto.RegisterType((*DeleteContainerRequest)(nil), "containers.DeleteContainerRequest")proto.RegisterType((*ListContainerMessage)(nil), "containers.ListContainerMessage")}
func init() {proto.RegisterFile("api/services/containers/containers.proto", fileDescriptor_776da4f07b3f96b9)}
client
// ContainersClient is the client API for Containers service.//// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.type ContainersClient interface {Get(ctx context.Context, in *GetContainerRequest, opts ...grpc.CallOption) (*GetContainerResponse, error)List(ctx context.Context, in *ListContainersRequest, opts ...grpc.CallOption) (*ListContainersResponse, error)Create(ctx context.Context, in *CreateContainerRequest, opts ...grpc.CallOption) (*CreateContainerResponse, error)Delete(ctx context.Context, in *DeleteContainerRequest, opts ...grpc.CallOption) (*empty.Empty, error)}type containersClient struct {cc *grpc.ClientConn}func NewContainersClient(cc *grpc.ClientConn) ContainersClient {return &containersClient{cc}}func (c *containersClient) Get(ctx context.Context, in *GetContainerRequest, opts ...grpc.CallOption) (*GetContainerResponse, error) {out := new(GetContainerResponse)err := c.cc.Invoke(ctx, "/containers.Containers/Get", in, out, opts...)if err != nil {return nil, err}return out, nil}func (c *containersClient) List(ctx context.Context, in *ListContainersRequest, opts ...grpc.CallOption) (*ListContainersResponse, error) {out := new(ListContainersResponse)err := c.cc.Invoke(ctx, "/containers.Containers/List", in, out, opts...)if err != nil {return nil, err}return out, nil}func (c *containersClient) Create(ctx context.Context, in *CreateContainerRequest, opts ...grpc.CallOption) (*CreateContainerResponse, error) {out := new(CreateContainerResponse)err := c.cc.Invoke(ctx, "/containers.Containers/Create", in, out, opts...)if err != nil {return nil, err}return out, nil}func (c *containersClient) Delete(ctx context.Context, in *DeleteContainerRequest, opts ...grpc.CallOption) (*empty.Empty, error) {out := new(empty.Empty)err := c.cc.Invoke(ctx, "/containers.Containers/Delete", in, out, opts...)if err != nil {return nil, err}return out, nil}
server
// ContainersServer is the server API for Containers service.type ContainersServer interface {Get(context.Context, *GetContainerRequest) (*GetContainerResponse, error)List(context.Context, *ListContainersRequest) (*ListContainersResponse, error)Create(context.Context, *CreateContainerRequest) (*CreateContainerResponse, error)Delete(context.Context, *DeleteContainerRequest) (*empty.Empty, error)}func RegisterContainersServer(s *grpc.Server, srv ContainersServer) {s.RegisterService(&_Containers_serviceDesc, srv)}var _Containers_serviceDesc = grpc.ServiceDesc{ServiceName: "containers.Containers",HandlerType: (*ContainersServer)(nil),Methods: []grpc.MethodDesc{{MethodName: "Get",Handler: _Containers_Get_Handler,},{MethodName: "List",Handler: _Containers_List_Handler,},{MethodName: "Create",Handler: _Containers_Create_Handler,},{MethodName: "Delete",Handler: _Containers_Delete_Handler,},},Streams: []grpc.StreamDesc{},Metadata: "api/services/containers/containers.proto",}
