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-25 18:16:42 +01:00
|
|
|
example "github.com/myodc/go-micro/examples/server/proto/example"
|
2015-05-23 11:53:40 +01:00
|
|
|
"golang.org/x/net/context"
|
2015-01-13 23:31:27 +00:00
|
|
|
)
|
|
|
|
|
2015-06-12 19:52:27 +01:00
|
|
|
func pub() {
|
|
|
|
msg := client.NewPublication("topic.go.micro.srv.example", &example.Message{
|
|
|
|
Say: "This is a publication",
|
|
|
|
})
|
|
|
|
|
|
|
|
// create context with metadata
|
|
|
|
ctx := c.WithMetadata(context.Background(), map[string]string{
|
|
|
|
"X-User-Id": "john",
|
|
|
|
"X-From-Id": "script",
|
|
|
|
})
|
|
|
|
|
|
|
|
// publish message
|
|
|
|
if err := client.Publish(ctx, msg); err != nil {
|
|
|
|
fmt.Println("pub err: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Published: %v\n", msg)
|
|
|
|
}
|
|
|
|
|
2015-06-01 18:55:27 +01:00
|
|
|
func call(i int) {
|
2015-05-25 18:16:42 +01:00
|
|
|
// Create new request to service go.micro.srv.example, method Example.Call
|
|
|
|
req := client.NewRequest("go.micro.srv.example", "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
|
2015-05-26 22:39:48 +01:00
|
|
|
ctx := c.WithMetadata(context.Background(), map[string]string{
|
2015-05-23 11:53:40 +01:00
|
|
|
"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-06-12 19:52:27 +01:00
|
|
|
fmt.Println("call err: ", err, rsp)
|
2015-06-01 18:55:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Call:", i, "rsp:", rsp.Msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func stream() {
|
|
|
|
// Create new request to service go.micro.srv.example, method Example.Call
|
|
|
|
req := client.NewRequest("go.micro.srv.example", "Example.Stream", &example.StreamingRequest{
|
|
|
|
Count: int64(10),
|
|
|
|
})
|
|
|
|
|
|
|
|
rspChan := make(chan *example.StreamingResponse, 10)
|
|
|
|
|
|
|
|
stream, err := client.Stream(context.Background(), req, rspChan)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("err:", err)
|
2015-01-13 23:31:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-01 18:55:27 +01:00
|
|
|
for rsp := range rspChan {
|
|
|
|
fmt.Println("Stream: rsp:", rsp.Count)
|
|
|
|
}
|
|
|
|
|
|
|
|
if stream.Error() != nil {
|
2015-06-12 19:52:27 +01:00
|
|
|
fmt.Println("stream err:", err)
|
2015-06-01 18:55:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cmd.Init()
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
call(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
stream()
|
2015-06-12 19:52:27 +01:00
|
|
|
pub()
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|