micro/README.md

126 lines
3.0 KiB
Markdown
Raw Normal View History

2015-01-14 02:31:27 +03:00
# Go Micro - a microservices client/server library
2015-02-02 21:53:16 +03:00
This a minimalistic step into microservices using HTTP/RPC and protobuf. It's the first implementation of a microservices "spec" called [Micro](https://github.com/asim/micro).
2015-01-14 11:38:39 +03:00
An example server can be found in go-micro/template.
2015-02-14 20:34:35 +03:00
[![GoDoc](http://img.shields.io/badge/go-documentation-brightgreen.svg?style=flat-square)](https://godoc.org/github.com/asim/go-micro)
2015-01-14 11:38:39 +03:00
## Prerequisites
2015-01-14 13:50:43 +03:00
Consul is the default discovery mechanism provided in go-micro. Discovery is however pluggable.
2015-01-14 11:38:39 +03:00
### Install Consul
[https://www.consul.io/intro/getting-started/install.html](https://www.consul.io/intro/getting-started/install.html)
2015-01-14 13:50:43 +03:00
## Getting Started
2015-01-14 11:38:39 +03:00
### Run Consul
```
$ consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul
```
2015-01-14 13:50:43 +03:00
### Run Service
2015-01-14 11:38:39 +03:00
```
$ 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
```
2015-01-14 13:50:43 +03:00
### Test Service
2015-01-14 11:38:39 +03:00
```
$ go run go-micro/examples/service_client.go
go.micro.service.template-c0bfcb44-728a-11e4-b099-68a86d0d36b6: Hello John
```
2015-01-14 13:50:43 +03:00
## 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/asim/go-micro/template/proto/example/example.proto`
### Create request handler
`go-micro/template/handler/example.go`:
2015-02-02 21:53:16 +03:00
```go
2015-01-14 13:50:43 +03:00
package handler
import (
"code.google.com/p/go.net/context"
"code.google.com/p/goprotobuf/proto"
"github.com/asim/go-micro/server"
example "github.com/asim/go-micro/template/proto/example"
2015-01-31 18:49:21 +03:00
log "github.com/golang/glog"
2015-01-14 13:50:43 +03:00
)
type Example struct{}
func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error {
2015-01-31 18:49:21 +03:00
log.Info("Received Example.Call request")
2015-01-14 13:50:43 +03:00
rsp.Msg = proto.String(server.Id + ": Hello " + req.GetName())
return nil
}
```
### Init server
`go-micro/template/main.go`:
2015-02-02 21:53:16 +03:00
```go
2015-01-14 13:50:43 +03:00
package main
import (
"github.com/asim/go-micro/server"
"github.com/asim/go-micro/template/handler"
2015-01-31 18:49:21 +03:00
log "github.com/golang/glog"
2015-01-14 13:50:43 +03:00
)
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
```