micro/examples/service_client.go

36 lines
754 B
Go
Raw Normal View History

2015-01-13 23:31:27 +00:00
package main
import (
"fmt"
2015-05-05 19:05:06 +01:00
"github.com/myodc/go-micro/client"
2015-05-21 19:24:57 +01:00
"github.com/myodc/go-micro/cmd"
2015-05-23 11:53:40 +01:00
c "github.com/myodc/go-micro/context"
2015-05-05 19:05:06 +01:00
example "github.com/myodc/go-micro/template/proto/example"
2015-05-23 11:53:40 +01:00
"golang.org/x/net/context"
2015-01-13 23:31:27 +00:00
)
func main() {
2015-05-21 19:24:57 +01:00
cmd.Init()
2015-01-13 23:31:27 +00:00
// Create new request to service go.micro.service.go-template, method Example.Call
req := client.NewRequest("go.micro.service.template", "Example.Call", &example.Request{
2015-05-09 00:42:07 +01:00
Name: "John",
2015-01-13 23:31:27 +00:00
})
2015-05-23 11:53:40 +01:00
// create context with metadata
ctx := c.WithMetaData(context.Background(), map[string]string{
"X-User-Id": "john",
"X-From-Id": "script",
})
2015-01-13 23:31:27 +00:00
rsp := &example.Response{}
// Call service
2015-05-23 11:53:40 +01:00
if err := client.Call(ctx, req, rsp); err != nil {
2015-01-13 23:31:27 +00:00
fmt.Println(err)
return
}
2015-05-09 00:42:07 +01:00
fmt.Println(rsp.Msg)
2015-01-13 23:31:27 +00:00
}