mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-24 10:07:04 +02:00
Add producer/consumer example with use of queue name for distribution of messages
This commit is contained in:
parent
5e1a618415
commit
74714876f2
51
examples/pubsub/consumer.go
Normal file
51
examples/pubsub/consumer.go
Normal file
@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
log "github.com/golang/glog"
|
||||
"github.com/micro/go-micro/broker"
|
||||
"github.com/micro/go-micro/cmd"
|
||||
|
||||
_ "github.com/micro/go-plugins/broker/rabbitmq"
|
||||
)
|
||||
|
||||
var (
|
||||
topic = "go.micro.topic.foo"
|
||||
)
|
||||
|
||||
// Example of a shared subscription which receives a subset of messages
|
||||
func sharedSub() {
|
||||
_, err := broker.Subscribe(topic, func(p broker.Publication) error {
|
||||
fmt.Println("[sub] received message:", string(p.Message().Body), "header", p.Message().Header)
|
||||
return nil
|
||||
}, broker.QueueName("consumer"))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Example of a subscription which receives all the messages
|
||||
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)
|
||||
}
|
||||
|
||||
sub()
|
||||
select {}
|
||||
}
|
49
examples/pubsub/producer.go
Normal file
49
examples/pubsub/producer.go
Normal file
@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
log "github.com/golang/glog"
|
||||
"github.com/micro/go-micro/broker"
|
||||
"github.com/micro/go-micro/cmd"
|
||||
|
||||
_ "github.com/micro/go-plugins/broker/rabbitmq"
|
||||
)
|
||||
|
||||
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.Errorf("[pub] failed: %v", err)
|
||||
} else {
|
||||
fmt.Println("[pub] pubbed message:", string(msg.Body))
|
||||
}
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
pub()
|
||||
}
|
Loading…
Reference in New Issue
Block a user