1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-24 08:02:32 +02:00
go-micro/README.md

130 lines
3.2 KiB
Markdown
Raw Normal View History

2015-04-02 21:47:21 +02:00
# Go Micro
2015-01-14 01:31:27 +02:00
2015-05-24 00:51:31 +02: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 10:38:39 +02:00
2015-05-25 19:16:42 +02:00
An example server can be found in examples/server.
2015-01-14 10:38:39 +02:00
2015-05-05 20:05:06 +02: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 19:34:35 +02:00
2015-05-18 00:04:19 +02:00
## Features
- Discovery
2015-05-25 19:16:42 +02:00
- Client
- Server
2015-05-18 00:04:19 +02:00
- Pub/Sub
2015-01-14 10:38:39 +02:00
## Prerequisites
2015-01-14 12:50:43 +02:00
Consul is the default discovery mechanism provided in go-micro. Discovery is however pluggable.
2015-01-14 10:38:39 +02:00
### Install Consul
[https://www.consul.io/intro/getting-started/install.html](https://www.consul.io/intro/getting-started/install.html)
2015-01-14 12:50:43 +02:00
## Getting Started
2015-01-14 10:38:39 +02:00
### Run Consul
```
$ consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul
```
2015-01-14 12:50:43 +02:00
### Run Service
2015-01-14 10:38:39 +02:00
```
2015-05-25 19:16:42 +02: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 10:38:39 +02:00
```
2015-01-14 12:50:43 +02:00
### Test Service
2015-01-14 10:38:39 +02:00
```
2015-05-25 19:16:42 +02:00
$ go run examples/client/main.go
go.micro.srv.example-59b6e0ab-0300-11e5-b696-68a86d0d36b6: Hello John
2015-01-14 10:38:39 +02:00
```
2015-01-14 12:50:43 +02:00
## Writing a service
### Create request/response proto
2015-05-25 19:16:42 +02:00
`go-micro/examples/server/proto/example/example.proto`:
2015-01-14 12:50:43 +02:00
```
2015-05-25 19:16:42 +02:00
syntax = "proto3";
2015-01-14 12:50:43 +02:00
message Request {
2015-05-25 19:16:42 +02:00
string name = 1;
2015-01-14 12:50:43 +02:00
}
message Response {
2015-05-25 19:16:42 +02:00
string msg = 1;
2015-01-14 12:50:43 +02:00
}
```
2015-05-05 20:05:06 +02: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 12:50:43 +02:00
### Create request handler
2015-05-25 19:16:42 +02:00
`go-micro/examples/server/handler/example.go`:
2015-01-14 12:50:43 +02:00
2015-02-02 20:53:16 +02:00
```go
2015-01-14 12:50:43 +02:00
package handler
import (
2015-05-25 19:16:42 +02: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 12:50:43 +02:00
2015-05-25 19:16:42 +02:00
"golang.org/x/net/context"
2015-01-14 12:50:43 +02:00
)
type Example struct{}
func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error {
2015-05-26 23:39:48 +02:00
md, _ := c.GetMetadata(ctx)
2015-05-25 19:16:42 +02:00
log.Info("Received Example.Call request with metadata: %v", md)
rsp.Msg = server.Id + ": Hello " + req.Name
return nil
2015-01-14 12:50:43 +02:00
}
```
### Init server
2015-05-25 19:16:42 +02:00
`go-micro/examples/server/main.go`:
2015-01-14 12:50:43 +02:00
2015-02-02 20:53:16 +02:00
```go
2015-01-14 12:50:43 +02:00
package main
import (
2015-01-31 17:49:21 +02:00
log "github.com/golang/glog"
2015-05-25 19:16:42 +02:00
"github.com/myodc/go-micro/cmd"
"github.com/myodc/go-micro/examples/server/handler"
"github.com/myodc/go-micro/server"
2015-01-14 12:50:43 +02:00
)
func main() {
2015-05-25 19:16:42 +02:00
// optionally setup command line usage
cmd.Init()
2015-01-14 12:50:43 +02:00
// Initialise Server
2015-05-26 23:42:41 +02:00
server.Init(
server.Name("go.micro.srv.example"),
)
2015-01-14 12:50:43 +02:00
// Register Handlers
server.Register(
server.NewReceiver(
new(handler.Example),
),
)
// Run server
if err := server.Run(); err != nil {
log.Fatal(err)
}
}
```
### Run service
```
2015-05-25 19:16:42 +02: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 12:50:43 +02:00
```