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
2015-05-09 00:42:07 +01:00
broker Fixup import paths 2015-05-05 19:05:06 +01:00
client Fixup import paths 2015-05-05 19:05:06 +01:00
cmd Fixup import paths 2015-05-05 19:05:06 +01:00
errors Fixes nil pointer dereference error 2015-01-30 16:03:17 +01:00
examples update example to use proto3 2015-05-09 00:42:07 +01:00
registry Add support for listing services, very rough 2015-04-03 00:52:49 +02:00
server Fixup import paths 2015-05-05 19:05:06 +01:00
store In memory store. Should have been here from the beginning. 2015-03-06 19:08:55 +00:00
template update example to use proto3 2015-05-09 00:42:07 +01:00
LICENSE Add apache license 2015-02-27 09:38:47 +00:00
README.md Fixup import paths 2015-05-05 19:05:06 +01:00

Go Micro

Go Micro is a microservices client/server library utilising HTTP/RPC and protobuf. It is part of the Micro toolchain.

An example server can be found in go-micro/template.

GoDoc

Prerequisites

Consul is the default discovery mechanism provided in go-micro. Discovery is however pluggable.

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 go-micro/template/main.go

1416690099281057746 [Debug] Rpc handler /_rpc
1416690099281092588 [Debug] Starting server go.micro.service.template id go.micro.service.template-c0bfcb44-728a-11e4-b099-68a86d0d36b6
1416690099281192941 [Debug] Listening on [::]:58264
1416690099281215346 [Debug] Registering go.micro.service.template-c0bfcb44-728a-11e4-b099-68a86d0d36b6

Test Service

$ go run go-micro/examples/service_client.go

go.micro.service.template-c0bfcb44-728a-11e4-b099-68a86d0d36b6: Hello John

Writing a service

Create request/response proto

go-micro/template/proto/example/example.proto:

package go.micro.service.template.example;

message Request {
	required string name = 1;
}

message Response {
	required string msg = 1;
}

Compile proto protoc -I$GOPATH/src --go_out=$GOPATH/src $GOPATH/src/github.com/myodc/go-micro/template/proto/example/example.proto

Create request handler

go-micro/template/handler/example.go:

package handler

import (
	"code.google.com/p/go.net/context"
	"github.com/golang/protobuf/proto"

	"github.com/myodc/go-micro/server"
	example "github.com/myodc/go-micro/template/proto/example"
	log "github.com/golang/glog"
)

type Example struct{}

func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error {
	log.Info("Received Example.Call request")

	rsp.Msg = proto.String(server.Id + ": Hello " + req.GetName())

	return nil
}

Init server

go-micro/template/main.go:

package main

import (
	"github.com/myodc/go-micro/server"
	"github.com/myodc/go-micro/template/handler"
	log "github.com/golang/glog"
)

func main() {
	server.Name = "go.micro.service.template"

	// Initialise Server
	server.Init()

	// Register Handlers
	server.Register(
		server.NewReceiver(
			new(handler.Example),
		),
	)

	// Run server
	if err := server.Run(); err != nil {
		log.Fatal(err)
	}
}

Run service

$ go run go-micro/template/main.go

1416690099281057746 [Debug] Rpc handler /_rpc
1416690099281092588 [Debug] Starting server go.micro.service.template id go.micro.service.template-c0bfcb44-728a-11e4-b099-68a86d0d36b6
1416690099281192941 [Debug] Listening on [::]:58264
1416690099281215346 [Debug] Registering go.micro.service.template-c0bfcb44-728a-11e4-b099-68a86d0d36b6