micro/README.md

130 lines
3.2 KiB
Markdown
Raw Normal View History

2015-04-02 22:47:21 +03:00
# Go Micro
2015-01-14 02:31:27 +03:00
2015-05-24 01:51:31 +03:00
Go Micro is a microservices library which provides the fundamental building blocks for writing fault tolerant distributed systems at scale. It is part of the [Micro](https://github.com/myodc/micro) toolchain.
2015-01-14 11:38:39 +03:00
2015-05-25 20:16:42 +03:00
An example server can be found in examples/server.
2015-01-14 11:38:39 +03:00
2015-05-05 21:05:06 +03:00
[![GoDoc](http://img.shields.io/badge/go-documentation-brightgreen.svg?style=flat-square)](https://godoc.org/github.com/myodc/go-micro)
2015-02-14 20:34:35 +03:00
2015-05-18 01:04:19 +03:00
## Features
- Discovery
2015-05-25 20:16:42 +03:00
- Client
- Server
2015-05-18 01:04:19 +03:00
- Pub/Sub
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
```
2015-05-25 20:16:42 +03:00
$ go run examples/server/main.go
I0525 18:06:14.471489 83304 server.go:117] Starting server go.micro.srv.example id go.micro.srv.example-59b6e0ab-0300-11e5-b696-68a86d0d36b6
I0525 18:06:14.474960 83304 rpc_server.go:126] Listening on [::]:62216
I0525 18:06:14.474997 83304 server.go:99] Registering node: go.micro.srv.example-59b6e0ab-0300-11e5-b696-68a86d0d36b6
2015-01-14 11:38:39 +03:00
```
2015-01-14 13:50:43 +03:00
### Test Service
2015-01-14 11:38:39 +03:00
```
2015-05-25 20:16:42 +03:00
$ go run examples/client/main.go
go.micro.srv.example-59b6e0ab-0300-11e5-b696-68a86d0d36b6: Hello John
2015-01-14 11:38:39 +03:00
```
2015-01-14 13:50:43 +03:00
## Writing a service
### Create request/response proto
2015-05-25 20:16:42 +03:00
`go-micro/examples/server/proto/example/example.proto`:
2015-01-14 13:50:43 +03:00
```
2015-05-25 20:16:42 +03:00
syntax = "proto3";
2015-01-14 13:50:43 +03:00
message Request {
2015-05-25 20:16:42 +03:00
string name = 1;
2015-01-14 13:50:43 +03:00
}
message Response {
2015-05-25 20:16:42 +03:00
string msg = 1;
2015-01-14 13:50:43 +03:00
}
```
2015-05-05 21:05:06 +03:00
Compile proto `protoc -I$GOPATH/src --go_out=$GOPATH/src $GOPATH/src/github.com/myodc/go-micro/template/proto/example/example.proto`
2015-01-14 13:50:43 +03:00
### Create request handler
2015-05-25 20:16:42 +03:00
`go-micro/examples/server/handler/example.go`:
2015-01-14 13:50:43 +03:00
2015-02-02 21:53:16 +03:00
```go
2015-01-14 13:50:43 +03:00
package handler
import (
2015-05-25 20:16:42 +03:00
log "github.com/golang/glog"
c "github.com/myodc/go-micro/context"
example "github.com/myodc/go-micro/examples/server/proto/example"
"github.com/myodc/go-micro/server"
2015-01-14 13:50:43 +03:00
2015-05-25 20:16:42 +03:00
"golang.org/x/net/context"
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-05-27 00:39:48 +03:00
md, _ := c.GetMetadata(ctx)
2015-05-25 20:16:42 +03:00
log.Info("Received Example.Call request with metadata: %v", md)
rsp.Msg = server.Id + ": Hello " + req.Name
return nil
2015-01-14 13:50:43 +03:00
}
```
### Init server
2015-05-25 20:16:42 +03:00
`go-micro/examples/server/main.go`:
2015-01-14 13:50:43 +03:00
2015-02-02 21:53:16 +03:00
```go
2015-01-14 13:50:43 +03:00
package main
import (
2015-01-31 18:49:21 +03:00
log "github.com/golang/glog"
2015-05-25 20:16:42 +03:00
"github.com/myodc/go-micro/cmd"
"github.com/myodc/go-micro/examples/server/handler"
"github.com/myodc/go-micro/server"
2015-01-14 13:50:43 +03:00
)
func main() {
2015-05-25 20:16:42 +03:00
// optionally setup command line usage
cmd.Init()
2015-01-14 13:50:43 +03:00
// Initialise Server
2015-05-27 00:42:41 +03:00
server.Init(
server.Name("go.micro.srv.example"),
)
2015-01-14 13:50:43 +03:00
// Register Handlers
server.Handle(
server.NewHandler(
2015-01-14 13:50:43 +03:00
new(handler.Example),
),
)
// Run server
if err := server.Run(); err != nil {
log.Fatal(err)
}
}
```
### Run service
```
2015-05-25 20:16:42 +03:00
$ go run examples/server/main.go
I0525 18:06:14.471489 83304 server.go:117] Starting server go.micro.srv.example id go.micro.srv.example-59b6e0ab-0300-11e5-b696-68a86d0d36b6
I0525 18:06:14.474960 83304 rpc_server.go:126] Listening on [::]:62216
I0525 18:06:14.474997 83304 server.go:99] Registering node: go.micro.srv.example-59b6e0ab-0300-11e5-b696-68a86d0d36b6
2015-01-14 13:50:43 +03:00
```