2015-12-02 19:56:57 +00: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 17:55:28 +00:00
|
|
|
"github.com/micro/go-micro/metadata"
|
2015-12-02 19:56:57 +00: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 17:55:28 +00:00
|
|
|
ctx := metadata.NewContext(context.Background(), map[string]string{
|
2015-12-02 19:56:57 +00: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")
|
2015-12-02 20:11:00 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
2015-12-02 19:56:57 +00:00
|
|
|
pub(i)
|
|
|
|
}
|
|
|
|
}
|