Go to file
2016-01-02 00:38:57 +00:00
broker Add extra options to be used by others that need them 2015-12-31 18:14:40 +00:00
client Update options to be public. This means people can implement the interfaces and actually use the options 2015-12-31 18:11:46 +00:00
cmd Make command internal 2016-01-02 00:38:57 +00:00
codec Some nitpicking 2015-12-02 01:38:56 +00:00
context don't just overwrite the context metadata, append if already exists 2015-11-26 20:35:50 +00:00
errors Fixes nil pointer dereference error 2015-01-30 16:03:17 +01:00
examples Make command internal 2016-01-02 00:38:57 +00:00
registry Add extra options to be used by others that need them 2015-12-31 18:14:40 +00:00
selector Fix the selectors 2016-01-01 01:29:40 +00:00
server Make command internal 2016-01-02 00:38:57 +00:00
transport Add extra options to be used by others that need them 2015-12-31 18:14:40 +00:00
.travis.yml Travis integration 2015-12-09 19:59:52 +00:00
go-micro.go Update top level init 2016-01-01 01:16:21 +00:00
LICENSE Add apache license 2015-02-27 09:38:47 +00:00
options.go Make command internal 2016-01-02 00:38:57 +00:00
README.md Make command internal 2016-01-02 00:38:57 +00:00
service.go Make command internal 2016-01-02 00:38:57 +00:00
wrapper.go rename wrap 2015-12-23 00:02:42 +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 server can be found in examples/server.

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

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