Created
March 25, 2016 11:57
-
-
Save asim/602a8464fb0fcffee693 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"time" | |
"github.com/micro/go-micro/broker" | |
"github.com/micro/go-micro/cmd" | |
// use kafka | |
_ "github.com/micro/go-plugins/broker/kafka" | |
) | |
var ( | |
topic = "go.micro.topic.foo" | |
) | |
func pub() { | |
tick := time.NewTicker(time.Second) | |
i := 0 | |
for _ = range tick.C { | |
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 { | |
log.Printf("[pub] failed: %v", err) | |
} else { | |
fmt.Println("[pub] pubbed message:", string(msg.Body)) | |
} | |
i++ | |
} | |
} | |
func sub() { | |
_, err := broker.Subscribe(topic, func(p broker.Publication) error { | |
fmt.Println("[sub] received message:", string(p.Message().Body), "header", p.Message().Header) | |
return nil | |
}) | |
if err != nil { | |
fmt.Println(err) | |
} | |
} | |
func main() { | |
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) | |
} | |
go pub() | |
go sub() | |
<-time.After(time.Second * 10) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment