1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00
A Go microservices framework
Go to file
2016-07-03 02:05:19 +01:00
broker add http broker benchmark so we can test codec changes 2016-06-25 03:15:56 +01:00
client request panic catch all 2016-06-30 16:19:02 +01:00
cmd Change help messages 2016-06-28 22:41:44 +01:00
codec Don't unmarshal if result is nil 2016-06-30 16:45:30 +01:00
errors Add package comments 2016-05-24 22:22:46 +01:00
examples Add readme to examples 2016-05-11 22:58:59 +01:00
metadata Add package comments 2016-05-24 22:22:46 +01:00
registry return error when zero services in mock registry 2016-06-30 20:11:04 +01:00
selector Strip blacklist 2016-06-19 14:41:33 +01:00
server Merge branch 'master' into pool 2016-06-06 14:06:03 +01:00
transport Add some comments 2016-06-06 14:05:02 +01:00
.travis.yml Update go versions 2016-03-23 18:34:20 +00:00
go-micro.go Add comments 2016-06-19 16:02:14 +01:00
go-micro.png update image 2016-04-22 10:34:32 +01:00
LICENSE Add apache license 2015-02-27 09:38:47 +00:00
options.go That should be i-1 2016-04-19 00:47:15 +01:00
README.md update mailing list link 2016-07-03 02:05:19 +01:00
service.go Make it possible to call service.Init multiple times 2016-05-06 12:21:41 +01:00
wrapper_test.go Appending to metadata is a side effect. Let's not do that anymore. 2016-04-15 16:45:59 +01:00
wrapper.go Appending to metadata is a side effect. Let's not do that anymore. 2016-04-15 16:45:59 +01:00

Go Micro License GoDoc Travis CI Go Report Card

Go Micro is a pluggable RPC framework for microservices. It provides the fundamental building blocks for writing distributed systems. It is part of Micro, the microservice toolkit.

The Micro philosophy is sane defaults with a pluggable architecture. We provide defaults to get you started quickly but everything can be easily swapped out. It comes with built in support for {json,proto}-rpc encoding, consul or multicast dns for service discovery, http for communication and random hashed client side load balancing.

Everything in go-micro is pluggable. You can find and contribute to plugins at github.com/micro/go-plugins.

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.

For the complete greeter example look at micro/examples/greeter.

Check out the blog post to learn how to write go-micro services https://blog.micro.mu/2016/03/28/go-micro.html.

Join the community to learn more:

Features

Feature Package Built-in Plugin Description
Discovery Registry consul,mdns Service discovery mechanism
Codec Codec proto,json Message encoding
Balancer Selector random Client side load balancing
Pub/Sub Broker http Asynchonous communication
Transport Transport http Synchronous communication
Client Client rpc RPC Client
Server Server rpc RPC Server

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
geocode-srv A geocoding service using the Google Geocoding API
hailo-srv A service for the hailo taxi service developer api
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
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!

How does it work?

Go Micro is a framework that addresses the fundamental requirements to write microservices.

Let's dig into the core components.

Registry

The registry provides a service discovery mechanism to resolve names to addresses. It can be backed by consul, etcd, zookeeper, dns, gossip, etc. Services should register using the registry on startup and deregister on shutdown. Services can optionally provide an expiry TTL and reregister on an interval to ensure liveness and that the service is cleaned up if it dies.

Selector

The selector is a load balancing abstraction which builds on the registry. It allows services to be "filtered" using filter functions and "selected" using a choice of algorithms such as random, roundrobin, leastconn, etc. The selector is leveraged by the Client when making requests. The client will use the selector rather than the registry as it provides that built in mechanism of load balancing.

Transport

The transport is the interface for synchronous request/response communication between services. It's akin to the golang net package but provides a higher level abstraction which allows us to switch out communication mechanisms e.g http, rabbitmq, websockets, NATS. The transport also supports bidirectional streaming. This is powerful for client side push to the server.

Broker

The broker provides an interface to a message broker for asynchronous pub/sub communication. This is one of the fundamental requirements of an event driven architecture and microservices. By default we use an inbox style point to point HTTP system to minimise the number of dependencies required to get started. However there are many message broker implementations available in go-plugins e.g RabbitMQ, NATS, NSQ, Google Cloud Pub Sub.

Codec

The codec is used for encoding and decoding messages before transporting them across the wire. This could be json, protobuf, bson, msgpack, etc. Where this differs from most other codecs is that we actually support the RPC format here as well. So we have JSON-RPC, PROTO-RPC, BSON-RPC, etc. It separates encoding from the client/server and provides a powerful method for integrating other systems such as gRPC, Vanadium, etc.

Server

The server is the building block for writing a service. Here you can name your service, register request handlers, add middeware, etc. The service builds on the above packages to provide a unified interface for serving requests. The built in server is an RPC system. In the future there maybe other implementations. The server also allows you to define multiple codecs to serve different encoded messages.

Client

The client provides an interface to make requests to services. Again like the server, it builds on the other packages to provide a unified interface for finding services by name using the registry, load balancing using the selector, making synchronous requests with the transport and asynchronous messaging using the broker.

The above components are combined at the top-level of micro as a Service.

Getting Started

This is a quick getting started guide with the greeter service example.

Prerequisites

There's just one prerequisite. We need a service discovery system to resolve service names to their address. The default discovery mechanism used in go-micro is Consul. Discovery is however pluggable so you can used etcd, kubernetes, zookeeper, etc. Other implementations can be found in go-plugins.

Alternatively we can use multicast DNS with the built in MDNS registry for a zero dependency configuration. Just pass --registry=mdns to the below commands.

Install Consul

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

Run Consul

$ consul agent -dev -advertise=127.0.0.1

Run Service

$ go run examples/service/main.go
2016/03/14 10:59:14 Listening on [::]:50137
2016/03/14 10:59:14 Broker Listening on [::]:50138
2016/03/14 10:59:14 Registering node: greeter-ca62b017-e9d3-11e5-9bbb-68a86d0d36b6

Test Service

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

Writing a service

Create request/response proto

One of the key requirements of microservices is strongly defined interfaces so we utilised protobuf to define the handler and request/response. Here's a definition for the Greeter handler with the method Hello which takes a HelloRequest and HelloResponse both with one string arguments.

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/{proto,protoc-gen-go}

There's still a need for proto compiler to generate Go stub code from our proto file. You can either use the micro fork above or the official repo github.com/golang/protobuf.

Compile the protobuf file

`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

Below is the code sample for the Greeter service. It basically implements the interface defined above for the Greeter handler, initialises the service, registers the handler and then runs itself. Simple as that.

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
2016/03/14 10:59:14 Listening on [::]:50137
2016/03/14 10:59:14 Broker Listening on [::]:50138
2016/03/14 10:59:14 Registering node: greeter-ca62b017-e9d3-11e5-9bbb-68a86d0d36b6

Define a client

Below is the client code to query the greeter service. Notice we're using the code generated client interface proto.NewGreeterClient. This reduces the amount of boiler plate code we need to write. The greeter client can be reused throughout the code if need be.

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

Sponsors

Next steps

Contributing