2015-01-14 02:31:27 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2015-11-20 19:17:33 +03:00
|
|
|
"github.com/micro/go-micro/client"
|
|
|
|
"github.com/micro/go-micro/cmd"
|
|
|
|
example "github.com/micro/go-micro/examples/server/proto/example"
|
2016-01-28 20:55:28 +03:00
|
|
|
"github.com/micro/go-micro/metadata"
|
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
|
|
|
// 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
|
2016-01-28 20:55:28 +03:00
|
|
|
ctx := metadata.NewContext(context.Background(), map[string]string{
|
2015-06-12 21:52:27 +03:00
|
|
|
"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
|
2016-01-28 20:55:28 +03:00
|
|
|
ctx := metadata.NewContext(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)
|
|
|
|
}
|
|
|
|
|
2015-12-18 23:28:50 +03:00
|
|
|
func stream(i int) {
|
2015-06-01 20:55:27 +03:00
|
|
|
// Create new request to service go.micro.srv.example, method Example.Call
|
2015-12-18 23:28:50 +03:00
|
|
|
// Request can be empty as its actually ignored and merely used to call the handler
|
2015-12-18 04:01:59 +03:00
|
|
|
req := client.NewRequest("go.micro.srv.example", "Example.Stream", &example.StreamingRequest{})
|
2015-06-01 20:55:27 +03:00
|
|
|
|
2015-12-17 23:37:35 +03:00
|
|
|
stream, err := client.Stream(context.Background(), req)
|
2015-06-01 20:55:27 +03:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("err:", err)
|
2015-01-14 02:31:27 +03:00
|
|
|
return
|
|
|
|
}
|
2015-12-18 23:28:50 +03:00
|
|
|
if err := stream.Send(&example.StreamingRequest{Count: int64(i)}); err != nil {
|
|
|
|
fmt.Println("err:", err)
|
|
|
|
return
|
|
|
|
}
|
2015-12-17 23:37:35 +03:00
|
|
|
for stream.Error() == nil {
|
|
|
|
rsp := &example.StreamingResponse{}
|
|
|
|
err := stream.Recv(rsp)
|
|
|
|
if err != nil {
|
2015-12-18 04:21:56 +03:00
|
|
|
fmt.Println("recv err", err)
|
2015-12-17 23:37:35 +03:00
|
|
|
break
|
|
|
|
}
|
2015-06-01 20:55:27 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-11-28 19:34:48 +03:00
|
|
|
if err := stream.Close(); err != nil {
|
|
|
|
fmt.Println("stream close err:", err)
|
|
|
|
}
|
2015-06-01 20:55:27 +03:00
|
|
|
}
|
|
|
|
|
2015-12-18 23:28:50 +03:00
|
|
|
func pingPong(i int) {
|
|
|
|
// Create new request to service go.micro.srv.example, method Example.Call
|
|
|
|
// Request can be empty as its actually ignored and merely used to call the handler
|
|
|
|
req := client.NewRequest("go.micro.srv.example", "Example.PingPong", &example.StreamingRequest{})
|
|
|
|
|
|
|
|
stream, err := client.Stream(context.Background(), req)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("err:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := 0; j < i; j++ {
|
2015-12-19 19:54:47 +03:00
|
|
|
if err := stream.Send(&example.Ping{Stroke: int64(j + 1)}); err != nil {
|
2015-12-18 23:28:50 +03:00
|
|
|
fmt.Println("err:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rsp := &example.Pong{}
|
|
|
|
err := stream.Recv(rsp)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("recv err", err)
|
|
|
|
break
|
|
|
|
}
|
2015-12-19 19:54:47 +03:00
|
|
|
fmt.Printf("Sent ping %v got pong %v\n", j+1, rsp.Stroke)
|
2015-12-18 23:28:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if stream.Error() != nil {
|
|
|
|
fmt.Println("stream err:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := stream.Close(); err != nil {
|
|
|
|
fmt.Println("stream close err:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
func main() {
|
|
|
|
cmd.Init()
|
2016-04-28 02:16:11 +03:00
|
|
|
|
|
|
|
fmt.Println("\n--- Publisher example ---\n")
|
|
|
|
pub()
|
|
|
|
|
2015-12-19 00:51:51 +03:00
|
|
|
fmt.Println("\n--- Call example ---\n")
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
call(i)
|
|
|
|
}
|
2015-11-27 03:17:36 +03:00
|
|
|
|
2015-12-19 00:51:51 +03:00
|
|
|
fmt.Println("\n--- Streamer example ---\n")
|
|
|
|
stream(10)
|
2015-12-18 23:28:50 +03:00
|
|
|
|
|
|
|
fmt.Println("\n--- Ping Pong example ---\n")
|
|
|
|
pingPong(10)
|
2015-11-27 03:17:36 +03:00
|
|
|
|
2015-01-14 02:31:27 +03:00
|
|
|
}
|