include pubsub in the readme

This commit is contained in:
Asim Aslam 2018-03-22 16:43:57 +00:00
parent 2ae4214215
commit 7e0ee9ec08

View File

@ -38,6 +38,7 @@ Watch the [Golang UK Conf 2016](https://www.youtube.com/watch?v=xspaDovwk34) vid
- [Service Discovery](#service-discovery) - [Service Discovery](#service-discovery)
- [Writing a Service](#writing-a-service) - [Writing a Service](#writing-a-service)
- [Writing a Function](#writing-a-function) - [Writing a Function](#writing-a-function)
- [Publish & Subscribe](#publish--subscribe)
- [Plugins](#plugins) - [Plugins](#plugins)
- [Wrappers](#wrappers) - [Wrappers](#wrappers)
@ -259,6 +260,45 @@ func main() {
It's that simple. It's that simple.
## Publish & Subscribe
Go-micro has a built in message broker interface for event driven architectures.
PubSub operates on the same protobuf generated messages as RPC. They are encoded/decoded automatically and sent via the broker.
By default go-micro includes a point-to-point http broker but this can be swapped out via go-plugins.
### Publish
Create a new publisher with a `topic` name and service client
```
p := micro.NewPublisher("events", service.Client())
```
// Publish a proto message
```
p.Publish(context.TODO(), &proto.Event{Name: "event"})
```
### Subscribe
Create a message handler. It's signature should be `func(context.Context, v interface{}) error`.
```
func ProcessEvent(ctx context.Context, event *proto.Event) error {
fmt.Printf("Got event %+v\n", event)
return nil
}
```
Register the message handler with a `topic`
```
micro.RegisterSubscriber("events", ProcessEvent)
```
## Plugins ## Plugins
By default go-micro only provides a few implementation of each interface at the core but it's completely pluggable. There's already dozens of plugins which are available at [github.com/micro/go-plugins](https://github.com/micro/go-plugins). Contributions are welcome! By default go-micro only provides a few implementation of each interface at the core but it's completely pluggable. There's already dozens of plugins which are available at [github.com/micro/go-plugins](https://github.com/micro/go-plugins). Contributions are welcome!