1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00
go-micro/examples/pubsub/srv/main.go
2021-01-20 21:28:48 +00:00

51 lines
1.3 KiB
Go

package main
import (
proto "github.com/asim/go-micro/examples/v3/pubsub/srv/proto"
"github.com/asim/go-micro/v3"
"github.com/asim/go-micro/v3/metadata"
"github.com/asim/go-micro/v3/server"
"github.com/asim/go-micro/v3/util/log"
"context"
)
// All methods of Sub will be executed when
// a message is received
type Sub struct{}
// Method can be of any name
func (s *Sub) Process(ctx context.Context, event *proto.Event) error {
md, _ := metadata.FromContext(ctx)
log.Logf("[pubsub.1] Received event %+v with metadata %+v\n", event, md)
// do something with event
return nil
}
// Alternatively a function can be used
func subEv(ctx context.Context, event *proto.Event) error {
md, _ := metadata.FromContext(ctx)
log.Logf("[pubsub.2] Received event %+v with metadata %+v\n", event, md)
// do something with event
return nil
}
func main() {
// create a service
service := micro.NewService(
micro.Name("go.micro.srv.pubsub"),
)
// parse command line
service.Init()
// register subscriber
micro.RegisterSubscriber("example.topic.pubsub.1", service.Server(), new(Sub))
// register subscriber with queue, each message is delivered to a unique subscriber
micro.RegisterSubscriber("example.topic.pubsub.2", service.Server(), subEv, server.SubscriberQueue("queue.pubsub"))
if err := service.Run(); err != nil {
log.Fatal(err)
}
}