micro/examples/client/pub/pub.go

41 lines
839 B
Go
Raw Normal View History

2015-12-02 22:56:57 +03:00
package main
import (
"fmt"
"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-12-02 22:56:57 +03:00
"golang.org/x/net/context"
)
// publishes a message
func pub(i int) {
msg := client.NewPublication("topic.go.micro.srv.example", &example.Message{
Say: fmt.Sprintf("This is a publication %d", i),
})
// create context with metadata
2016-01-28 20:55:28 +03:00
ctx := metadata.NewContext(context.Background(), map[string]string{
2015-12-02 22:56:57 +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 %d: %v\n", i, msg)
}
func main() {
cmd.Init()
fmt.Println("\n--- Publisher example ---\n")
for i := 0; i < 10; i++ {
2015-12-02 22:56:57 +03:00
pub(i)
}
}