Go to file
2016-02-03 17:20:32 +00:00
broker Add broker comment 2016-01-30 21:18:57 +00:00
client Add client comment 2016-01-30 21:15:08 +00:00
cmd Allow setting registry address, broker address and transport address without specifying registry flag 2016-01-27 01:13:31 +00:00
codec Nitpick Headers to Header 2016-01-28 18:11:13 +00:00
errors Add error comment 2016-01-30 21:16:45 +00:00
examples Move context to metadata 2016-01-28 17:55:28 +00:00
metadata Add metadata comment 2016-01-30 21:17:44 +00:00
registry Add some comments 2016-01-30 21:13:34 +00:00
selector Replace map[string]string with Context for extra options. map[string]string is essentially useless. Context can store anything 2016-01-06 16:25:12 +00:00
server Merge pull request #45 from micro/metadata 2016-01-28 18:30:53 +00:00
transport Add transport comment 2016-01-30 21:19:55 +00:00
.travis.yml Travis integration 2015-12-09 19:59:52 +00:00
go-micro.go FromContext/NewContext methods 2016-01-28 18:23:24 +00:00
LICENSE Add apache license 2015-02-27 09:38:47 +00:00
options.go Prefer RegisterTTL set through Init 2016-01-27 12:23:18 +00:00
README.md Rename monitor 2016-02-03 17:20:32 +00:00
service.go Move context to metadata 2016-01-28 17:55:28 +00:00
wrapper.go Move context to metadata 2016-01-28 17:55:28 +00:00

Go Micro GoDoc Travis CI

Go Micro is a pluggable RPC based microservice library which provides the fundamental building blocks for writing distributed applications. It is part of the Micro toolchain. It supports Proto-RPC and JSON-RPC as the request/response protocol out of the box and defaults to Consul for discovery.

Every aspect of go-micro is pluggable.

An example service can be found in examples/service. The examples directory contains many more examples for using things such as middleware/wrappers, selector filters, pub/sub and code generation.

Features

Feature Package Built-in Plugin Description
Discovery Registry consul A way of locating services to communicate with
Client Client rpc Used to make RPC requests to a service
Codec Codec proto,json Encoding/Decoding handler for requests
Balancer Selector random Service node filter and pool
Server Server rpc Listens and serves RPC requests
Pub/Sub Broker http Publish and Subscribe to events
Transport Transport http Communication mechanism between services

Example Services

Project Description
greeter A greeter service (includes Go, Ruby, Python examples)
geo-srv Geolocation tracking service using hailocab/go-geoindex
geo-api A HTTP API handler for geo location tracking and search
discovery-srv A discovery in the micro platform
geocode-srv A geocoding service using the Google Geocoding API
hailo-srv A service for the hailo taxi service developer api
monitor-srv A monitoring service for Micro services
place-srv A microservice to store and retrieve places (includes Google Place Search API)
slack-srv The slack bot API as a go-micro RPC service
trace-srv A distributed tracing microservice in the realm of dapper, zipkin, etc
twitter-srv A microservice for the twitter API
user-srv A microservice for user management and authentication

Go Plugins

By default go-micro only provides a single implementation of each interface. Plugins can be found at github.com/micro/go-plugins. Contributions welcome!

Prerequisites

Consul is the default discovery mechanism provided in go-micro. Discovery is however pluggable so you can used etcd, kubernetes, zookeeper, etc.

Install Consul

https://www.consul.io/intro/getting-started/install.html

Getting Started

Run Consul

$ consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul

Run Service

$ go run examples/service/main.go --logtostderr
I0102 00:22:26.413467   12018 rpc_server.go:297] Listening on [::]:62492
I0102 00:22:26.413803   12018 http_broker.go:115] Broker Listening on [::]:62493
I0102 00:22:26.414009   12018 rpc_server.go:212] Registering node: greeter-e6b2fc6f-b0e6-11e5-a42f-68a86d0d36b6

Test Service

$ go run examples/service/main.go --client
Hello John

Writing a service

Create request/response proto

go-micro/examples/service/proto/greeter.proto:

syntax = "proto3";

service Greeter {
	rpc Hello(HelloRequest) returns (HelloResponse) {}
}

message HelloRequest {
	string name = 1;
}

message HelloResponse {
	string greeting = 2;
}

Install protobuf for code generation

We use a protobuf plugin for code generation. This is completely optional. Look at examples/server and examples/client for examples without code generation.

go get github.com/micro/protobuf

Compile proto protoc -I$GOPATH/src --go_out=plugins=micro:$GOPATH/src $GOPATH/src/github.com/micro/go-micro/examples/service/proto/greeter.proto

Define the service

go-micro/examples/service/main.go:

package main

import (
	"fmt"

	micro "github.com/micro/go-micro"
	proto "github.com/micro/go-micro/examples/service/proto"
	"golang.org/x/net/context"
)

type Greeter struct{}

func (g *Greeter) Hello(ctx context.Context, req *proto.HelloRequest, rsp *proto.HelloResponse) error {
	rsp.Greeting = "Hello " + req.Name
	return nil
}

func main() {
	// Create a new service. Optionally include some options here.
	service := micro.NewService(
		micro.Name("greeter"),
		micro.Version("latest"),
		micro.Metadata(map[string]string{
			"type": "helloworld",
		}),
	)

	// Init will parse the command line flags. Any flags set will
	// override the above settings. Options defined here will
	// override anything set on the command line.
	service.Init()

	// Register handler
	proto.RegisterGreeterHandler(service.Server(), new(Greeter))

	// Run the server
	if err := service.Run(); err != nil {
		fmt.Println(err)
	}
}

Run service

go run examples/service/main.go --logtostderr
I0102 00:22:26.413467   12018 rpc_server.go:297] Listening on [::]:62492
I0102 00:22:26.413803   12018 http_broker.go:115] Broker Listening on [::]:62493
I0102 00:22:26.414009   12018 rpc_server.go:212] Registering node: greeter-e6b2fc6f-b0e6-11e5-a42f-68a86d0d36b6

Define a client

client.go

package main

import (
	"fmt"

	micro "github.com/micro/go-micro"
	proto "github.com/micro/go-micro/examples/service/proto"
	"golang.org/x/net/context"
)


func main() {
	// Create a new service. Optionally include some options here.
	service := micro.NewService(micro.Name("greeter.client"))

	// Create new greeter client
	greeter := proto.NewGreeterClient("greeter", service.Client())

	// Call the greeter
	rsp, err := greeter.Hello(context.TODO(), &proto.HelloRequest{Name: "John"})
	if err != nil {
		fmt.Println(err)
	}

	// Print response
	fmt.Println(rsp.Greeting)
}

Run the client

go run client.go
Hello John