By using a DefaultHealthChecker - as with a DefaultName, DefaultServer, ... - it is possible to overwrite the default implementation. This means we can now do extra actions in our health check that is necessary for the applications specific needs.
Go Micro
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 |
monitoring-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