Runtime

Micro is a runtime for microservices development

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:

  • API Gateway: A single http entry point with dynamic request routing using service discovery. The API gateway allows you to build a scalable microservice architecture on the backend and consolidate serving a public api on the frontend. The micro api provides powerful routing via discovery and pluggable handlers to serve http, grpc, websockets, publish events and more.

  • Interactive CLI: A CLI to describe, query and interact directly with your platform and services from the terminal. The CLI gives you all the commands you expect to understand what’s happening with your micro services. It also includes an interactive mode.

  • Service Proxy: A transparent proxy built on Go Micro and the MUCP protocol. Offload service discovery, load balancing, message encoding, middleware, transport and broker plugins to a single a location. Run it standalone or alongside your service.

  • Service Templates: Generate new service templates to get started quickly. Micro provides predefined templates for writing micro services. Always start in the same way, build identical services to be more productive.

  • SlackOps Bot: A bot which runs on your platform and lets you manage your applications from Slack itself. The micro bot enables ChatOps and gives you the ability to do everything with your team via messaging. It also includes ability to create slack commmands as services which are discovered dynamically.

  • Web Dashboard: The web dashboard allows you to explore your services, describe their endpoints, the request and response formats and even query them directly. The dashboard is also includes a built in CLI like experience for developers who want to drop into the terminal on the fly.

Install Micro

  1. go get github.com/micro/micro

Or via Docker

  1. docker pull micro/micro

Latest release binaries

  1. # Mac OS or Linux
  2. curl -fsSL https://micro.mu/install.sh | /bin/bash
  3. # Windows
  4. powershell -Command "iwr -useb https://micro.mu/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

  1. # install
  2. brew install etcd
  3. # run
  4. etcd

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

  1. # Use flag
  2. micro --registry=etcd list services
  3. # Use env var
  4. MICRO_REGISTRY=etcd micro list services`

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

  1. 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.

  1. # install protobuf
  2. brew install protobuf
  3. # install protoc-gen-go
  4. go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
  5. # install protoc-gen-micro
  6. go get -u github.com/micro/protoc-gen-micro

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

  1. micro new github.com/micro/example

The command will output

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

Compile the protobuf code using protoc

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

Now run it like any other go application

  1. # cd into your service directory
  2. cd github.com/micro/example
  3. # run the service
  4. micro run service --local

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.

  1. micro list services

Output

  1. etcd
  2. go.micro.srv.example
  3. 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.

  1. micro get service go.micro.srv.example

Output

  1. service go.micro.srv.example
  2. version latest
  3. ID Address Port Metadata
  4. go.micro.srv.example-437d1277-303b-11e8-9be9-f40f242f6897 192.168.1.65 53545 transport=http,broker=http,server=rpc,registry=etcd
  5. Endpoint: Example.Call
  6. Metadata: stream=false
  7. Request: {
  8. name string
  9. }
  10. Response: {
  11. msg string
  12. }
  13. Endpoint: Example.PingPong
  14. Metadata: stream=true
  15. Request: {}
  16. Response: {}
  17. Endpoint: Example.Stream
  18. Metadata: stream=true
  19. Request: {}
  20. Response: {}
  21. Endpoint: Func
  22. Metadata: subscriber=true,topic=topic.go.micro.srv.example
  23. Request: {
  24. say string
  25. }
  26. Response: {}
  27. Endpoint: Example.Handle
  28. Metadata: subscriber=true,topic=topic.go.micro.srv.example
  29. Request: {
  30. say string
  31. }
  32. Response: {}

Call service

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

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

Output

  1. {
  2. "msg": "Hello John"
  3. }

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.

  1. MICRO_API_HANDLER=rpc \
  2. MICRO_API_NAMESPACE=go.micro.srv \
  3. micro api

Some info:

  • MICRO_API_HANDLER sets the http handler
  • MICRO_API_NAMESPACE sets the service namespace

Call API

Make POST request to the api using json

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

Output

  1. {"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:

  • broker - pubsub message broker
  • registry - service discovery
  • selector - client side load balancing
  • transport - request-response or bidirectional streaming
  • client - the client which manages the above features
  • server - the server which manages the above features

Find plugins at go-plugins

Using Plugins

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

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. )

Building Binary

Rebuild the micro binary using the Go toolchain

  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

Enable Plugins

Enable the plugins with command line flags or env vars

  1. # flags
  2. micro --registry=etcdv3 --transport=nats --broker=kafka [command]
  3. # env vars
  4. MICRO_REGISTRY=etcdv3 MICRO_TRANSPORT=nats MICRO_BROKER=kafka micro [command]