diff --git a/README.md b/README.md index 4ef95a5..24ea1fb 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ Use flag `--registry=mdns` - [client](client) - Usage of the Client package to call a service. - [server](server) - Use of the Server package directly to server requests. - [service](service) - Example of the top level Service in go-micro. +- [grpc](grpc) - Examples of how to use [go-grpc](https://github.com/micro/go-grpc) ## External diff --git a/grpc/README.md b/grpc/README.md new file mode 100644 index 0000000..9ab5924 --- /dev/null +++ b/grpc/README.md @@ -0,0 +1,6 @@ +# GRPC + +Contains examples for using [go-grpc](https://github.com/micro/go-grpc) + +- [greeter](greeter) - A greeter example + diff --git a/grpc/greeter/README.md b/grpc/greeter/README.md new file mode 100644 index 0000000..24a8675 --- /dev/null +++ b/grpc/greeter/README.md @@ -0,0 +1,23 @@ +# Greeter Service + +An example Go-Micro based GRPC service + +## What's here? + +- **srv** - a GRPC greeter service +- **cli** - a GRPC client that calls the service once + +Run Service +``` +$ go run server/main.go --registry=mdns +2016/11/03 18:41:22 Listening on [::]:55194 +2016/11/03 18:41:22 Broker Listening on [::]:55195 +2016/11/03 18:41:22 Registering node: go.micro.srv.greeter-1e200612-a1f5-11e6-8e84-68a86d0d36b6 +``` + +Test Service +``` +$ go run client/main.go --registry=mdns +Hello John +``` + diff --git a/grpc/greeter/cli/main.go b/grpc/greeter/cli/main.go new file mode 100644 index 0000000..a9f67c4 --- /dev/null +++ b/grpc/greeter/cli/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + + hello "github.com/micro/examples/grpc/greeter/srv/proto/hello" + "github.com/micro/go-grpc" + "github.com/micro/go-micro/metadata" + + "golang.org/x/net/context" +) + +func main() { + service := grpc.NewService() + service.Init() + + // use the generated client stub + cl := hello.NewSayClient("go.micro.srv.greeter", service.Client()) + + // Set arbitrary headers in context + ctx := metadata.NewContext(context.Background(), map[string]string{ + "X-User-Id": "john", + "X-From-Id": "script", + }) + + rsp, err := cl.Hello(ctx, &hello.Request{ + Name: "John", + }) + if err != nil { + fmt.Println(err) + return + } + + fmt.Println(rsp.Msg) +} diff --git a/grpc/greeter/srv/main.go b/grpc/greeter/srv/main.go new file mode 100644 index 0000000..3d0b810 --- /dev/null +++ b/grpc/greeter/srv/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "log" + "time" + + hello "github.com/micro/examples/greeter/srv/proto/hello" + "github.com/micro/go-grpc" + "github.com/micro/go-micro" + + "golang.org/x/net/context" +) + +type Say struct{} + +func (s *Say) Hello(ctx context.Context, req *hello.Request, rsp *hello.Response) error { + log.Print("Received Say.Hello request") + rsp.Msg = "Hello " + req.Name + return nil +} + +func main() { + service := grpc.NewService( + micro.Name("go.micro.srv.greeter"), + micro.RegisterTTL(time.Second*30), + micro.RegisterInterval(time.Second*10), + ) + + // optionally setup command line usage + service.Init() + + // Register Handlers + hello.RegisterSayHandler(service.Server(), new(Say)) + + // Run server + if err := service.Run(); err != nil { + log.Fatal(err) + } +} diff --git a/grpc/greeter/srv/proto/hello/hello.pb.go b/grpc/greeter/srv/proto/hello/hello.pb.go new file mode 100644 index 0000000..a8801b0 --- /dev/null +++ b/grpc/greeter/srv/proto/hello/hello.pb.go @@ -0,0 +1,136 @@ +// Code generated by protoc-gen-go. +// source: github.com/micro/examples/grpc/greeter/srv/proto/hello/hello.proto +// DO NOT EDIT! + +/* +Package go_micro_srv_greeter is a generated protocol buffer package. + +It is generated from these files: + github.com/micro/examples/grpc/greeter/srv/proto/hello/hello.proto + +It has these top-level messages: + Request + Response +*/ +package go_micro_srv_greeter + +import proto "github.com/golang/protobuf/proto" +import fmt "fmt" +import math "math" + +import ( + client "github.com/micro/go-micro/client" + server "github.com/micro/go-micro/server" + context "golang.org/x/net/context" +) + +// 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.ProtoPackageIsVersion2 // please upgrade the proto package + +type Request struct { + Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"` +} + +func (m *Request) Reset() { *m = Request{} } +func (m *Request) String() string { return proto.CompactTextString(m) } +func (*Request) ProtoMessage() {} +func (*Request) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } + +type Response struct { + Msg string `protobuf:"bytes,1,opt,name=msg" json:"msg,omitempty"` +} + +func (m *Response) Reset() { *m = Response{} } +func (m *Response) String() string { return proto.CompactTextString(m) } +func (*Response) ProtoMessage() {} +func (*Response) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } + +func init() { + proto.RegisterType((*Request)(nil), "go.micro.srv.greeter.Request") + proto.RegisterType((*Response)(nil), "go.micro.srv.greeter.Response") +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ client.Option +var _ server.Option + +// Client API for Say service + +type SayClient interface { + Hello(ctx context.Context, in *Request, opts ...client.CallOption) (*Response, error) +} + +type sayClient struct { + c client.Client + serviceName string +} + +func NewSayClient(serviceName string, c client.Client) SayClient { + if c == nil { + c = client.NewClient() + } + if len(serviceName) == 0 { + serviceName = "go.micro.srv.greeter" + } + return &sayClient{ + c: c, + serviceName: serviceName, + } +} + +func (c *sayClient) Hello(ctx context.Context, in *Request, opts ...client.CallOption) (*Response, error) { + req := c.c.NewRequest(c.serviceName, "Say.Hello", in) + out := new(Response) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for Say service + +type SayHandler interface { + Hello(context.Context, *Request, *Response) error +} + +func RegisterSayHandler(s server.Server, hdlr SayHandler, opts ...server.HandlerOption) { + s.Handle(s.NewHandler(&Say{hdlr}, opts...)) +} + +type Say struct { + SayHandler +} + +func (h *Say) Hello(ctx context.Context, in *Request, out *Response) error { + return h.SayHandler.Hello(ctx, in, out) +} + +func init() { + proto.RegisterFile("github.com/micro/examples/grpc/greeter/srv/proto/hello/hello.proto", fileDescriptor0) +} + +var fileDescriptor0 = []byte{ + // 183 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x74, 0x8e, 0xcd, 0xaa, 0xc2, 0x30, + 0x10, 0x85, 0x6f, 0xe9, 0xfd, 0x33, 0x2b, 0x09, 0x2e, 0x44, 0xac, 0x48, 0x57, 0xae, 0x32, 0xa0, + 0x6f, 0xe0, 0xaa, 0x3b, 0xa1, 0x3e, 0x41, 0x5b, 0x86, 0xb4, 0xd0, 0x34, 0x71, 0x26, 0x2d, 0xfa, + 0xf6, 0xd2, 0x98, 0xa5, 0x6e, 0x86, 0xc3, 0x1c, 0x3e, 0xbe, 0x23, 0xce, 0xba, 0xf3, 0xed, 0x58, + 0xab, 0xc6, 0x1a, 0x30, 0x5d, 0x43, 0x16, 0xf0, 0x5e, 0x19, 0xd7, 0x23, 0x83, 0x26, 0xd7, 0x80, + 0x26, 0x44, 0x8f, 0x04, 0x4c, 0x13, 0x38, 0xb2, 0xde, 0x42, 0x8b, 0x7d, 0x1f, 0xaf, 0x0a, 0x1f, + 0xb9, 0xd2, 0x56, 0x05, 0x56, 0x31, 0x4d, 0x2a, 0x12, 0x79, 0x26, 0xfe, 0x4a, 0xbc, 0x8d, 0xc8, + 0x5e, 0x4a, 0xf1, 0x3d, 0x54, 0x06, 0xd7, 0xc9, 0x3e, 0x39, 0x2c, 0xca, 0x90, 0xf3, 0xad, 0xf8, + 0x2f, 0x91, 0x9d, 0x1d, 0x18, 0xe5, 0x52, 0xa4, 0x86, 0x75, 0xac, 0xe7, 0x78, 0xbc, 0x88, 0xf4, + 0x5a, 0x3d, 0x64, 0x21, 0x7e, 0x8a, 0x59, 0x24, 0x33, 0xf5, 0xce, 0xa1, 0xa2, 0x60, 0xb3, 0xfb, + 0x54, 0xbf, 0x04, 0xf9, 0x57, 0xfd, 0x1b, 0xa6, 0x9e, 0x9e, 0x01, 0x00, 0x00, 0xff, 0xff, 0xc1, + 0xe5, 0xed, 0x58, 0xf0, 0x00, 0x00, 0x00, +} diff --git a/grpc/greeter/srv/proto/hello/hello.proto b/grpc/greeter/srv/proto/hello/hello.proto new file mode 100644 index 0000000..a7c28a4 --- /dev/null +++ b/grpc/greeter/srv/proto/hello/hello.proto @@ -0,0 +1,15 @@ +syntax = "proto3"; + +package go.micro.srv.greeter; + +service Say { + rpc Hello(Request) returns (Response) {} +} + +message Request { + string name = 1; +} + +message Response { + string msg = 1; +}