Rework use of context
This commit is contained in:
@@ -5,16 +5,17 @@ import (
|
||||
|
||||
h "github.com/grpc/grpc-common/go/helloworld"
|
||||
"github.com/myodc/go-micro/client"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// run github.com/grpc/grpc-common/go/greeter_server/main.go
|
||||
func main() {
|
||||
req := client.NewRpcRequest("helloworld.Greeter", "SayHello", &h.HelloRequest{
|
||||
req := client.NewRpcRequest("helloworld.Greeter", "helloworld.Greeter/SayHello", &h.HelloRequest{
|
||||
Name: "John",
|
||||
}, "application/grpc")
|
||||
|
||||
rsp := &h.HelloReply{}
|
||||
err := client.CallRemote("localhost:50051", "helloworld.Greeter/SayHello", req, rsp)
|
||||
err := client.CallRemote(context.Background(), "localhost:50051", req, rsp)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
@@ -5,7 +5,9 @@ import (
|
||||
|
||||
"github.com/myodc/go-micro/client"
|
||||
"github.com/myodc/go-micro/cmd"
|
||||
c "github.com/myodc/go-micro/context"
|
||||
example "github.com/myodc/go-micro/template/proto/example"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -16,14 +18,16 @@ func main() {
|
||||
Name: "John",
|
||||
})
|
||||
|
||||
// Set arbitrary headers
|
||||
req.Headers().Set("X-User-Id", "john")
|
||||
req.Headers().Set("X-From-Id", "script")
|
||||
// create context with metadata
|
||||
ctx := c.WithMetaData(context.Background(), map[string]string{
|
||||
"X-User-Id": "john",
|
||||
"X-From-Id": "script",
|
||||
})
|
||||
|
||||
rsp := &example.Response{}
|
||||
|
||||
// Call service
|
||||
if err := client.Call(req, rsp); err != nil {
|
||||
if err := client.Call(ctx, req, rsp); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user