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

136 lines
3.1 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-05 20:05:06 +02:00
Go Micro is a microservices client/server library utilising HTTP/RPC and protobuf. It is part of the [Micro](https://github.com/myodc/micro) toolchain.
2015-01-14 10:38:39 +02:00
An example server can be found in go-micro/template.
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
- Client/Server
- Pub/Sub
- Key/Value store
### Planned
- Metrics
- Tracing
- Logging
- Stats
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
```
$ go run go-micro/template/main.go
2015-05-23 13:22:23 +02:00
I0523 12:21:09.506998 77096 server.go:113] Starting server go.micro.service.template id go.micro.service.template-cfc481fc-013d-11e5-bcdc-68a86d0d36b6
I0523 12:21:09.507281 77096 rpc_server.go:112] Listening on [::]:51868
I0523 12:21:09.507329 77096 server.go:95] Registering node: go.micro.service.template-cfc481fc-013d-11e5-bcdc-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
```
$ go run go-micro/examples/service_client.go
2015-05-23 13:22:23 +02:00
go.micro.service.template-cfc481fc-013d-11e5-bcdc-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
`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;
}
```
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
`go-micro/template/handler/example.go`:
2015-02-02 20:53:16 +02:00
```go
2015-01-14 12:50:43 +02:00
package handler
import (
"code.google.com/p/go.net/context"
2015-05-05 20:05:06 +02:00
"github.com/golang/protobuf/proto"
2015-01-14 12:50:43 +02:00
2015-05-05 20:05:06 +02:00
"github.com/myodc/go-micro/server"
example "github.com/myodc/go-micro/template/proto/example"
2015-01-31 17:49:21 +02:00
log "github.com/golang/glog"
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-01-31 17:49:21 +02:00
log.Info("Received Example.Call request")
2015-01-14 12:50:43 +02:00
rsp.Msg = proto.String(server.Id + ": Hello " + req.GetName())
return nil
}
```
### Init server
`go-micro/template/main.go`:
2015-02-02 20:53:16 +02:00
```go
2015-01-14 12:50:43 +02:00
package main
import (
2015-05-05 20:05:06 +02:00
"github.com/myodc/go-micro/server"
"github.com/myodc/go-micro/template/handler"
2015-01-31 17:49:21 +02:00
log "github.com/golang/glog"
2015-01-14 12:50:43 +02: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
2015-05-23 13:22:23 +02:00
I0523 12:21:09.506998 77096 server.go:113] Starting server go.micro.service.template id go.micro.service.template-cfc481fc-013d-11e5-bcdc-68a86d0d36b6
I0523 12:21:09.507281 77096 rpc_server.go:112] Listening on [::]:51868
I0523 12:21:09.507329 77096 server.go:95] Registering node: go.micro.service.template-cfc481fc-013d-11e5-bcdc-68a86d0d36b6
2015-01-14 12:50:43 +02:00
```