2015-04-26 22:45:46 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2015-05-23 19:55:08 +03:00
|
|
|
log "github.com/golang/glog"
|
2015-05-05 21:05:06 +03:00
|
|
|
"github.com/myodc/go-micro/broker"
|
2015-05-23 19:55:08 +03:00
|
|
|
"github.com/myodc/go-micro/cmd"
|
2015-04-26 22:45:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
topic = "go.micro.topic.foo"
|
|
|
|
)
|
|
|
|
|
|
|
|
func pub() {
|
|
|
|
tick := time.NewTicker(time.Second)
|
|
|
|
i := 0
|
|
|
|
for _ = range tick.C {
|
2015-06-12 21:52:27 +03:00
|
|
|
msg := &broker.Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"id": fmt.Sprintf("%d", i),
|
|
|
|
},
|
|
|
|
Body: []byte(fmt.Sprintf("%d: %s", i, time.Now().String())),
|
|
|
|
}
|
|
|
|
if err := broker.Publish(topic, msg); err != nil {
|
2015-05-23 19:55:08 +03:00
|
|
|
log.Errorf("[pub] failed: %v", err)
|
|
|
|
} else {
|
2015-06-12 21:52:27 +03:00
|
|
|
fmt.Println("[pub] pubbed message:", string(msg.Body))
|
2015-05-23 19:55:08 +03:00
|
|
|
}
|
2015-04-26 22:45:46 +03:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sub() {
|
2015-06-12 21:52:27 +03:00
|
|
|
_, err := broker.Subscribe(topic, func(msg *broker.Message) {
|
|
|
|
fmt.Println("[sub] received message:", string(msg.Body), "header", msg.Header)
|
2015-04-26 22:45:46 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2015-05-23 19:55:08 +03:00
|
|
|
cmd.Init()
|
|
|
|
|
|
|
|
if err := broker.Init(); err != nil {
|
|
|
|
log.Fatalf("Broker Init error: %v", err)
|
|
|
|
}
|
|
|
|
if err := broker.Connect(); err != nil {
|
|
|
|
log.Fatalf("Broker Connect error: %v", err)
|
|
|
|
}
|
2015-04-26 22:45:46 +03:00
|
|
|
|
|
|
|
go pub()
|
|
|
|
go sub()
|
|
|
|
|
|
|
|
<-time.After(time.Second * 10)
|
|
|
|
}
|