Reference

This reference doc is an in depth guide for the technical details and usage of Micro

Overview

Micro is a platform for cloud native development. It consists of a server, command line interface and service framework which enables you to build, run, manage and consume Micro services. This reference walks through the majority of Micro in depth and attempts to help guide you through any usage. It should be thought of much like a language spec and will evolve over time.

Installation

Below are the instructions for installing micro locally, in docker or on kubernetes

Local

Micro can be installed locally in the following way. We assume for the most part a Linux env with Go and Git installed.

Go Get

  1. go get github.com/micro/micro/v3

Docker

  1. docker pull micro/micro

Release Binaries

  1. # MacOS
  2. curl -fsSL https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh | /bin/bash
  3. # Linux
  4. wget -q https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh -O - | /bin/bash
  5. # Windows
  6. powershell -Command "iwr -useb https://raw.githubusercontent.com/micro/micro/master/scripts/install.ps1 | iex"

Kubernetes

Micro can be installed onto a Kubernetes cluster using helm. Micro will be deployed in full and leverage zero-dep implementations designed for Kubernetes. For example, micro store will internally leverage a file store on a persistent volume, meaning there are no infrastructure dependencies required.

Dependencies

You will need to be connected to a Kubernetes cluster

Install

Install micro with the following commands:

  1. helm repo add micro https://micro.github.io/helm
  2. helm install micro micro/micro

Uninstall

Uninstall micro with the following commands:

  1. helm uninstall micro
  2. helm repo remove micro

Server

The micro server is a distributed systems runtime for the Cloud and beyond. It provides the building blocks for distributed systems development as a set of services, command line and service framework. The server is much like a distributed operating system in the sense that each component runs independent of each other but work together as one system. This composition allows us to use a microservices architecture pattern even for the platform.

Features

The server provides the below functionality as built in primitives for services development.

  • Authentication
  • Configuration
  • PubSub Messaging
  • Event Streaming
  • Service Discovery
  • Service Networking
  • Key-Value Storage
  • HTTP API Gateway
  • gRPC Identity Proxy

Usage

To start the server simply run

  1. micro server

This will boot the entire system and services including a http api on :8080 and grpc proxy on :8081

Help

Run the following command to check help output

  1. micro --help

Commands

Run helloworld and check its status

  1. micro env # should point to local
  2. micro run github.com/micro/services/helloworld # run helloworld
  3. micro status # wait for status running
  4. micro services # should display helloworld

Call the service and verify output

  1. $ micro helloworld --name=John
  2. {
  3. "msg": "Hello John"
  4. }

Remove the service

  1. micro kill helloworld

Command Line

The command line interface is the primary way to interact with a micro server. It’s a simple binary that can either be interacted with using simple commands or an interactive prompt. The CLI proxies all commands as RPC calls to the Micro server. In many of the builtin commands it will perform formatting and additional syntactic work.

Builtin Commands

Built in commands are system or configuration level commands for interacting with the server or changing user config. For the most part this is syntactic sugar for user convenience. Here’s a subset of well known commands.

  1. signup
  2. login
  3. run
  4. update
  5. kill
  6. services
  7. logs
  8. status
  9. env
  10. user

The micro binary and each subcommand has a –help flag to provide a usage guide. The majority should be obvious to the user. We will go through a few in more detail.

Signup

Signup is a command which attempts to query a “signup” to register a new account, this is env specific and requires a signup service to be running. By default locally this will not exist and we expect the user to use the admin/micro credentials to administer the system. You can then choose to run your own signup service conforming to the proto in micro/proto or use micro auth create account.

Signup is seen as a command for those who want to run their own micro server for others and potentially license the software to take payment.

Login

Login authenticates the user and stores credentials locally in a .micro/tokens file. This calls the micro auth service to authenticate the user against existing accounts stored in the system. Login asks for a username and password at the prompt.

Dynamic Commands

When issuing a command to the Micro CLI (ie. micro command), if the command is not a builtin, Micro will try to dynamically resolve this command and call a service running. Let’s take the micro registry command, because although the registry is a core service that’s running by default on a local Micro setup, the registry command is not a builtin one.

With the --help flag, we can get information about available subcommands and flags

  1. $ micro registry --help
  2. NAME:
  3. micro registry
  4. VERSION:
  5. latest
  6. USAGE:
  7. micro registry [command]
  8. COMMANDS:
  9. deregister
  10. getService
  11. listServices
  12. register
  13. watch

The commands listed are endpoints of the registry service (see micro services).

To see the flags (which are essentially endpoint request parameters) for a subcommand:

  1. $ micro registry getService --help
  2. NAME:
  3. micro registry getService
  4. USAGE:
  5. micro registry getService [flags]
  6. FLAGS:
  7. --service string
  8. --options_ttl int64
  9. --options_domain string

At this point it is useful to have a look at the proto of the registry service here.

In particular, let’s see the GetService endpoint definition to understand how request parameters map to flags:

  1. message Options {
  2. int64 ttl = 1;
  3. string domain = 2;
  4. }
  5. message GetRequest {
  6. string service = 1;
  7. Options options = 2;
  8. }

As the above definition tells us, the request of GetService has the field service at the top level, and fields ttl and domain in an options structure. The dynamic CLI maps the underscored flagnames (ie. options_domain) to request fields, so the following request JSON:

  1. {
  2. "service": "serviceName",
  3. "options": {
  4. "domain": "domainExample"
  5. }
  6. }

is equivalent to the following flags:

  1. micro registry getService --service=serviceName --options_domain=domainExample

User Config

The command line uses local user config stores in ~/.micro for any form of state such as saved environments, tokens, etc. It will always attempt to read from here unless specified otherwise. Currently we store all config in a single file config.json and any auth tokens in a tokens file.

Environments

Micro is built with a federated and multi-environment model in mind. Our development normally maps through local, staging and production, so Micro takes this forward looking view and builds in the notion of environments which are completely isolated micro environments you can interact with through the CLI. This reference explains environments.

View Current

Environments can be displayed using the micro env command.

  1. $ micro env
  2. * local 127.0.0.1:8081
  3. dev proxy.m3o.dev
  4. platform proxy.m3o.com

There are three builtin environments, local being the default, and two m3o specific offerings; dev and platform. These exist for convenience and speed of development. Additional environments can be created using micro env add [name] [host:port]. Environment addresses point to the micro proxy which defaults to :8081.

Add Environment

The command micro env --help provides a summary of usage. Here’s an example of how to add an environment.

  1. $ micro env add foobar example.com
  2. $ micro env
  3. * local 127.0.0.1:8081
  4. dev proxy.m3o.dev
  5. platform proxy.m3o.com
  6. foobar example.com

Set Environment

The * marks which environment is selected. Let’s select the newly added:

  1. $ micro env set foobar
  2. $ micro env
  3. local 127.0.0.1:8081
  4. dev proxy.m3o.dev
  5. platform proxy.m3o.com
  6. * foobar example.com

Login to an Environment

Each environment is effectively an isolated deployment with its own authentication, storage, etc. So each env requires signup and login. At this point we have to log in to the example env with micro login. If you don’t have credentials to the environment, you have to ask the admin.

Services

Micro is built as a distributed operating system leveraging the microservices architecture pattern.

Overview

Below we describe the list of services provided by the Micro Server. Each service is considered a building block primitive for a platform and distributed systems development. The proto interfaces for each can be found in micro/proto/auth and the Go library, client and server implementations in micro/service/auth.

API

The API service is a http API gateway which acts as a public entrypoint and converts http/json to RPC.

Overview

The micro API is the public entrypoint for all external access to services to be consumed by frontend, mobile, etc. The api accepts http/json requests and uses path based routing to resolve to backend services. It converts the request to gRPC and forward appropriately. The idea here is to focus on microservices on the backend and stitch everything together as a single API for the frontend.

Usage

In the default local environment the API address is 127.0.0.1:8080. Each service running is callable through this API.

  1. $ curl http://127.0.0.1:8080/
  2. {"version": "v3.0.0-beta"}

An example call would be listing services in the registry:

  1. $ curl http://127.0.0.1:8080/registry/listServices

The format is

  1. curl http://127.0.0.1:8080/[servicename]/[endpointName]

The endpoint name is lower camelcase.

The parameters can be passed on as query params

  1. $ curl http://127.0.0.1:8080/helloworld/call?name=Joe
  2. {"msg":"Hello Joe"}

or JSON body:

  1. curl -XPOST --header "Content-Type: application/json" -d '{"name":"Joe"}' http://127.0.0.1:8080/helloworld/call
  2. {"msg":"Hello Joe"}

To specify a namespace when calling the API, the Micro-Namespace header can be used:

  1. $ curl -H "Micro-Namespace: foobar" http://127.0.0.1:8080/helloworld/call?name=Joe

To call a non-public service/endpoint, the Authorization header can be used:

  1. MICRO_API_TOKEN=`micro user token`
  2. curl -H "Authorization: Bearer $MICRO_API_TOKEN" http://127.0.0.1:8080/helloworld/call?name=Joe

Auth

The auth service provides both authentication and authorization.

Overview

The auth service stores accounts and access rules. It provides the single source of truth for all authentication and authorization within the Micro runtime. Every service and user requires an account to operate. When a service is started by the runtime an account is generated for it. Core services and services run by Micro load rules periodically and manage the access to their resources on a per request basis.

Usage

For CLI command help use micro auth --help or auth subcommand help such as micro auth create --help.

Login

To login to a server simply so the following

  1. $ micro login
  2. Enter username: admin
  3. Enter password:
  4. Successfully logged in.

Assuming you are pointing to the right environment. It defaults to the local micro server.

Rules

Rules determine what resource a user can access. The default rule is the following:

  1. $ micro auth list rules
  2. ID Scope Access Resource Priority
  3. default <public> GRANTED *:*:* 0

The default rule makes all services callable that appear in the micro status output. Let’s see an example of this.

  1. $ micro run helloworld
  2. # Wait for the service to accept calls
  3. $ curl 127.0.0.1:8080/helloworld/call?name=Alice
  4. {"msg":"Hello Alice"}

If we want to prevent unauthorized users from calling our services, we can create the following rule

  1. # This command creates a rule that enables only logged in users to call the micro server
  2. micro auth create rule --access=granted --scope='*' --resource="*:*:*" onlyloggedin
  3. # Create the rule which allows us to login
  4. micro auth create rule --access=granted --resource="service:auth:*" auth-public

and delete the default one. Here, the scope * is markedly different from the <public> scope we have seen earlier when doing a micro auth list rules:

  1. $ micro auth list rules
  2. ID Scope Access Resource Priority
  3. auth-public <public> GRANTED service:auth:* 0
  4. onlyloggedin * GRANTED *:*:* 0
  5. default <public> GRANTED *:*:* 0

Now, let’s remove the default rule.

  1. # This command deletes the 'default' rule - the rule which enables anyone to call the 'micro server'.
  2. $ micro auth delete rule default
  3. Rule deleted

Let’s try curling our service again:

  1. $ curl 127.0.0.1:8080/helloworld/call?name=Alice
  2. {"Id":"helloworld","Code":401,"Detail":"Unauthorized call made to helloworld:Helloworld.Call","Status":"Unauthorized"}

Our onlyloggedin rule took effect. We can still call the service with a token:

  1. $ token=$(micro user token)
  2. # Locally:
  3. # curl "Authorization: Bearer $token" 127.0.0.1:8080/helloworld/call?name=Alice
  4. {"msg":"Hello Alice"}

(Please note tokens have a limited lifetime so the line $ token=$(micro user token) has to be reissued from time to time, or the command must be used inline.)

Accounts

Auth service supports the concept of accounts. The default account used to access the micro server is the admin account.

  1. $ micro auth list accounts
  2. ID Name Scopes Metadata
  3. admin admin admin n/a

We can create accounts for teammates and coworkers with micro auth create account:

  1. $ micro auth create account --scopes=admin jane
  2. Account created: {"id":"jane","type":"","issuer":"micro","metadata":null,"scopes":["admin"],"secret":"bb7c1a96-c0c6-4ff5-a0e9-13d456f3db0a","name":"jane"}

The freshly created account can be used with micro login by using the jane id and bb7c1a96-c0c6-4ff5-a0e9-13d456f3db0a password.

Broker

The broker is a message broker for asynchronous pubsub messaging.

Overview

The broker provides a simple abstraction for pubsub messaging. It focuses on simple semantics for fire-and-forget asynchronous communication. The goal here is to provide a pattern for async notifications where some update or events occurred but that does not require persistence. The client and server build in the ability to publish on one side and subscribe on the other. The broker provides no message ordering guarantees.

While a Service is normally called by name, messaging focuses on Topics that can have multiple publishers and subscribers. The broker is abstracting away in the service’s client/server which includes message encoding/decoding so you don’t have to spend all your time marshalling.

Client

The client contains the Publish method which takes a proto message, encodes it and publishes onto the broker on a given topic. It takes the metadata from the client context and includes these as headers in the message including the content-type so the subscribe side knows how to deal with it.

Server

The server supports a Subscribe method which allows you to register a handler as you would for handling requests. In this way we can mirror the handler behaviour and deserialize the message when consuming from the broker. In this model the server handles connecting to the broker, subscribing, consuming and executing your subscriber function.

Usage

Publisher:

  1. bytes, err := json.Marshal(&Healthcheck{
  2. Healthy: true,
  3. Service: "foo",
  4. })
  5. if err != nil {
  6. return err
  7. }
  8. return broker.Publish("health", &broker.Message{Body: bytes})

Subscriber:

  1. handler := func(msg *broker.Message) error {
  2. var hc Healthcheck
  3. if err := json.Unmarshal(msg.Body, &hc); err != nil {
  4. return err
  5. }
  6. if hc.Healthy {
  7. logger.Infof("Service %v is healthy", hc.Service)
  8. } else {
  9. logger.Infof("Service %v is not healthy", hc.Service)
  10. }
  11. return nil
  12. }
  13. sub, err := broker.Subscribe("health", handler)
  14. if err != nil {
  15. return err
  16. }

Config

The config service provides dynamic configuration for services.

Overview

Config can be stored and loaded separately to the application itself for configuring business logic, api keys, etc. We read and write these as key-value pairs which also support nesting of JSON values. The config interface also supports storing secrets by defining the secret key as an option at the time of writing the value.

Usage

Let’s assume we have a service called helloworld from which we want to read configuration data. First we have to insert said data with the cli. Config data can be organized under different “paths” with the dot notation. It’s a good convention to save all config data belonging to a service under a top level path segment matching the service name:

  1. $ micro config set helloworld.somekey hello
  2. $ micro config get helloworld.somekey
  3. hello

We can save another key too and read all values in one go with the dot notation:

  1. $ micro config set helloworld.someotherkey "Hi there!"
  2. $ micro config get helloworld
  3. {"somekey":"hello","someotherkey":"Hi there!"}

As it can be seen, the config (by default) stores configuration data as JSONs. We can save any type:

  1. $ micro config set helloworld.someboolkey true
  2. $ micro config get helloworld.someboolkey
  3. true
  4. $ micro config get helloworld
  5. {"someboolkey":true,"somekey":"hello","someotherkey":"Hi there!"}

So far we have only saved top level keys. Let’s explore the advantages of the dot notation.

  1. $ micro config set helloworld.keywithsubs.subkey1 "So easy!"
  2. {"keywithsubs":{"subkey1":"So easy!"},"someboolkey":true,"somekey":"hello","someotherkey":"Hi there!"}

Some of the example keys are getting in our way, let’s learn how to delete:

  1. $ micro config del helloworld.someotherkey
  2. $ micro config get helloworld
  3. {"keywithsubs":{"subkey1":"So easy!"},"someboolkey":true,"somekey":"hello"}

We can of course delete not just leaf level keys, but top level ones too:

  1. $ micro config del helloworld.keywithsubs
  2. $ micro config get helloworld
  3. {"someboolkey":true,"somekey":"hello"}
Secrets

The config also supports secrets - values encrypted at rest. This helps in case of leaks, be it a security one or an accidental copy paste.

They are fairly easy to save:

  1. $ micro config set --secret helloworld.hushkey "Very secret stuff"
  2. $ micro config get helloworld.hushkey
  3. [secret]
  4. $ micro config get --secret helloworld.hushkey
  5. Very secret stuff
  6. $ micro config get helloworld
  7. {"hushkey":"[secret]","someboolkey":true,"somekey":"hello"}
  8. $ micro config get --secret helloworld
  9. {"hushkey":"Very secret stuff","someboolkey":true,"somekey":"hello"}

Even bool or number values can be saved as secrets, and they will appear as the string constant [secret] unless decrypted:

  1. $ micro config set --secret helloworld.hush_number_key 42
  2. $ micro config get helloworld
  3. {"hush_number_key":"[secret]","hushkey":"[secret]","someboolkey":true,"somekey":"hello"}
  4. $ micro config get --secret helloworld
  5. {"hush_number_key":42,"hushkey":"Very secret stuff","someboolkey":true,"somekey":"hello"}

Service Framework

It is similarly easy to access and set config values from a service. A good example of reading values is the config example test service:

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/micro/micro/v3/service"
  6. "github.com/micro/micro/v3/service/config"
  7. )
  8. type keyConfig struct {
  9. Subkey string `json:"subkey"`
  10. Subkey1 int `json:"subkey1"`
  11. }
  12. type conf struct {
  13. Key keyConfig `json:"key"`
  14. }
  15. func main() {
  16. go func() {
  17. for {
  18. time.Sleep(time.Second)
  19. val, err := config.Get("key.subkey")
  20. fmt.Println("Value of key.subkey: ", val.String(""), err)
  21. val, err = config.Get("key", config.Secret(true))
  22. if err != nil {
  23. fmt.Println(err)
  24. }
  25. c := conf{}
  26. err = val.Scan(&c.Key)
  27. fmt.Println("Value of key.subkey1: ", c.Key.Subkey1, err)
  28. }
  29. }()
  30. // run the service
  31. service.Run()
  32. }

The above service will print the value of key.subkey and key.subkey every second. By passing in the config.Secret(true) option, we tell config to decrypt secret values for us, similarly to the --secret CLI flag.

The config interface specifies not just Get Set and Delete to access values, but a few convenience functions too in the Value interface.

It is worth noting that String Int etc methods will do a best effort try at coercing types, ie. if the value saved is a string, Int will try to parse it. However, the same does not apply to the Scan method, which uses json.Unmarshal under the hood, which we all know fails when encountering type mismatches.

Get should, in all cases, return a non nil Value, so even if the Get errors, Value.Int() and other operations should never panic.

Advanced Concepts

Merging Config Values

When saving a string with the CLI that is a valid JSON map, it gets expanded to be saved as a proper map structure, instead of a string, ie

  1. $ micro config set helloworld '{"a": "val1", "b": "val2"}'
  2. $ micro config get helloworld.a
  3. val1
  4. # If the string would be saved as is, `helloworld.a` would be a nonexistent path

The advantages of this become particularly visible when Setting a complex type with the library:

  1. type conf struct {
  2. A string `json:"a"`
  3. B string `json:"b"`
  4. }
  5. c1 := conf{"val1", "val2"}
  6. config.Set("key", c1)
  7. v, _ := config.Get("key")
  8. c2 := &conf{}
  9. v.Scan(c2)
  10. // c1 and c2 should be equal

Or with the following example

  1. $ micro config del helloworld
  2. $ micro config set helloworld '{"a":1}'
  3. $ micro config get helloworld
  4. {"a":1}
  5. $ micro config set helloworld '{"b":2}'
  6. $ micro config get helloworld
  7. {"a":1,"b":2}

Secret encryption keys for micro server

By default, if not specified, micro server generates and saves an encryption key to the location ~/.micro/config_secret_key. This is intended for local zero dependency use, but not for production.

To specify the secret for the micro server either the env var MICRO_CONFIG_SECRET_KEY or the flag config_secret_key key must be specified.

Errors

The errors package provides error types for most common HTTP status codes, e.g. BadRequest, InternalServerError etc. It’s recommended when returning an error to an RPC handler, one of these errors is used. If any other type of error is returned, it’s treated as an InternalServerError.

Micro API detects these error types and will use them to determine the response status code. For example, if your handler returns errors.BadRequest, the API will return a 400 status code. If no error is returned the API will return the default 200 status code.

Error codes are also used when handling retries. If your service returns a 500 (InternalServerError) or 408 (Timeout) then the client will retry the request. Other status codes are treated as client error and won’t be retried.

Usage

  1. import (
  2. "github.com/micro/micro/v3/service/errors"
  3. )
  4. func (u *Users) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadResponse) error {
  5. if len(req.Id) == 0 {
  6. return errors.BadRequest("users.Read.MissingID", "Missing ID")
  7. }
  8. ...
  9. }

Events

The events service is a service for event streaming and persistent storage of events.

Overview

Event streaming differs from pubsub messaging in that it provides an ordered stream of events that can be consumed or replayed from any given point in the past. If you have experience with Kafka then you know it’s basically a distributed log which allows you to read a file from different offsets and stream it.

The event service and interface provide the event streaming abstraction for writing and reading events along with consuming from any given offset. It also supports acking and error handling where appropriate.

Events also different from the broker in that it provides a fixed Event type where you fill in the details and handle the decoding of the message body yourself. Events could have large payloads so we don’t want to unnecessarily decode where you may just want to hand off to a storage system.

Functions

The events package has two parts: Stream and Store. Stream is used to Publish and Consume to messages for a given topic. For example, in a chat application one user would Publish a message and another would subscribe. If you later needed to retrieve messages, you could either replay them using the Subscribe function and passing the Offset option, or list them using the Read function.

  1. func Publish(topic string, msg interface{}, opts ...PublishOption) error

The Publish function has two required arguments: topic and message. Topic is the channel you’re publishing the event to, in the case of a chat application this would be the chat id. The message is any struct, e.g. the message being sent to the chat. When the subscriber receives the event they’ll be able to unmarshal this object. Publish has two supported options, WithMetadata to pass key/value pairs and WithTimestamp to override the default timestamp on the event.

  1. func Consume(topic string, opts ...ConsumeOption) (<-chan Event, error)

The Consume function is used to consume events. In the case of a chat application, the client would pass the chat ID as the topic, and any events published to the stream will be sent to the event channel. Event has an Unmarshal function which can be used to access the message payload, as demonstrated below:

  1. for {
  2. evChan, err := events.Consume(chatID)
  3. if err != nil {
  4. logger.Error("Error subscribing to topic %v: %v", chatID, err)
  5. return err
  6. }
  7. for {
  8. ev, ok := <- evChan
  9. if !ok {
  10. break
  11. }
  12. var msg Message
  13. if err :=ev.Unmarshal(&msg); err != nil {
  14. logger.Errorf("Error unmarshaling event %v: %v", ev.ID, err)
  15. return err
  16. }
  17. logger.Infof("Received message: %v", msg.Subject)
  18. }
  19. }

Example

The Chat Service examples usage of the events service, leveraging both the stream and store functions.

Network

The network is a service to service network for request proxying

Overview

The network provides a service to service networking abstraction that includes proxying, authentication, tenancy isolation and makes use of the existing service discovery and routing system. The goal here is not to provide service mesh but a higher level control plane for routing that can govern access based on the existing system. The network requires every service to be pointed to it, making an explicit choice for routing.

Beneath the covers cilium, envoy and other service mesh tools can be used to provide a highly resilient mesh.

Registry

The registry is a service directory and endpoint explorer

Overview

The service registry provides a single source of truth for all services and their APIs. All services on startup will register their name, version, address and endpoints with the registry service. They then periodically re-register to “heartbeat”, otherwise being expired based on a pre-defined TTL of 90 seconds.

The goal of the registry is to allow the user to explore APIs and services within a running system.

The simplest form of access is the below command to list services.

  1. micro services

Usage

The get service endpoint returns information about a service including response parameters parameters for endpoints:

  1. $ micro registry getService --service=helloworld
  2. {
  3. "services": [
  4. {
  5. "name": "helloworld",
  6. "version": "latest",
  7. "metadata": {
  8. "domain": "micro"
  9. },
  10. "endpoints": [
  11. {
  12. "name": "Helloworld.Call",
  13. "request": {
  14. "name": "CallRequest",
  15. "type": "CallRequest",
  16. "values": [
  17. {
  18. "name": "name",
  19. "type": "string",
  20. "values": []
  21. }
  22. ]
  23. },
  24. "response": {
  25. "name": "CallResponse",
  26. "type": "CallResponse",
  27. "values": [
  28. {
  29. "name": "message",
  30. "type": "string",
  31. "values": []
  32. }
  33. ]
  34. },
  35. "metadata": {}
  36. },
  37. {
  38. "name": "Helloworld.Stream",
  39. "request": {
  40. "name": "Context",
  41. "type": "Context",
  42. "values": []
  43. },
  44. "response": {
  45. "name": "Stream",
  46. "type": "Stream",
  47. "values": []
  48. },
  49. "metadata": {
  50. "stream": "true"
  51. }
  52. }
  53. ],
  54. "nodes": [
  55. {
  56. "id": "helloworld-3a0d02be-f98e-4d9d-a8fa-24e942580848",
  57. "address": "192.168.43.193:34321",
  58. "port": "0",
  59. "metadata": {
  60. "broker": "service",
  61. "protocol": "grpc",
  62. "registry": "service",
  63. "server": "grpc",
  64. "transport": "grpc"
  65. }
  66. }
  67. ],
  68. "options": {
  69. "ttl": "0",
  70. "domain": ""
  71. }
  72. }
  73. ]
  74. }

This is an especially useful feature for writing custom meta tools like API explorers.

Runtime

Overview

The runtime service is responsible for running, updating and deleting binaries or containers (depending on the platform - eg. binaries locally, pods on k8s etc) and their logs.

Running a service

The micro run command tells the runtime to run a service. The following are all valid examples:

  1. micro run github.com/micro/services/helloworld
  2. micro run . # deploy local folder to your local micro server
  3. micro run ../path/to/folder # deploy local folder to your local micro server
  4. micro run helloworld # deploy latest version, translates to micro run github.com/micro/services/helloworld or your custom base url
  5. micro run helloworld@9342934e6180 # deploy certain version
  6. micro run helloworld@branchname # deploy certain branch
  7. micro run --name helloworld .

Specifying Service Name

The service name is derived from the directory name of your application. In case you want to override this specify the --name flag.

  1. micro run --name helloworld github.com/myorg/helloworld/server

Running a local folder

If the first parameter is an existing local folder, ie

  1. micro run ./foobar

Then the CLI will upload that folder to the runtime and the runtime runs that.

Running a git source

If the first parameter to micro run points to a git repository (be it on GitHub, GitLab, Bitbucket or any other provider), then the address gets sent to the runtime and the runtime downloads the code and runs it.

Using references

References are the part of the first parameter passed to run after the @ sign. It can either be a branch name (no reference means version latest which equals to master in git terminology) or a commit hash.

When branch names are passed in, the latest commit of the code will run.

Listing runtime objects

The micro status command lists all things running in the runtime:

  1. $ micro status
  2. NAME VERSION SOURCE STATUS BUILD UPDATED METADATA
  3. helloworld latest github.com/micro/services/helloworld running n/a 20h43m45s ago owner=admin, group=micro

The output includes the error if there is one. Commands like micro kill, micro logs, micro update accept the name returned by the micro status as the first parameter (and not the service name as that might differ).

Updating a service

The micro update command makes the runtime pull the latest commit in the branch and restarts the service.

In case of local code it requires not the runtime name (returned by micro status) but the local path. For commit hash deploys it just restarts the service.

Examples: micro update helloworld, micro update helloworld@branch, micro update helloworld@commit, micro update ./helloworld.

Deleting a service

The micro kill command removes a runtime object from the runtime. It accepts the name returned by micro status.

Examples: micro kill helloworld.

Logs

The micro logs command shows logs for a runtime object. It accepts the name returned by micro status.

The -f flag makes the command stream logs continuously.

Examples: micro logs helloworld, micro logs -f helloworld.

Store

Micro’s store interface is for persistent key-value storage.

For a good beginner level doc on the Store, please see the Getting started tutorial.

Overview

Key-value stores that support ordering of keys can be used to build complex applications. Due to their very limited feature set, key-value stores generally scale easily and reliably, often linearly with the number of nodes added.

This scalability comes at the expense of inconvenience and mental overhead when writing business logic. For use cases where linear scalability is important, this trade-off is preferred.

Query by ID

Reading by ID is the archetypal job for key value stores. Storing data to enable this ID works just like in any other database:

  1. # entries designed for querying "users by id"
  2. KEY VALUE
  3. id1 {"id":"id1", "name":"Jane", "class":"firstGrade", "avgScore": 98}
  4. id2 {"id":"id2", "name":"Alice","class":"secondGrade", "avgScore": 92}
  5. id3 {"id":"id3", "name":"Joe", "class":"secondGrade" "avgScore": 89}
  6. id4 {"id":"id4", "name":"Betty","class":"thirdGrade" "avgScore": 94}
  1. import "github.com/micro/micro/v3/service/store"
  2. records, err := store.Read("id1")
  3. if err != nil {
  4. fmt.Println("Error reading from store: ", err)
  5. }
  6. fmt.Println(records[0].Value)
  7. // Will output {"id":"id1", "name":"Jane", "class":"firstGrade", "avgScore": 98}

Given this data structure, we can do two queries:

  • reading a given key (get “id1”, get “id2”)
  • if the keys are ordered, we can ask for X number of entries after a key (get 3 entries after “id2”)

Finding values in an ordered set is possibly the simplest task we can ask a database. The problem with the above data structure is that it’s not very useful to ask “find me keys coming in the order after “id2”. To enable other kinds of queries, the data must be saved with different keys.

In the case of the schoold students, let’s say we wan’t to list by class. To do this, having the query in mind, we can copy the data over to another table named after the query we want to do:

Query by Field Value Equality

  1. # entries designed for querying "users by class"
  2. KEY VALUE
  3. firstGrade/id1 {"id":"id1", "name":"Jane", "class":"firstGrade", "avgScore": 98}
  4. secondGrade/id2 {"id":"id2", "name":"Alice","class":"secondGrade", "avgScore": 92}
  5. secondGrade/id3 {"id":"id3", "name":"Joe", "class":"secondGrade" "avgScore": 89}
  6. thirdGrade/id4 {"id":"id4", "name":"Betty","class":"thirdGrade" "avgScore": 94}
  1. import "github.com/micro/micro/v3/service/store"
  2. records, err := store.Read("", store.Prefix("secondGrade"))
  3. if err != nil {
  4. fmt.Println("Error reading from store: ", err)
  5. }
  6. fmt.Println(records[0].Value)
  7. // Will output
  8. // secondGrade/id2 {"id":"id2", "name":"Alice","class":"secondGrade", "avgScore": 92}
  9. // secondGrade/id3 {"id":"id3", "name":"Joe", "class":"secondGrade" "avgScore": 89}

Since the keys are ordered it is very trivial to get back let’s say “all second graders”. Key value stores which have their keys ordered support something similar to “key starts with/key has prefix” query. In the case of second graders, listing all records where the “keys start with secondGrade” will give us back all the second graders.

This query is basically a field equals to as we essentially did a field class == secondGrade. But we could also exploit the ordered nature of the keys to do a value comparison query, ie field avgScores is less than 90 or field AvgScores is between 90 and 95 etc., if we model our data appropriately:

Query for Field Value Ranges

  1. # entries designed for querying "users by avgScore"
  2. KEY VALUE
  3. 089/id3 {"id":"id3", "name":"Joe", "class":"secondGrade" "avgScore": 89}
  4. 092/id2 {"id":"id2", "name":"Alice","class":"secondGrade", "avgScore": 92}
  5. 094/id4 {"id":"id4", "name":"Betty","class":"thirdGrade" "avgScore": 94}
  6. 098/id1 {"id":"id1", "name":"Jane", "class":"firstGrade", "avgScore": 98}

It’s worth remembering that the keys are strings, and that they are ordered lexicographically. For this reason when dealing with numbering values, we must make sure that they are prepended to the same length appropriately.

At the moment Micro’s store does not support this kind of query, this example is only here to hint at future possibilities with the store.

Tables Usage

Micro services only have access to one Store table. This means all keys live in the same namespace and can collide. A very useful pattern is to separate the entries by their intended query pattern, ie taking the “users by id” and users by class records above:

  1. KEY VALUE
  2. # entries designed for querying "users by id"
  3. usersById/id1 {"id":"id1", "name":"Jane", "class":"firstGrade", "avgScore": 98}
  4. usersById/id2 {"id":"id2", "name":"Alice","class":"secondGrade", "avgScore": 92}
  5. usersById/id3 {"id":"id3", "name":"Joe", "class":"secondGrade" "avgScore": 89}
  6. usersById/id4 {"id":"id4", "name":"Betty","class":"thirdGrade" "avgScore": 94}
  7. # entries designed for querying "users by class"
  8. usersByClass/firstGrade/id1 {"id":"id1", "name":"Jane", "class":"firstGrade", "avgScore": 98}
  9. usersByClass/secondGrade/id2 {"id":"id2", "name":"Alice","class":"secondGrade", "avgScore": 92}
  10. usersByClass/secondGrade/id3 {"id":"id3", "name":"Joe", "class":"secondGrade" "avgScore": 89}
  11. usersByClass/thirdGrade/id4 {"id":"id4", "name":"Betty","class":"thirdGrade" "avgScore": 94}

Respective go examples this way become:

  1. import "github.com/micro/micro/v3/service/store"
  2. const idPrefix = "usersById/"
  3. records, err := store.Read(idPrefix + "id1")
  4. if err != nil {
  5. fmt.Println("Error reading from store: ", err)
  6. }
  7. fmt.Println(records[0].Value)
  8. // Will output {"id":"id1", "name":"Jane", "class":"firstGrade", "avgScore": 98}
  1. import "github.com/micro/micro/v3/service/store"
  2. const classPrefix = "usersByClass/"
  3. records, err := store.Read("", store.Prefix(classPrefix + "secondGrade"))
  4. if err != nil {
  5. fmt.Println("Error reading from store: ", err)
  6. }
  7. fmt.Println(records[0].Value)
  8. // Will output
  9. // secondGrade/id2 {"id":"id2", "name":"Alice","class":"secondGrade", "avgScore": 92}
  10. // secondGrade/id3 {"id":"id3", "name":"Joe", "class":"secondGrade" "avgScore": 89}

Metadata

Metadata / headers can be passed via the context in RPC calls. The context/metadata package allows services to get and set metadata in a context. The Micro API will add request headers into context, for example if the “Foobar” header is set on an API call to “localhost:8080/users/List”, the users service can access this value as follows:

  1. import (
  2. "context"
  3. "github.com/micro/micro/v3/service/context/metadata"
  4. )
  5. ...
  6. func (u *Users) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResponse) error {
  7. val, ok := metadata.Get(ctx, "Foobar")
  8. if !ok {
  9. return fmt.Errorf("Missing Foobar header")
  10. }
  11. fmt.Println("Foobar header was set to: %v", val)
  12. return nil
  13. }

Likewise, clients can set metadata in context using the metadata.Set function as follows:

  1. func (u *Users) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResponse) error {
  2. newCtx := metadata.Set(ctx, "Foobar", "mycustomval")
  3. fRsp, err := u.foosrv.Call(newCtx, &foosrv.Request{})
  4. ...
  5. }

Plugins

Micro is a pluggable architecture built on Go’s interface types. Plugins enable swapping out underlying infrastructure.

Overview

Micro is pluggable, meaning the implementation for each module can be replaced depending on the requirements. Plugins are applied to the micro server and not to services directly, this is done so the underlying infrastructure can change with zero code changes required in your services.

An example of a pluggable interface is the store. Locally micro will use a filestore to persist data, this is great because it requires zero dependencies and still offers persistence between restarts. When running micro in a test suite, this could be swapped to an in-memory cache which is better suited as it offers consistency between runs. In production, this can be swapped out for standalone infrastructure such as cockroachdb or etcd depending on the requirement.

Let’s take an example where our service wants to load data from the store. Our service would call store.Read(userPrefix + userID) to load the value, behind the scenes this will execute an RPC to the store service which will in-turn call store.Read on the current DefaultStore implementation configured for the server.

Profiles

Profiles are used to configure multiple plugins at once. Micro comes with a few profiles out the box, such as “local”, “kubernetes” and “test”. These profiles can be found in profile/profile.go. You can configure micro to use one of these profiles using the MICRO_PROFILE env var, for example: MICRO_PROFILE=test micro server. The default profile used is “local”.

Writing a profile

Profiles should be created as packages within the profile directory. Let’s create a “staging” profile by creating profile/staging/staging.go. The example below shows how to override the default store of Local profile to use an in-memory implementation:

  1. // Package staging configures micro for a staging environment
  2. package staging
  3. import (
  4. "github.com/urfave/cli/v2"
  5. "github.com/micro/micro/v3/profile"
  6. "github.com/micro/micro/v3/service/store"
  7. "github.com/micro/micro/v3/service/store/memory"
  8. )
  9. func init() {
  10. profile.Register("staging", staging)
  11. }
  12. var staging = &profile.Profile{
  13. Name: "staging",
  14. Setup: func(ctx *cli.Context) error {
  15. profile.Local.Setup(ctx)
  16. store.DefaultStore = memory.NewStore()
  17. return nil
  18. },
  19. }
  1. pushd profile/staging
  2. go mod init github.com/micro/micro/profile/staging
  3. go mod tidy
  4. popd

Using a custom profile

You can load a custom profile using a couple of commands, the first adds a replace to your go mod, indicating it should look for your custom profile within the profile directory:

  1. go mod edit -replace github.com/micro/micro/profile/staging/v3=./profile/staging
  2. go mod tidy

The second command creates a profile.go file which imports your profile. When your profile is imported, the init() function which is defined in staging.go is called, registering your profile.

  1. micro init --profile=staging --output=profile.go

Now you can start your server using this profile:

  1. MICRO_PROFILE=staging go run . server

This site is open source. Improve this page.