1.安装

go get google.golang.org/grpc
go get github.com/golang/protobuf

gogoprotobuf
go get github.com/gogo/protobuf/protoc-gen-gofast
go get github.com/gogo/protobuf/proto
go get github.com/gogo/protobuf/protoc-gen-gogo
go 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

  1. syntax = "proto3";
  2. package containers;
  3. import "github.com/gogo/protobuf/gogoproto/gogo.proto";
  4. import "google/protobuf/any.proto";
  5. import "google/protobuf/empty.proto";
  6. import "google/protobuf/timestamp.proto";
  7. option go_package = "containers";
  8. // Containers provides metadata storage for containers used in the execution
  9. // service.
  10. service Containers {
  11. rpc Get(GetContainerRequest) returns (GetContainerResponse);
  12. rpc List(ListContainersRequest) returns (ListContainersResponse);
  13. rpc Create(CreateContainerRequest) returns (CreateContainerResponse);
  14. rpc Delete(DeleteContainerRequest) returns (google.protobuf.Empty);
  15. }
  16. message Container {
  17. string id = 1;
  18. map<string, string> labels = 2;
  19. string image = 3;
  20. google.protobuf.Any spec = 4;
  21. google.protobuf.Timestamp created_at = 5 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
  22. }
  23. message GetContainerRequest {
  24. string id = 1;
  25. }
  26. message GetContainerResponse {
  27. Container container = 1 [(gogoproto.nullable) = false];
  28. }
  29. message ListContainersRequest {
  30. }
  31. message ListContainersResponse {
  32. repeated Container containers = 1 [(gogoproto.nullable) = false];
  33. }
  34. message CreateContainerRequest {
  35. Container container = 1 [(gogoproto.nullable) = false];
  36. }
  37. message CreateContainerResponse {
  38. Container container = 1 [(gogoproto.nullable) = false];
  39. }
  40. message DeleteContainerRequest {
  41. string id = 1;
  42. }
  43. message ListContainerMessage {
  44. Container container = 1;
  45. }

3.codegen

protoc -I.:${GOPATH}/src --gofast_out=plugins=grpc:. api/services/containers/containers.proto

  1. type Container struct {
  2. Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
  3. 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"`
  4. Image string `protobuf:"bytes,3,opt,name=image,proto3" json:"image,omitempty"`
  5. Spec *any.Any `protobuf:"bytes,4,opt,name=spec,proto3" json:"spec,omitempty"`
  6. CreatedAt time.Time `protobuf:"bytes,5,opt,name=created_at,json=createdAt,proto3,stdtime" json:"created_at"`
  7. XXX_NoUnkeyedLiteral struct{} `json:"-"`
  8. XXX_unrecognized []byte `json:"-"`
  9. XXX_sizecache int32 `json:"-"`
  10. }
  1. func init() {
  2. proto.RegisterType((*Container)(nil), "containers.Container")
  3. proto.RegisterMapType((map[string]string)(nil), "containers.Container.LabelsEntry")
  4. proto.RegisterType((*GetContainerRequest)(nil), "containers.GetContainerRequest")
  5. proto.RegisterType((*GetContainerResponse)(nil), "containers.GetContainerResponse")
  6. proto.RegisterType((*ListContainersRequest)(nil), "containers.ListContainersRequest")
  7. proto.RegisterType((*ListContainersResponse)(nil), "containers.ListContainersResponse")
  8. proto.RegisterType((*CreateContainerRequest)(nil), "containers.CreateContainerRequest")
  9. proto.RegisterType((*CreateContainerResponse)(nil), "containers.CreateContainerResponse")
  10. proto.RegisterType((*DeleteContainerRequest)(nil), "containers.DeleteContainerRequest")
  11. proto.RegisterType((*ListContainerMessage)(nil), "containers.ListContainerMessage")
  12. }
  1. func init() {
  2. proto.RegisterFile("api/services/containers/containers.proto", fileDescriptor_776da4f07b3f96b9)
  3. }

client

  1. // ContainersClient is the client API for Containers service.
  2. //
  3. // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
  4. type ContainersClient interface {
  5. Get(ctx context.Context, in *GetContainerRequest, opts ...grpc.CallOption) (*GetContainerResponse, error)
  6. List(ctx context.Context, in *ListContainersRequest, opts ...grpc.CallOption) (*ListContainersResponse, error)
  7. Create(ctx context.Context, in *CreateContainerRequest, opts ...grpc.CallOption) (*CreateContainerResponse, error)
  8. Delete(ctx context.Context, in *DeleteContainerRequest, opts ...grpc.CallOption) (*empty.Empty, error)
  9. }
  10. type containersClient struct {
  11. cc *grpc.ClientConn
  12. }
  13. func NewContainersClient(cc *grpc.ClientConn) ContainersClient {
  14. return &containersClient{cc}
  15. }
  16. func (c *containersClient) Get(ctx context.Context, in *GetContainerRequest, opts ...grpc.CallOption) (*GetContainerResponse, error) {
  17. out := new(GetContainerResponse)
  18. err := c.cc.Invoke(ctx, "/containers.Containers/Get", in, out, opts...)
  19. if err != nil {
  20. return nil, err
  21. }
  22. return out, nil
  23. }
  24. func (c *containersClient) List(ctx context.Context, in *ListContainersRequest, opts ...grpc.CallOption) (*ListContainersResponse, error) {
  25. out := new(ListContainersResponse)
  26. err := c.cc.Invoke(ctx, "/containers.Containers/List", in, out, opts...)
  27. if err != nil {
  28. return nil, err
  29. }
  30. return out, nil
  31. }
  32. func (c *containersClient) Create(ctx context.Context, in *CreateContainerRequest, opts ...grpc.CallOption) (*CreateContainerResponse, error) {
  33. out := new(CreateContainerResponse)
  34. err := c.cc.Invoke(ctx, "/containers.Containers/Create", in, out, opts...)
  35. if err != nil {
  36. return nil, err
  37. }
  38. return out, nil
  39. }
  40. func (c *containersClient) Delete(ctx context.Context, in *DeleteContainerRequest, opts ...grpc.CallOption) (*empty.Empty, error) {
  41. out := new(empty.Empty)
  42. err := c.cc.Invoke(ctx, "/containers.Containers/Delete", in, out, opts...)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return out, nil
  47. }

server

  1. // ContainersServer is the server API for Containers service.
  2. type ContainersServer interface {
  3. Get(context.Context, *GetContainerRequest) (*GetContainerResponse, error)
  4. List(context.Context, *ListContainersRequest) (*ListContainersResponse, error)
  5. Create(context.Context, *CreateContainerRequest) (*CreateContainerResponse, error)
  6. Delete(context.Context, *DeleteContainerRequest) (*empty.Empty, error)
  7. }
  8. func RegisterContainersServer(s *grpc.Server, srv ContainersServer) {
  9. s.RegisterService(&_Containers_serviceDesc, srv)
  10. }
  11. var _Containers_serviceDesc = grpc.ServiceDesc{
  12. ServiceName: "containers.Containers",
  13. HandlerType: (*ContainersServer)(nil),
  14. Methods: []grpc.MethodDesc{
  15. {
  16. MethodName: "Get",
  17. Handler: _Containers_Get_Handler,
  18. },
  19. {
  20. MethodName: "List",
  21. Handler: _Containers_List_Handler,
  22. },
  23. {
  24. MethodName: "Create",
  25. Handler: _Containers_Create_Handler,
  26. },
  27. {
  28. MethodName: "Delete",
  29. Handler: _Containers_Delete_Handler,
  30. },
  31. },
  32. Streams: []grpc.StreamDesc{},
  33. Metadata: "api/services/containers/containers.proto",
  34. }