2015-01-14 02:31:27 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-11-26 23:43:33 +03:00
|
|
|
"time"
|
2015-01-14 02:31:27 +03:00
|
|
|
|
2015-11-20 19:17:33 +03:00
|
|
|
"github.com/micro/go-micro/client"
|
|
|
|
"github.com/micro/go-micro/cmd"
|
|
|
|
c "github.com/micro/go-micro/context"
|
|
|
|
example "github.com/micro/go-micro/examples/server/proto/example"
|
2015-05-23 13:53:40 +03:00
|
|
|
"golang.org/x/net/context"
|
2015-01-14 02:31:27 +03:00
|
|
|
)
|
|
|
|
|
2015-11-26 23:43:33 +03:00
|
|
|
// wrapper example code
|
|
|
|
|
|
|
|
// log wrapper logs every time a request is made
|
|
|
|
type logWrapper struct {
|
|
|
|
client.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logWrapper) Call(ctx context.Context, req client.Request, rsp interface{}) error {
|
|
|
|
md, _ := c.GetMetadata(ctx)
|
|
|
|
fmt.Printf("[Log Wrapper] ctx: %v service: %s method: %s\n", md, req.Service(), req.Method())
|
|
|
|
return l.Client.Call(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// trace wrapper attaches a unique trace ID - timestamp
|
|
|
|
type traceWrapper struct {
|
|
|
|
client.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *traceWrapper) Call(ctx context.Context, req client.Request, rsp interface{}) error {
|
|
|
|
ctx = c.WithMetadata(ctx, map[string]string{
|
|
|
|
"X-Trace-Id": fmt.Sprintf("%d", time.Now().Unix()),
|
|
|
|
})
|
|
|
|
return t.Client.Call(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements client.Wrapper as logWrapper
|
|
|
|
func logWrap(c client.Client) client.Client {
|
|
|
|
return &logWrapper{c}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements client.Wrapper as traceWrapper
|
|
|
|
func traceWrap(c client.Client) client.Client {
|
|
|
|
return &traceWrapper{c}
|
|
|
|
}
|
|
|
|
|
|
|
|
// publishes a message
|
2015-06-12 21:52:27 +03: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 20:55:27 +03:00
|
|
|
func call(i int) {
|
2015-05-25 20:16:42 +03: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 02:42:07 +03:00
|
|
|
Name: "John",
|
2015-01-14 02:31:27 +03:00
|
|
|
})
|
|
|
|
|
2015-05-23 13:53:40 +03:00
|
|
|
// create context with metadata
|
2015-05-27 00:39:48 +03:00
|
|
|
ctx := c.WithMetadata(context.Background(), map[string]string{
|
2015-05-23 13:53:40 +03:00
|
|
|
"X-User-Id": "john",
|
|
|
|
"X-From-Id": "script",
|
|
|
|
})
|
2015-01-14 02:31:27 +03:00
|
|
|
|
|
|
|
rsp := &example.Response{}
|
|
|
|
|
|
|
|
// Call service
|
2015-05-23 13:53:40 +03:00
|
|
|
if err := client.Call(ctx, req, rsp); err != nil {
|
2015-06-12 21:52:27 +03:00
|
|
|
fmt.Println("call err: ", err, rsp)
|
2015-06-01 20:55:27 +03: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-14 02:31:27 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
for rsp := range rspChan {
|
|
|
|
fmt.Println("Stream: rsp:", rsp.Count)
|
|
|
|
}
|
|
|
|
|
|
|
|
if stream.Error() != nil {
|
2015-06-12 21:52:27 +03:00
|
|
|
fmt.Println("stream err:", err)
|
2015-06-01 20:55:27 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cmd.Init()
|
|
|
|
|
2015-11-28 14:22:29 +03:00
|
|
|
// client.DefaultClient = client.NewClient(
|
|
|
|
// client.Codec("application/pb", pb.Codec),
|
|
|
|
// client.ContentType("application/pb"),
|
|
|
|
// )
|
|
|
|
for {
|
|
|
|
fmt.Println("\n--- Call example ---\n")
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
call(i)
|
|
|
|
}
|
2015-11-27 03:17:36 +03:00
|
|
|
|
2015-11-28 14:22:29 +03:00
|
|
|
fmt.Println("\n--- Streamer example ---\n")
|
|
|
|
stream()
|
2015-11-27 03:17:36 +03:00
|
|
|
|
2015-11-28 14:22:29 +03:00
|
|
|
fmt.Println("\n--- Publisher example ---\n")
|
|
|
|
pub()
|
2015-11-26 23:43:33 +03:00
|
|
|
|
2015-11-28 14:22:29 +03:00
|
|
|
fmt.Println("\n--- Wrapper example ---\n")
|
2015-11-26 23:43:33 +03:00
|
|
|
|
2015-11-28 14:22:29 +03:00
|
|
|
// Wrap the default client
|
|
|
|
client.DefaultClient = logWrap(client.DefaultClient)
|
2015-11-26 23:43:33 +03:00
|
|
|
|
2015-11-28 14:22:29 +03:00
|
|
|
call(0)
|
2015-11-26 23:43:33 +03:00
|
|
|
|
2015-11-28 14:22:29 +03:00
|
|
|
// Wrap using client.Wrap option
|
|
|
|
client.DefaultClient = client.NewClient(
|
|
|
|
client.Wrap(traceWrap),
|
|
|
|
client.Wrap(logWrap),
|
|
|
|
)
|
2015-11-26 23:43:33 +03:00
|
|
|
|
2015-11-28 14:22:29 +03:00
|
|
|
call(1)
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
}
|
2015-01-14 02:31:27 +03:00
|
|
|
}
|