micro.dev

module
v4.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0

README

Micro

Go Report Card Go.Dev reference Apache License

Micro is a Go service development platform. It addresses the core requirements for building services in the cloud by providing a set of APIs which act as the building blocks of any platform. Micro deals with the complexity of distributed systems and provides simpler programmable abstractions for developers 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 via an api, cli or sdks.

API

The server embeds a HTTP API (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.

Go SDK

Micro comes with a built in Go framework for service based development. The framework lets you write services without piecing together endless lines of boilerplate code. Configured and initialised by default, import it and get started.

CLI

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. Run your server locally for development and in the cloud for production, seamlessly switch between them using the CLI command micro env set [environment].

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
Edit the code

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 SDK

A proto SDK 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 JavaScript
const micro = require('micro-js-client');

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

Learn More

See the getting started guide to learn more.

Cloud Environment

1 click deploy Micro on DigitalOcean

Directories

Path Synopsis
api module
client Module
client module
grpc Module
router Module
selector Module
cmd
client
Package cli is a command line interface
Package cli is a command line interface
client/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
client/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
client/new
Package new generates micro service templates
Package new generates micro service templates
client/run
Package runtime is the micro runtime
Package runtime is the micro runtime
client/shutdown
Package shutdown will issue a shutdown signal
Package shutdown will issue a shutdown signal
client/store
Package cli implements the `micro store` subcommands for example:
Package cli implements the `micro store` subcommands for example:
client/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.
client/user
Package user handles the user cli command
Package user handles the user cli command
client/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
protoc-gen-micro
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
cli Module
go-micro Module
pkg Module
pkg/service Module
logger module
metadata module
micro module
go-micro Module
pkg module
service Module
service/proto Module
plugins module
selector Module
server Module
server/http Module
proto module
api Module
network Module
router Module
registry module
etcd Module
server 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
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 for managing data access
Package model for managing data access
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
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
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
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
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
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
api/server Module
events/stream Module
grpc Module
pkg Module
pkg/cache Module
pkg/service Module
pkg/tracing Module
proxy Module
services Module
services/pkg 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.
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