-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Asim Aslam
committed
Jan 5, 2017
1 parent
f84830b
commit cc2e672
Showing
29 changed files
with
2,161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Broker | ||
|
||
## Contents | ||
|
||
- main.go - demonstrates simple runs pub-sub as two go routines running for 10 seconds. | ||
- producer - publishes messages to the broker every second | ||
- consumer - consumes any messages sent by the producer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/micro/go-micro/broker" | ||
"github.com/micro/go-micro/cmd" | ||
// To enable rabbitmq plugin uncomment | ||
//_ "github.com/micro/go-plugins/broker/rabbitmq" | ||
) | ||
|
||
var ( | ||
topic = "go.micro.topic.foo" | ||
) | ||
|
||
// Example of a shared subscription which receives a subset of messages | ||
func sharedSub() { | ||
_, err := broker.Subscribe(topic, func(p broker.Publication) error { | ||
fmt.Println("[sub] received message:", string(p.Message().Body), "header", p.Message().Header) | ||
return nil | ||
}, broker.Queue("consumer")) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
// Example of a subscription which receives all the messages | ||
func sub() { | ||
_, err := broker.Subscribe(topic, func(p broker.Publication) error { | ||
fmt.Println("[sub] received message:", string(p.Message().Body), "header", p.Message().Header) | ||
return nil | ||
}) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
func main() { | ||
cmd.Init() | ||
|
||
if err := broker.Init(); err != nil { | ||
log.Fatalf("Broker Init error: %v", err) | ||
} | ||
if err := broker.Connect(); err != nil { | ||
log.Fatalf("Broker Connect error: %v", err) | ||
} | ||
|
||
sub() | ||
select {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/micro/go-micro/broker" | ||
"github.com/micro/go-micro/cmd" | ||
) | ||
|
||
var ( | ||
topic = "go.micro.topic.foo" | ||
) | ||
|
||
func pub() { | ||
tick := time.NewTicker(time.Second) | ||
i := 0 | ||
for _ = range tick.C { | ||
msg := &broker.Message{ | ||
Header: map[string]string{ | ||
"id": fmt.Sprintf("%d", i), | ||
}, | ||
Body: []byte(fmt.Sprintf("%d: %s", i, time.Now().String())), | ||
} | ||
if err := broker.Publish(topic, msg); err != nil { | ||
log.Printf("[pub] failed: %v", err) | ||
} else { | ||
fmt.Println("[pub] pubbed message:", string(msg.Body)) | ||
} | ||
i++ | ||
} | ||
} | ||
|
||
func sub() { | ||
_, err := broker.Subscribe(topic, func(p broker.Publication) error { | ||
fmt.Println("[sub] received message:", string(p.Message().Body), "header", p.Message().Header) | ||
return nil | ||
}) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
func main() { | ||
cmd.Init() | ||
|
||
if err := broker.Init(); err != nil { | ||
log.Fatalf("Broker Init error: %v", err) | ||
} | ||
if err := broker.Connect(); err != nil { | ||
log.Fatalf("Broker Connect error: %v", err) | ||
} | ||
|
||
go pub() | ||
go sub() | ||
|
||
<-time.After(time.Second * 10) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/micro/go-micro/broker" | ||
"github.com/micro/go-micro/cmd" | ||
// To enable rabbitmq plugin uncomment | ||
//_ "github.com/micro/go-plugins/broker/rabbitmq" | ||
) | ||
|
||
var ( | ||
topic = "go.micro.topic.foo" | ||
) | ||
|
||
func pub() { | ||
tick := time.NewTicker(time.Second) | ||
i := 0 | ||
for _ = range tick.C { | ||
msg := &broker.Message{ | ||
Header: map[string]string{ | ||
"id": fmt.Sprintf("%d", i), | ||
}, | ||
Body: []byte(fmt.Sprintf("%d: %s", i, time.Now().String())), | ||
} | ||
if err := broker.Publish(topic, msg); err != nil { | ||
log.Printf("[pub] failed: %v", err) | ||
} else { | ||
fmt.Println("[pub] pubbed message:", string(msg.Body)) | ||
} | ||
i++ | ||
} | ||
} | ||
|
||
func main() { | ||
cmd.Init() | ||
|
||
if err := broker.Init(); err != nil { | ||
log.Fatalf("Broker Init error: %v", err) | ||
} | ||
|
||
if err := broker.Connect(); err != nil { | ||
log.Fatalf("Broker Connect error: %v", err) | ||
} | ||
|
||
pub() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Client | ||
|
||
## Contents | ||
|
||
- main.go - calls each of the go.micro.srv.example handlers and includes the use of the streaming handler | ||
- codegen - demonstrates how to use code generation to remove boilerplate code | ||
- dc_filter - shows how to use Select filters inside a call wrapper for filtering to the local DC | ||
- dc_selector - is the same as dc_filter but as a Selector implementation itself | ||
- pub - publishes messages using the Publish method. By default encoding in protobuf | ||
- selector - shows how to write and load your own Selector | ||
- wrapper - provides examples for how to use client Wrappers (middleware) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
# Code Generation [Experimental] | ||
|
||
We're experimenting with code generation to reduce the amount of boiler plate code written. | ||
|
||
## Example | ||
|
||
Going from this | ||
```golang | ||
req := client.NewRequest("go.micro.srv.example", "Example.Call", &example.Request{ | ||
Name: "John", | ||
}) | ||
|
||
rsp := &example.Response{} | ||
|
||
if err := client.Call(context.Background(), req, rsp); err != nil { | ||
return err | ||
} | ||
``` | ||
|
||
To | ||
|
||
```golang | ||
rsp, err := cl.Call(context.Background(), &example.Request{Name: "John"}) | ||
if err != nil { | ||
return err | ||
} | ||
``` | ||
|
||
## Generation of stub code for the example service | ||
|
||
```shell | ||
go get github.com/micro/protobuf/protoc-gen-go | ||
cd examples/server/proto/example | ||
protoc --go_out=plugins=micro:. example.proto | ||
``` | ||
|
||
Look at examples/server/proto/example/example.pb.go | ||
to see the generated code. | ||
|
||
## Guide | ||
|
||
### Download the protoc-gen-go code | ||
|
||
```shell | ||
go get github.com/micro/protobuf/protoc-gen-go | ||
``` | ||
|
||
### Define your proto service. | ||
|
||
hello.proto | ||
```shell | ||
syntax = "proto3"; | ||
|
||
// package name is used as the service name for discovery | ||
// if service name is not passed in when initialising the | ||
// client | ||
package go.micro.srv.greeter; | ||
|
||
service Say { | ||
rpc Hello(Request) returns (Response) {} | ||
} | ||
|
||
message Request { | ||
optional string name = 1; | ||
} | ||
|
||
message Response { | ||
optional string msg = 1; | ||
} | ||
``` | ||
**Note: Remember to set package name in the proto, it's used to generate | ||
the service for discovery.** | ||
### Generate code | ||
```shell | ||
protoc --go_out=plugins=micro:. hello.proto | ||
``` | ||
### Generated code | ||
```shell | ||
// Client API for Say service | ||
type SayClient interface { | ||
Hello(ctx context.Context, in *Request) (*Response, error) | ||
} | ||
type sayClient struct { | ||
c client.Client | ||
serviceName string | ||
} | ||
func NewSayClient(serviceName string, c client.Client) SayClient { | ||
if c == nil { | ||
c = client.NewClient() | ||
} | ||
if len(serviceName) == 0 { | ||
serviceName = "go.micro.srv.greeter" | ||
} | ||
return &sayClient{ | ||
c: c, | ||
serviceName: serviceName, | ||
} | ||
} | ||
func (c *sayClient) Hello(ctx context.Context, in *Request) (*Response, error) { | ||
req := c.c.NewRequest(c.serviceName, "Say.Hello", in) | ||
out := new(Response) | ||
err := c.c.Call(ctx, req, out) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return out, nil | ||
} | ||
// Server API for Say service | ||
type SayHandler interface { | ||
Hello(context.Context, *Request, *Response) error | ||
} | ||
func RegisterSayHandler(s server.Server, hdlr SayHandler) { | ||
s.Handle(s.NewHandler(hdlr)) | ||
} | ||
``` | ||
### Use the client | ||
```golang | ||
import ( | ||
"fmt" | ||
"golang.org/x/net/context" | ||
"github.com/micro/go-micro/client" | ||
hello "path/to/hello/proto" | ||
) | ||
func main() { | ||
cl := hello.NewSayClient("go.micro.srv.greeter", client.DefaultClient) | ||
// alternative initialisation | ||
// cl := hello.NewSayClient("", nil) | ||
rsp, err := cl.Hello(contex.Background(), &hello.Request{"Name": "John"}) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
``` |
Oops, something went wrong.