Plugins

Go Micro is a pluggable framework

Overview

Go Micro is built on Go interfaces. Because of this the implementation of these interfaces are pluggable.

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. Contributions are welcome! Plugins ensure that your Go Micro services live on long after technology evolves.

Add plugins

If you want to integrate plugins simply link them in a separate file and rebuild

Create a plugins.go file and import the plugins you want:

  1. package main
  2. import (
  3. // consul registry
  4. _ "github.com/micro/go-plugins/registry/consul"
  5. // rabbitmq transport
  6. _ "github.com/micro/go-plugins/transport/rabbitmq"
  7. // kafka broker
  8. _ "github.com/micro/go-plugins/broker/kafka"
  9. )

Build your application by including the plugins file:

  1. # assuming files main.go and plugins.go are in the top level dir
  2. # For local use
  3. go build -o service *.go

Flag usage of plugins:

  1. service --registry=etcdv3 --transport=nats --broker=kafka

Or what’s preferred is using environment variables for 12-factor apps

  1. MICRO_REGISTRY=consul \
  2. MICRO_TRANSPORT=rabbitmq \
  3. MICRO_BROKER=kafka \
  4. service

Plugin Option

Alternatively you can set the plugin as an option to a service directly in code

  1. package main
  2. import (
  3. "github.com/micro/go-micro/v2"
  4. // consul registry
  5. "github.com/micro/go-plugins/registry/consul"
  6. // rabbitmq transport
  7. "github.com/micro/go-plugins/transport/rabbitmq"
  8. // kafka broker
  9. "github.com/micro/go-plugins/broker/kafka"
  10. )
  11. func main() {
  12. registry := consul.NewRegistry()
  13. broker := kafka.NewBroker()
  14. transport := rabbitmq.NewTransport()
  15. service := micro.NewService(
  16. micro.Name("greeter"),
  17. micro.Registry(registry),
  18. micro.Broker(broker),
  19. micro.Transport(transport),
  20. )
  21. service.Init()
  22. service.Run()
  23. }

Write Plugins

Plugins are a concept built on Go’s interface. Each package maintains a high level interface abstraction. Simply implement the interface and pass it in as an option to the service.

The service discovery interface is called Registry. Anything which implements this interface can be used as a registry. The same applies to the other packages.

  1. type Registry interface {
  2. Register(*Service, ...RegisterOption) error
  3. Deregister(*Service) error
  4. GetService(string) ([]*Service, error)
  5. ListServices() ([]*Service, error)
  6. Watch() (Watcher, error)
  7. String() string
  8. }

Browse go-plugins to get a better idea of implementation details.