Micro

Overview

Micro addresses the key requirements for building scalable systems. It takes the microservice architecture pattern and transforms it into a set of tools which act as the building blocks of a platform. Micro deals with the complexity of distributed systems and provides simple abstractions already understood by developers.

Technology is constantly evolving. The infrastructure stack is always changing. Micro is a pluggable toolkit which addresses these issues. Plug in any stack or underlying technology. Build future-proof systems using micro.

Features

The runtime is composed of the following features:

Install Micro

go get github.com/micro/micro/v2

Or via Docker

docker pull micro/micro

Latest release binaries

# MacOS
curl -fsSL https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh | /bin/bash

# Linux
wget -q  https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh -O - | /bin/bash

# Windows
powershell -Command "iwr -useb https://raw.githubusercontent.com/micro/micro/master/scripts/install.ps1 | iex"

Dependencies

The micro runtime has two dependencies:

Service Discovery

Service discovery is used for name resolution, routing and centralising metadata. All services are register themselves in discovery so they can be found by other services.

Micro uses the go-micro registry interface for service discovery. Multicast DNS is the default implementation. This enables a zeroconf setup so you don’t have to change anything for local development. In case you’re using docker, kubernetes or need something more resilient we recommend etcd.

Etcd

Etcd is an alternative highly available service discovery mechanism

# install
brew install etcd

# run
etcd

Pass --registry=etcd or set the env var MICRO_REGISTRY=consul for any command

# Use flag
micro --registry=etcd list services

# Use env var
MICRO_REGISTRY=etcd micro list services`

Specify the address of etcd where it’s not on the same host.

MICRO_REGISTRY_ADDRESS=10.0.0.1:2379

See go-plugins for more service discovery plugins.

Protobuf

Protobuf is used for code generation. It reduces the amount of boilerplate code needed to be written.

# install protobuf
brew install protobuf

# install protoc-gen-go
go get github.com/golang/protobuf/{proto,protoc-gen-go}

# install protoc-gen-micro
go get github.com/micro/micro/v2/cmd/protoc-gen-micro@master

See protoc-gen-micro for more details.

Writing a service

Micro includes new template generation to speed up writing applications

For full details on writing services see go-micro.

Generate template

Here we’ll quickly generate an example template using micro new

Specify a path relative to $GOPATH

micro new github.com/micro/example

The command will output

example/
	Dockerfile	# A template docker file
	README.md	# A readme with command used
	handler/	# Example rpc handler
	main.go		# The main Go program
	proto/		# Protobuf directory
	subscriber/	# Example pubsub Subscriber

Compile the protobuf code using protoc

protoc --proto_path=. --micro_out=. --go_out=. proto/example/example.proto

Now run it like any other go application

# Run the micro service
micro server

# cd into your service directory
cd github.com/micro/example

# run the server
micro run --server .

Example

Now we have a running application using micro new template generation, let’s test it out.

List services

Each service registers with discovery so we should be able to find it.

micro list services

Output

etcd
go.micro.srv.example
topic:topic.go.micro.srv.example

The example app has registered with the fully qualified domain name go.micro.srv.example

Get Service

Each service registers with a unique id, address and metadata.

micro get service go.micro.srv.example

Output

service  go.micro.srv.example

version latest

ID	Address	Port	Metadata
go.micro.srv.example-437d1277-303b-11e8-9be9-f40f242f6897	192.168.1.65	53545	transport=http,broker=http,server=rpc,registry=etcd

Endpoint: Example.Call
Metadata: stream=false

Request: {
	name string
}

Response: {
	msg string
}


Endpoint: Example.PingPong
Metadata: stream=true

Request: {}

Response: {}


Endpoint: Example.Stream
Metadata: stream=true

Request: {}

Response: {}


Endpoint: Func
Metadata: subscriber=true,topic=topic.go.micro.srv.example

Request: {
	say string
}

Response: {}


Endpoint: Example.Handle
Metadata: subscriber=true,topic=topic.go.micro.srv.example

Request: {
	say string
}

Response: {}

Call service

Make an RPC call via the CLI. The query is sent as json.

micro call go.micro.srv.example Example.Call '{"name": "John"}'

Output

{
	"msg": "Hello John"
}

Look at the cli doc for more info.

Now let’s test call the service via HTTP.

Run API

The micro api is a http gateway which dynamically routes to backend services

Let’s run it so we can query the example service.

MICRO_API_HANDLER=rpc \
MICRO_API_NAMESPACE=go.micro.srv \
micro api

Some info:

Call API

Make POST request to the api using json

curl -XPOST -H 'Content-Type: application/json' -d '{"name": "John"}' http://localhost:8080/example/call

Output

{"msg":"Hello John"}

See the api doc for more info.

Plugins

Micro is built on go-micro making it a pluggable toolkit.

Go-micro provides abstractions for distributed systems infrastructure which can be swapped out.

Pluggable Features

The micro features which are pluggable:

Find plugins at go-plugins

Using Plugins

Integrate go-micro plugins by simply linking them in a separate file

Create a plugins.go file

import (
	// etcd v3 registry
	_ "github.com/micro/go-plugins/registry/etcdv3"
	// nats transport
	_ "github.com/micro/go-plugins/transport/nats"
	// kafka broker
	_ "github.com/micro/go-plugins/broker/kafka"
)

Building Binary

Rebuild the micro binary using the Go toolchain

# For local use
go build -i -o micro ./main.go ./plugins.go

# For docker image
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o micro ./main.go ./plugins.go

Enable Plugins

Enable the plugins with command line flags or env vars

# flags
micro --registry=etcdv3 --transport=nats --broker=kafka [command]

# env vars
MICRO_REGISTRY=etcdv3 MICRO_TRANSPORT=nats MICRO_BROKER=kafka micro [command]