github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/pubsub/cli/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "time" 6 7 "context" 8 proto "github.com/micro/go-micro/examples/pubsub/srv/proto" 9 "github.com/micro/go-micro/v2" 10 "github.com/micro/go-micro/v2/util/log" 11 "github.com/pborman/uuid" 12 ) 13 14 // send events using the publisher 15 func sendEv(topic string, p micro.Publisher) { 16 t := time.NewTicker(time.Second) 17 18 for _ = range t.C { 19 // create new event 20 ev := &proto.Event{ 21 Id: uuid.NewUUID().String(), 22 Timestamp: time.Now().Unix(), 23 Message: fmt.Sprintf("Messaging you all day on %s", topic), 24 } 25 26 log.Logf("publishing %+v\n", ev) 27 28 // publish an event 29 if err := p.Publish(context.Background(), ev); err != nil { 30 log.Logf("error publishing: %v", err) 31 } 32 } 33 } 34 35 func main() { 36 // create a service 37 service := micro.NewService( 38 micro.Name("go.micro.cli.pubsub"), 39 ) 40 // parse command line 41 service.Init() 42 43 // create publisher 44 pub1 := micro.NewPublisher("example.topic.pubsub.1", service.Client()) 45 pub2 := micro.NewPublisher("example.topic.pubsub.2", service.Client()) 46 47 // pub to topic 1 48 go sendEv("example.topic.pubsub.1", pub1) 49 // pub to topic 2 50 go sendEv("example.topic.pubsub.2", pub2) 51 52 // block forever 53 select {} 54 }