PubSub

Go Micro builds in PubSub for event driven microservices

Overview

Microservices is an event driven architecture patterna and so Go Micro builds in the concept of asynchronous messaging using a message broker interface. It seamlessly operates on protobuf types for you. Automatically encoding and decoding messages as they are sent and received from the broker.

By default go-micro includes a point-to-point http broker but this can be swapped out via go-plugins.

Publish Message

Create a new publisher with a topic name and service client

  1. p := micro.NewEvent("events", service.Client())

Publish a proto message

  1. p.Publish(context.TODO(), &proto.Event{Name: "event"})

Subscribe

Create a message handler. It’s signature should be func(context.Context, v interface{}) error.

  1. func ProcessEvent(ctx context.Context, event *proto.Event) error {
  2. fmt.Printf("Got event %+v\n", event)
  3. return nil
  4. }

Register the message handler with a topic

  1. micro.RegisterSubscriber("events", ProcessEvent)

See examples/pubsub for a complete example.