diff --git a/README.md b/README.md index 254557b8..0fde7aa0 100644 --- a/README.md +++ b/README.md @@ -2,22 +2,16 @@ 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. -An example server can be found in go-micro/template. +An example server can be found in examples/server. [![GoDoc](http://img.shields.io/badge/go-documentation-brightgreen.svg?style=flat-square)](https://godoc.org/github.com/myodc/go-micro) ## Features - Discovery -- Client/Server +- Client +- Server - Pub/Sub -## Future Features -- Config -- Routing -- Monitoring -- Tracing -- Logging - ## Prerequisites Consul is the default discovery mechanism provided in go-micro. Discovery is however pluggable. @@ -34,79 +28,80 @@ $ consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul ### Run Service ``` -$ go run go-micro/template/main.go - -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 +$ 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 ``` ### Test Service ``` -$ go run go-micro/examples/service_client.go - -go.micro.service.template-cfc481fc-013d-11e5-bcdc-68a86d0d36b6: Hello John +$ go run examples/client/main.go +go.micro.srv.example-59b6e0ab-0300-11e5-b696-68a86d0d36b6: Hello John ``` ## Writing a service ### Create request/response proto -`go-micro/template/proto/example/example.proto`: +`go-micro/examples/server/proto/example/example.proto`: ``` -package go.micro.service.template.example; +syntax = "proto3"; message Request { - required string name = 1; + string name = 1; } message Response { - required string msg = 1; + 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`: +`go-micro/examples/server/handler/example.go`: ```go package handler import ( - "code.google.com/p/go.net/context" - "github.com/golang/protobuf/proto" + 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" - "github.com/myodc/go-micro/server" - example "github.com/myodc/go-micro/template/proto/example" - log "github.com/golang/glog" + "golang.org/x/net/context" ) 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 + md, _ := c.GetMetaData(ctx) + log.Info("Received Example.Call request with metadata: %v", md) + rsp.Msg = server.Id + ": Hello " + req.Name + return nil } ``` ### Init server -`go-micro/template/main.go`: +`go-micro/examples/server/main.go`: ```go package main import ( - "github.com/myodc/go-micro/server" - "github.com/myodc/go-micro/template/handler" log "github.com/golang/glog" + "github.com/myodc/go-micro/cmd" + "github.com/myodc/go-micro/examples/server/handler" + "github.com/myodc/go-micro/server" ) func main() { - server.Name = "go.micro.service.template" + // optionally setup command line usage + cmd.Init() + + server.Name = "go.micro.srv.example" // Initialise Server server.Init() @@ -127,9 +122,8 @@ func main() { ### Run service ``` -$ go run go-micro/template/main.go - -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 +$ 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 ``` diff --git a/examples/service_client.go b/examples/client/main.go similarity index 69% rename from examples/service_client.go rename to examples/client/main.go index 00a56ea0..9927176e 100644 --- a/examples/service_client.go +++ b/examples/client/main.go @@ -6,14 +6,15 @@ import ( "github.com/myodc/go-micro/client" "github.com/myodc/go-micro/cmd" c "github.com/myodc/go-micro/context" - example "github.com/myodc/go-micro/template/proto/example" + example "github.com/myodc/go-micro/examples/server/proto/example" "golang.org/x/net/context" ) func main() { cmd.Init() - // Create new request to service go.micro.service.go-template, method Example.Call - req := client.NewRequest("go.micro.service.template", "Example.Call", &example.Request{ + + // Create new request to service go.micro.srv.example, method Example.Call + req := client.NewRequest("go.micro.srv.example", "Example.Call", &example.Request{ Name: "John", }) diff --git a/examples/grpc_client.go b/examples/grpc_client.go deleted file mode 100644 index 9c8be094..00000000 --- a/examples/grpc_client.go +++ /dev/null @@ -1,24 +0,0 @@ -package main - -import ( - "fmt" - - h "github.com/grpc/grpc-common/go/helloworld" - "github.com/myodc/go-micro/client" - "golang.org/x/net/context" -) - -// run github.com/grpc/grpc-common/go/greeter_server/main.go -func main() { - req := client.NewRpcRequest("helloworld.Greeter", "helloworld.Greeter/SayHello", &h.HelloRequest{ - Name: "John", - }, "application/grpc") - - rsp := &h.HelloReply{} - err := client.CallRemote(context.Background(), "localhost:50051", req, rsp) - if err != nil { - fmt.Println(err) - } - - fmt.Println(rsp.Message) -} diff --git a/examples/pub_sub.go b/examples/pubsub/main.go similarity index 100% rename from examples/pub_sub.go rename to examples/pubsub/main.go diff --git a/examples/server/Dockerfile b/examples/server/Dockerfile new file mode 100644 index 00000000..9bb5a8c8 --- /dev/null +++ b/examples/server/Dockerfile @@ -0,0 +1,3 @@ +FROM scratch +ADD server / +ENTRYPOINT [ "/server" ] diff --git a/examples/server/README.md b/examples/server/README.md new file mode 100644 index 00000000..9fc82ac3 --- /dev/null +++ b/examples/server/README.md @@ -0,0 +1,27 @@ +# Example Service + +An example Go service running with go-micro + +### Prerequisites + +Install Consul +[https://www.consul.io/intro/getting-started/install.html](https://www.consul.io/intro/getting-started/install.html) + +Run Consul +``` +$ consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul +``` + +Run Service +``` +$ go run 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 +``` + +Test Service +``` +$ go run client/main.go +go.micro.srv.example-59b6e0ab-0300-11e5-b696-68a86d0d36b6: Hello John +``` diff --git a/template/handler/example.go b/examples/server/handler/example.go similarity index 60% rename from template/handler/example.go rename to examples/server/handler/example.go index 1823376f..0ba789cc 100644 --- a/template/handler/example.go +++ b/examples/server/handler/example.go @@ -3,8 +3,8 @@ package handler import ( 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" - example "github.com/myodc/go-micro/template/proto/example" "golang.org/x/net/context" ) @@ -12,14 +12,8 @@ import ( type Example struct{} func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error { - md, ok := c.GetMetaData(ctx) - if ok { - log.Infof("Received Example.Call request with metadata: %v", md) - } else { - log.Info("Received Example.Call request") - } - + md, _ := c.GetMetaData(ctx) + log.Info("Received Example.Call request with metadata: %v", md) rsp.Msg = server.Id + ": Hello " + req.Name - return nil } diff --git a/template/main.go b/examples/server/main.go similarity index 81% rename from template/main.go rename to examples/server/main.go index 35238a95..50f2cf59 100644 --- a/template/main.go +++ b/examples/server/main.go @@ -3,15 +3,15 @@ package main import ( log "github.com/golang/glog" "github.com/myodc/go-micro/cmd" + "github.com/myodc/go-micro/examples/server/handler" "github.com/myodc/go-micro/server" - "github.com/myodc/go-micro/template/handler" ) func main() { // optionally setup command line usage cmd.Init() - server.Name = "go.micro.service.template" + server.Name = "go.micro.srv.example" // Initialise Server server.Init() diff --git a/template/proto/example/example.pb.go b/examples/server/proto/example/example.pb.go similarity index 88% rename from template/proto/example/example.pb.go rename to examples/server/proto/example/example.pb.go index dfc87d8c..251d6517 100644 --- a/template/proto/example/example.pb.go +++ b/examples/server/proto/example/example.pb.go @@ -1,12 +1,12 @@ // Code generated by protoc-gen-go. -// source: myodc/go-micro/template/proto/example/example.proto +// source: go-micro/examples/server/proto/example/example.proto // DO NOT EDIT! /* Package example is a generated protocol buffer package. It is generated from these files: - myodc/go-micro/template/proto/example/example.proto + go-micro/examples/server/proto/example/example.proto It has these top-level messages: Request diff --git a/template/proto/example/example.proto b/examples/server/proto/example/example.proto similarity index 53% rename from template/proto/example/example.proto rename to examples/server/proto/example/example.proto index c8de6e28..095efa85 100644 --- a/template/proto/example/example.proto +++ b/examples/server/proto/example/example.proto @@ -1,9 +1,9 @@ syntax = "proto3"; message Request { - optional string name = 1; + string name = 1; } message Response { - optional string msg = 1; + string msg = 1; } diff --git a/template/Dockerfile b/template/Dockerfile deleted file mode 100644 index 4149ef71..00000000 --- a/template/Dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM scratch -ADD template / -ENTRYPOINT [ "/template" ] diff --git a/template/README.md b/template/README.md deleted file mode 100644 index 1a596abc..00000000 --- a/template/README.md +++ /dev/null @@ -1,29 +0,0 @@ -# Template Service - -An example Go service running with go-micro - -### Prerequisites - -Install Consul -[https://www.consul.io/intro/getting-started/install.html](https://www.consul.io/intro/getting-started/install.html) - -Run Consul -``` -$ consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -``` - -Run Service -``` -$ go run main.go - -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 -``` - -Test Service -``` -$ go run go-micro/examples/service_client.go - -go.micro.service.template-cfc481fc-013d-11e5-bcdc-68a86d0d36b6: Hello John -``` diff --git a/template/pod.json b/template/pod.json deleted file mode 100644 index 1bb0e2ae..00000000 --- a/template/pod.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "kind": "Pod", - "apiVersion": "v1beta1", - "id": "template-service", - "desiredState": { - "manifest": { - "version": "v1beta1", - "id": "template-service", - "containers": [{ - "name": "template-service", - "image": "chuhnk/go-template", - "ports": [{"name": "template-service", "containerPort": 8080}], - "command": ["--registry=kubernetes", "--bind_address=:8080"] - }], - } - }, - "labels": { - "name": "go.micro.service.template", - } -} - diff --git a/template/service.json b/template/service.json deleted file mode 100644 index 4dc64727..00000000 --- a/template/service.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "id": "template-service", - "kind": "Service", - "apiVersion": "v1beta1", - "port": 9091, - "containerPort": 8080, - "selector": { "name": "go.micro.service.template" }, - "labels": { "name": "go.micro.service.template" } -}