micro.dev

module
v4.0.0-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2023 License: Apache-2.0

README

Micro

Go Report Card Go.Dev reference Apache License DigitalOcean Droplet

Micro is an API first development platform. It addresses the core requirements for building apps in the cloud by providing a set of services which act as the building blocks of any platform. Micro deals with the complexity of distributed systems and provides simpler programmable abstractions to build on.

Overview

Architecture

Below are the core components that make up Micro.

Server

Micro is built as a microkernel architecture. It abstracts away the complexity of the underlying infrastructure by providing a set of building block services composed as a single logical server for the end user to consume.

The server consists of the following services.

  • Auth - Authentication and authorization out of the box using JWT tokens and rule based access control.
  • Broker - Ephemeral pubsub messaging for asynchronous communication and distributing notifications
  • Config - Dynamic configuration and secrets management for service level config without reload
  • Events - Event streaming with ordered messaging, replay from offsets and persistent storage
  • Network - service-to-service networking and control plane for all internal request traffic
  • Runtime - Service lifecycle and process management with support for source to running auto build
  • Registry - Centralised service discovery and API endpoint explorer with feature rich metadata
  • Store - Key-Value storage with TTL expiry and persistent crud to keep microservices stateless
API

The server embeds a HTTP API gateway (on port 8080) which can be used to make requests as simple JSON. The API automatically maps HTTP Paths and POST requests to internal RPC service names and endpoints.

Proxy

Additionally there's a gRPC proxy (on port 8081) which used to make requests via the CLI or externally. The proxy is identity aware which means it can be used to gatekeep remote access to Micro running anywhere.

Framework

Micro comes with a built in Go framework for service based development. The framework makes it drop dead simple to write your services without having to piece together endless lines of boilerplate code. Configured and initialised by default, just import it and get started.

Command Line

The command line interface includes dynamic command mapping for all services running on the platform. It turns any service instantly into a CLI command along with flag parsing for inputs. Includes support for environments, namespaces, creating and running services, status info and logs.

Environments

Micro bakes in the concept of Environments and multi-tenancy through Namespaces. Run your server locally for development and in the cloud for staging and production, seamlessly switch between them using the CLI commands micro env set [environment] and micro user set [namespace].

Install

From Source
make build
Prebuilt Binaries
Windows
powershell -Command "iwr -useb https://raw.githubusercontent.com/micro/micro/master/scripts/install.ps1 | iex"

Linux
wget -q  https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh -O - | /bin/bash
MacOS
curl -fsSL https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh | /bin/bash
Run the server

The server starts with a single command ready to use

micro server

Now go to localhost:8080 and make sure the output is something like {"version": "v3.10.1"}.

Usage

Set the environment e.g local

micro env set local
Login to Micro

Default username/password: admin/micro

$ micro login
Enter username: admin
Enter password:
Successfully logged in.

See what's running:

$ micro services
auth
broker
config
events
network
registry
runtime
store
Create a Service

Generate a service using the template

micro new helloworld

Output

Creating service helloworld

.
├── main.go
├── handler
│   └── helloworld.go
├── proto
│   └── helloworld.proto
├── Makefile
├── README.md
├── .gitignore
└── go.mod


download protoc zip packages (protoc-$VERSION-$PLATFORM.zip) and install:

visit https://github.com/protocolbuffers/protobuf/releases

compile the proto file helloworld.proto:

cd helloworld
make init
go mod vendor
make proto
Making changes

Edit the protobuf definition in proto/helloworld.proto and run make proto to recompile

Go to handler/helloworld.go to make changes to the response handler

type Helloworld struct{}

func New() *Helloworld {
        return &Helloworld{}
}

func (h *Helloworld) Call(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
        rsp.Msg = "Hello " + req.Name
        return nil
}
Run the service

Run from local dir

micro run .

Or from a git url

micro run github.com/micro/services/helloworld
Check service status
$ micro status
NAME		VERSION	SOURCE					STATUS	BUILD	UPDATED	METADATA
helloworld	latest	github.com/micro/services/helloworld	running	n/a	4s ago	owner=admin, group=micro
View service logs
$ micro logs helloworld
2020-10-06 17:52:21  file=service/service.go:195 level=info Starting [service] helloworld
2020-10-06 17:52:21  file=grpc/grpc.go:902 level=info Server [grpc] Listening on [::]:33975
2020-10-06 17:52:21  file=grpc/grpc.go:732 level=info Registry [service] Registering node: helloworld-67627b23-3336-4b92-a032-09d8d13ecf95
Call via CLI
$ micro helloworld call --name=Jane
{
	"msg": "Hello Jane"
}
Call via API
curl "http://localhost:8080/helloworld/Call?name=John"
Call via RPC

An RPC client is used within a service and must be run by micro

package main

import (
	"context"
	"fmt"
	"time"

	"micro.dev/v4/service"
	pb "github.com/micro/services/helloworld/proto"
)

func callService(hw pb.HelloworldService) {
	for {
		// call an endpoint on the service
		rsp, err := hw.Call(context.Background(), &pb.CallRequest{
			Name: "John",
		})
		if err != nil {
			fmt.Println("Error calling helloworld: ", err)
			return
		}

		// print the response
		fmt.Println("Response: ", rsp.Message)

		time.Sleep(time.Second)
	}
}

func main() {
	// create and initialise a new service
	srv := service.New(
		service.Name("caller"),
	)

	// new helloworld client
	hw := pb.NewHelloworldService("helloworld", srv.Client())
	
	// run the client caller
	go callService(hw)
	
	// run the service
	service.Run()
}

Run it

micro run .
Call via Go

Get your user token

export TOKEN=`micro user token`

Call helloworld

package main

import (
    "fmt"
    "os"

    "github.com/micro/micro-go"
)

type Request struct {
	Name string `json:"name"`
}

type Response struct {
	Msg string `json:"msg"`
}

func main() {
	token := os.Getenv("TOKEN")
	c := micro.NewClient(nil)

	// set your api token
	c.SetToken(token)

   	req := &Request{
		Name: "John",
	}
	
	var rsp Response

	if err := c.Call("helloworld", "Call", req, &rsp); err != nil {
		fmt.Println(err)
		return
	}
	
	fmt.Println(rsp)
}

Run it

go run main.go
Call via JS
const micro = require('micro-js-client');

new micro.Client({ token: process.env.TOKEN })
  .call('helloworld', 'Call', {"name": "Alice"})
  .then((response) => {
    console.log(response);
  });

Get Started

For more see the getting started guide.

Micro Web

Use services via the web with the Micro Web dashboard

micro web

Browse to localhost:8082

Dev Env

1 click deploy a Micro Dev environment on a DigitalOcean Droplet

Use our refcode so we get $25 credit too!

Directories

Path Synopsis
api module
client module
proto module
proto/api module
auth module
jwt module
broker module
client module
grpc module
router module
selector module
cmd
cli
Package cli is a command line interface
Package cli is a command line interface
cli/gen
Package gen provides the micro gen command which simply runs go generate
Package gen provides the micro gen command which simply runs go generate
cli/init
Package init provides the micro init command for initialising plugins and imports
Package init provides the micro init command for initialising plugins and imports
cli/new
Package new generates micro service templates
Package new generates micro service templates
cli/run
Package runtime is the micro runtime
Package runtime is the micro runtime
cli/shutdown
Package shutdown will issue a shutdown signal
Package shutdown will issue a shutdown signal
cli/store
Package cli implements the `micro store` subcommands for example:
Package cli implements the `micro store` subcommands for example:
cli/token
Package token contains CLI client token related helpers tToken files consist of one line per token, each token having the structure of `micro://envAddress/namespace[/id]:token`, ie.
Package token contains CLI client token related helpers tToken files consist of one line per token, each token having the structure of `micro://envAddress/namespace[/id]:token`, ie.
cli/user
Package user handles the user cli command
Package user handles the user cli command
cli/util
Package cliutil contains methods used across all cli commands @todo: get rid of os.Exits and use errors instread
Package cliutil contains methods used across all cli commands @todo: get rid of os.Exits and use errors instread
micro command
protoc-gen-micro command
protoc-gen-micro is a plugin for the Google protocol buffer compiler to generate Go code.
protoc-gen-micro is a plugin for the Google protocol buffer compiler to generate Go code.
protoc-gen-micro/generator
The code generator for the plugin for the Google protocol buffer compiler.
The code generator for the plugin for the Google protocol buffer compiler.
server
Package server is the micro server which runs the whole system
Package server is the micro server which runs the whole system
web
Package web is a web dashboard
Package web is a web dashboard
client module
client/auth module
client/config module
client/gen module
client/init module
client/new module
client/run module
client/store module
client/token module
client/user module
client/util module
go-micro module
pkg module
pkg/service module
config module
source module
source/file module
debug module
log module
errors module
examples module
go-micro module
logger module
metadata module
micro module
go-micro module
pkg module
service module
service/proto module
plugin module
registry module
registry/etcd module
plugins module
selector module
server module
server/http module
v4/broker module
proto module
api module
network module
router module
registry module
etcd module
selector module
server module
grpc module
logger module
service module
api
api/handler
Package handler provides http handlers
Package handler provides http handlers
api/handler/rpc
Package rpc is a go-micro rpc handler.
Package rpc is a go-micro rpc handler.
api/router
Package router provides api service routing
Package router provides api service routing
api/router/registry
Package registry provides a dynamic api service router
Package registry provides a dynamic api service router
api/server/http
Package http provides a http server with features; acme, cors, etc
Package http provides a http server with features; acme, cors, etc
auth/jwt
Package jwt is a jwt implementation of the auth interface
Package jwt is a jwt implementation of the auth interface
broker
Package broker is the micro broker
Package broker is the micro broker
broker/memory
Package memory provides a memory broker
Package memory provides a memory broker
client
Package client is an interface for an RPC client
Package client is an interface for an RPC client
client/grpc
Package grpc provides a gRPC client
Package grpc provides a gRPC client
config
Package config is an interface for dynamic configuration.
Package config is an interface for dynamic configuration.
config/env
Package env provides config from environment variables
Package env provides config from environment variables
context
Package context provides a context for accessing services
Package context provides a context for accessing services
context/metadata
Package metadata is a way of defining message headers
Package metadata is a way of defining message headers
errors
Package errors provides a way to return detailed information for an RPC request error.
Package errors provides a way to return detailed information for an RPC request error.
events
Package events is for event streaming and storage
Package events is for event streaming and storage
logger
Package log provides a log interface
Package log provides a log interface
model
Package model implements convenience methods for managing indexes on top of the Store.
Package model implements convenience methods for managing indexes on top of the Store.
model/sql
Package sql is for a sql database model
Package sql is for a sql database model
profile
Package profile is for specific profiles @todo this package is the definition of cruft and should be rewritten in a more elegant way
Package profile is for specific profiles @todo this package is the definition of cruft and should be rewritten in a more elegant way
registry
Package registry is the micro registry
Package registry is the micro registry
registry/cache
Package cache provides a registry cache
Package cache provides a registry cache
registry/memory
Package memory provides an in-memory registry
Package memory provides an in-memory registry
registry/noop
Package noop is a registry which does nothing
Package noop is a registry which does nothing
router/kubernetes
Package kubernetes is a kubernetes router which uses the service name and network to route
Package kubernetes is a kubernetes router which uses the service name and network to route
runtime
Package runtime is a service runtime manager
Package runtime is a service runtime manager
runtime/build/util/tar
Package tar is a wrapper around archive/tar, it's used for archiving and unarchiving source from and into folders on the host.
Package tar is a wrapper around archive/tar, it's used for archiving and unarchiving source from and into folders on the host.
runtime/build/util/zip
Package zip is a wrapper around archive/zip, it's used for archiving and unarchiving source from and into folders on the host.
Package zip is a wrapper around archive/zip, it's used for archiving and unarchiving source from and into folders on the host.
runtime/local/process
Package process executes a binary
Package process executes a binary
runtime/local/process/os
Package os runs processes locally
Package os runs processes locally
runtime/local/test/bar command
Package main is used to test the local runtime, specifically the entrypoint function
Package main is used to test the local runtime, specifically the entrypoint function
runtime/local/test/foo/cmd/bar command
Package main is used to test the local runtime, specifically the entrypoint function
Package main is used to test the local runtime, specifically the entrypoint function
runtime/local/test/foo/cmd/baz command
Package main is used to test the local runtime, specifically the entrypoint function
Package main is used to test the local runtime, specifically the entrypoint function
runtime/local/test/qux/cmd/test command
Package main is used to test the local runtime, specifically the entrypoint function
Package main is used to test the local runtime, specifically the entrypoint function
runtime/source
Package source retrieves source code
Package source retrieves source code
runtime/source/go
Package golang is a source for Go
Package golang is a source for Go
server/grpc
Package grpc provides a grpc server
Package grpc provides a grpc server
store
Package store is an interface for distributed data storage.
Package store is an interface for distributed data storage.
store/file
Package local is a file system backed store
Package local is a file system backed store
store/memory
Package memory is a in-memory store store
Package memory is a in-memory store store
auth/client module
broker/client module
cmd module
codec module
codec/json module
config/client module
events/client module
events/stream module
grpc module
metadata module
pkg module
pkg/cache module
pkg/service module
pkg/tracing module
proxy module
registry/etcd module
services module
services/pkg module
store/client module
time module
time/handler module
time/proto module
web module
servicecodec module
json module
services module
helloworld module
pkg module
pkg/gorm module
pkg/tenant module
pkg/tracing module
store module
file module
fakes
Code generated by counterfeiter.
Code generated by counterfeiter.
transport module
kafka module
util module
backoff
Package backoff provides backoff functionality
Package backoff provides backoff functionality
buf
codec
Package codec is an interface for encoding messages
Package codec is an interface for encoding messages
codec/bytes
Package bytes provides a bytes codec which does not encode or decode anything
Package bytes provides a bytes codec which does not encode or decode anything
codec/grpc
Package grpc provides a grpc codec
Package grpc provides a grpc codec
codec/json
Package json provides a json codec
Package json provides a json codec
codec/jsonrpc
Package jsonrpc provides a json-rpc 1.0 codec
Package jsonrpc provides a json-rpc 1.0 codec
codec/proto
Package proto provides a proto codec
Package proto provides a proto codec
codec/protorpc
Protorpc provides a net/rpc proto-rpc codec.
Protorpc provides a net/rpc proto-rpc codec.
codec/text
Package text reads any text/* content-type
Package text reads any text/* content-type
config
Package config contains helper methods for client side config management (`~/.micro/config.json` file).
Package config contains helper methods for client side config management (`~/.micro/config.json` file).
ctx
muxer
Package muxer provides proxy muxing
Package muxer provides proxy muxing
net
proxy
Package proxy is a transparent proxy built on the go-micro/server
Package proxy is a transparent proxy built on the go-micro/server
proxy/grpc
Package grpc is a grpc proxy built for the go-micro/server
Package grpc is a grpc proxy built for the go-micro/server
proxy/http
Package http provides a micro rpc to http proxy
Package http provides a micro rpc to http proxy
qson
Package qson implmenets decoding of URL query params into JSON and Go values (using JSON struct tags).
Package qson implmenets decoding of URL query params into JSON and Go values (using JSON struct tags).
ring
Package ring provides a simple ring buffer for storing local data
Package ring provides a simple ring buffer for storing local data
selector
Package selector is for node selection and load balancing
Package selector is for node selection and load balancing
sync
Package sync is an interface for distributed synchronization
Package sync is an interface for distributed synchronization
sync/memory
Package memory provides a sync.Mutex implementation of the lock for local use
Package memory provides a sync.Mutex implementation of the lock for local use
tls
opentelemetry module
v4
cmd module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL