-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Asim Aslam
committed
Jan 5, 2017
1 parent
29e7ccb
commit f430139
Showing
7 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |