Add pub/sub to client/server and make broker more low level

This commit is contained in:
Asim
2015-06-12 19:52:27 +01:00
parent cdf2f2cbcd
commit b91af916f9
24 changed files with 542 additions and 193 deletions

View File

@@ -10,6 +10,26 @@ import (
"golang.org/x/net/context"
)
func pub() {
msg := client.NewPublication("topic.go.micro.srv.example", &example.Message{
Say: "This is a publication",
})
// create context with metadata
ctx := c.WithMetadata(context.Background(), map[string]string{
"X-User-Id": "john",
"X-From-Id": "script",
})
// publish message
if err := client.Publish(ctx, msg); err != nil {
fmt.Println("pub err: ", err)
return
}
fmt.Printf("Published: %v\n", msg)
}
func call(i int) {
// Create new request to service go.micro.srv.example, method Example.Call
req := client.NewRequest("go.micro.srv.example", "Example.Call", &example.Request{
@@ -26,7 +46,7 @@ func call(i int) {
// Call service
if err := client.Call(ctx, req, rsp); err != nil {
fmt.Println("err: ", err, rsp)
fmt.Println("call err: ", err, rsp)
return
}
@@ -52,7 +72,7 @@ func stream() {
}
if stream.Error() != nil {
fmt.Println("err:", err)
fmt.Println("stream err:", err)
return
}
@@ -67,4 +87,5 @@ func main() {
}
stream()
pub()
}

View File

@@ -7,9 +7,6 @@ import (
log "github.com/golang/glog"
"github.com/myodc/go-micro/broker"
"github.com/myodc/go-micro/cmd"
c "github.com/myodc/go-micro/context"
"golang.org/x/net/context"
)
var (
@@ -20,24 +17,24 @@ func pub() {
tick := time.NewTicker(time.Second)
i := 0
for _ = range tick.C {
ctx := c.WithMetadata(context.Background(), map[string]string{
"id": fmt.Sprintf("%d", i),
})
msg := fmt.Sprintf("%d: %s", i, time.Now().String())
if err := broker.Publish(ctx, topic, []byte(msg)); err != nil {
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.Errorf("[pub] failed: %v", err)
} else {
fmt.Println("[pub] pubbed message:", msg)
fmt.Println("[pub] pubbed message:", string(msg.Body))
}
i++
}
}
func sub() {
_, err := broker.Subscribe(topic, func(ctx context.Context, msg *broker.Message) {
md, _ := c.GetMetadata(ctx)
fmt.Println("[sub] received message:", string(msg.Body), "context", md)
_, err := broker.Subscribe(topic, func(msg *broker.Message) {
fmt.Println("[sub] received message:", string(msg.Body), "header", msg.Header)
})
if err != nil {
fmt.Println(err)

View File

@@ -4,6 +4,7 @@ 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/examples/server/subscriber"
"github.com/myodc/go-micro/server"
)
@@ -23,6 +24,21 @@ func main() {
),
)
// Register Subscribers
server.Subscribe(
server.NewSubscriber(
"topic.go.micro.srv.example",
new(subscriber.Example),
),
)
server.Subscribe(
server.NewSubscriber(
"topic.go.micro.srv.example",
subscriber.Handler,
),
)
// Run server
if err := server.Run(); err != nil {
log.Fatal(err)

View File

@@ -9,6 +9,7 @@ It is generated from these files:
go-micro/examples/server/proto/example/example.proto
It has these top-level messages:
Message
Request
Response
StreamingRequest
@@ -21,6 +22,14 @@ import proto "github.com/golang/protobuf/proto"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
type Message struct {
Say string `protobuf:"bytes,1,opt,name=say" json:"say,omitempty"`
}
func (m *Message) Reset() { *m = Message{} }
func (m *Message) String() string { return proto.CompactTextString(m) }
func (*Message) ProtoMessage() {}
type Request struct {
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
}

View File

@@ -1,5 +1,9 @@
syntax = "proto3";
message Message {
string say = 1;
}
message Request {
string name = 1;
}

View File

@@ -0,0 +1,18 @@
package subscriber
import (
log "github.com/golang/glog"
example "github.com/myodc/go-micro/examples/server/proto/example"
"golang.org/x/net/context"
)
type Example struct{}
func (e *Example) Handle(ctx context.Context, msg *example.Message) error {
log.Info("Handler Received message: ", msg.Say)
return nil
}
func Handler(msg *example.Message) {
log.Info("Function Received message: ", msg.Say)
}