Skip to content

Commit

Permalink
add grpc example
Browse files Browse the repository at this point in the history
  • Loading branch information
Asim Aslam committed Jan 5, 2017
1 parent 29e7ccb commit f430139
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions grpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# GRPC

Contains examples for using [go-grpc](https://github.com/micro/go-grpc)

- [greeter](greeter) - A greeter example

23 changes: 23 additions & 0 deletions grpc/greeter/README.md
Original file line number Diff line number Diff line change
@@ -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
```

35 changes: 35 additions & 0 deletions grpc/greeter/cli/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
39 changes: 39 additions & 0 deletions grpc/greeter/srv/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
136 changes: 136 additions & 0 deletions grpc/greeter/srv/proto/hello/hello.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions grpc/greeter/srv/proto/hello/hello.proto
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit f430139

Please sign in to comment.