mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-12 08:23:58 +02:00
d94936f6c9
* v3 * revert plugins * fixup some issues
55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"context"
|
|
proto "github.com/micro/go-micro/examples/pubsub/srv/proto"
|
|
"github.com/asim/go-micro/v3"
|
|
"github.com/asim/go-micro/v3/util/log"
|
|
"github.com/pborman/uuid"
|
|
)
|
|
|
|
// send events using the publisher
|
|
func sendEv(topic string, p micro.Publisher) {
|
|
t := time.NewTicker(time.Second)
|
|
|
|
for _ = range t.C {
|
|
// create new event
|
|
ev := &proto.Event{
|
|
Id: uuid.NewUUID().String(),
|
|
Timestamp: time.Now().Unix(),
|
|
Message: fmt.Sprintf("Messaging you all day on %s", topic),
|
|
}
|
|
|
|
log.Logf("publishing %+v\n", ev)
|
|
|
|
// publish an event
|
|
if err := p.Publish(context.Background(), ev); err != nil {
|
|
log.Logf("error publishing: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
// create a service
|
|
service := micro.NewService(
|
|
micro.Name("go.micro.cli.pubsub"),
|
|
)
|
|
// parse command line
|
|
service.Init()
|
|
|
|
// create publisher
|
|
pub1 := micro.NewPublisher("example.topic.pubsub.1", service.Client())
|
|
pub2 := micro.NewPublisher("example.topic.pubsub.2", service.Client())
|
|
|
|
// pub to topic 1
|
|
go sendEv("example.topic.pubsub.1", pub1)
|
|
// pub to topic 2
|
|
go sendEv("example.topic.pubsub.2", pub2)
|
|
|
|
// block forever
|
|
select {}
|
|
}
|