Framework Plugins

Micro is a pluggable toolkit and framework. The internal features can be swapped out with any go-plugins.

The toolkit has a separate plugin interface. Learn more at micro/plugin.

Below is info on go-micro plugin usage.

Usage

Plugins can be added to go-micro in the following ways. By doing so they’ll be available to set via command line args or environment variables.

Import the plugins in a Go program then call service.Init to parse the command line and environment variables.

  1. import (
  2. "github.com/micro/go-micro/v2"
  3. _ "github.com/micro/go-plugins/broker/rabbitmq"
  4. _ "github.com/micro/go-plugins/registry/kubernetes"
  5. _ "github.com/micro/go-plugins/transport/nats"
  6. )
  7. func main() {
  8. service := micro.NewService(
  9. // Set service name
  10. micro.Name("my.service"),
  11. )
  12. // Parse CLI flags
  13. service.Init()
  14. }

Flags

Specify the plugins as flags

  1. go run service.go --broker=rabbitmq --registry=kubernetes --transport=nats

Env

Use env vars to specify the plugins

  1. MICRO_BROKER=rabbitmq \
  2. MICRO_REGISTRY=kubernetes \
  3. MICRO_TRANSPORT=nats \
  4. go run service.go

Options

Import and set as options when creating a new service

  1. import (
  2. "github.com/micro/go-micro/v2"
  3. "github.com/micro/go-plugins/registry/kubernetes"
  4. )
  5. func main() {
  6. registry := kubernetes.NewRegistry() //a default to using env vars for master API
  7. service := micro.NewService(
  8. // Set service name
  9. micro.Name("my.service"),
  10. // Set service registry
  11. micro.Registry(registry),
  12. )
  13. }

Build

An anti-pattern is modifying the main.go file to include plugins. Best practice recommendation is to includeplugins in a separate file and rebuild with it included. This allows for automation of building plugins andclean separation of concerns.

Create file plugins.go

  1. package main
  2. import (
  3. _ "github.com/micro/go-plugins/broker/rabbitmq"
  4. _ "github.com/micro/go-plugins/registry/kubernetes"
  5. _ "github.com/micro/go-plugins/transport/nats"
  6. )

Build with plugins.go

  1. go build -o service main.go plugins.go

Run with plugins

  1. MICRO_BROKER=rabbitmq \
  2. MICRO_REGISTRY=kubernetes \
  3. MICRO_TRANSPORT=nats \
  4. service

Rebuild Toolkit

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

Create a plugins.go file

  1. import (
  2. // etcd v3 registry
  3. _ "github.com/micro/go-plugins/registry/etcdv3"
  4. // nats transport
  5. _ "github.com/micro/go-plugins/transport/nats"
  6. // kafka broker
  7. _ "github.com/micro/go-plugins/broker/kafka"
  8. )

Build binary

  1. // For local use
  2. go build -i -o micro ./main.go ./plugins.go
  3. // For docker image
  4. CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o micro ./main.go ./plugins.go

Flag usage of plugins

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

Repository

The go-micro plugins can be found in github.com/micro/go-plugins.