// TODO: publish event func (p *platform) Register(s *registry.Service, opts ...registry.RegisterOption) error { p.Lock() defer p.Unlock() if err := p.opts.Registry.Register(s, opts...); err != nil { return err } service := toProto(s) hb := &proto.Heartbeat{ Id: s.Nodes[0].Id, Service: service, Interval: int64(p.opts.Interval.Seconds()), Ttl: int64((p.opts.Interval.Seconds()) * 5), } p.heartbeats[hb.Id] = hb // now register return client.Publish(context.TODO(), client.NewPublication(WatchTopic, &proto.Result{ Action: "update", Service: service, Timestamp: time.Now().Unix(), })) }
// TODO: publish event func (p *platform) Deregister(s *registry.Service) error { p.Lock() defer p.Unlock() if err := p.opts.Registry.Deregister(s); err != nil { return err } delete(p.heartbeats, s.Nodes[0].Id) // now deregister return client.Publish(context.TODO(), client.NewPublication(WatchTopic, &proto.Result{ Action: "delete", Service: toProto(s), Timestamp: time.Now().Unix(), })) }
// publishes a message func pub(i int) { msg := client.NewPublication("topic.go.micro.srv.example", &example.Message{ Say: fmt.Sprintf("This is a publication %d", i), }) // create context with metadata ctx := metadata.NewContext(context.Background(), map[string]string{ "X-User-Id": "john", "X-From-Id": "script", }) // publish message if err := client.Publish(ctx, msg); err != nil { fmt.Println("pub err: ", err) return } fmt.Printf("Published %d: %v\n", i, msg) }
// Publish a change func Publish(ctx context.Context, ch *proto.WatchResponse) error { req := client.NewPublication(WatchTopic, ch) return client.Publish(ctx, req) }