mirror of https://github.com/dapr/go-sdk.git
parent
14e7b5eb13
commit
c7a0fabf13
|
@ -1,45 +0,0 @@
|
|||
# Dapr PubSub Example with go-sdk
|
||||
|
||||
This folder contains two go file that uses this go-SDK to invoke the Dapr PubSub API.
|
||||
|
||||
## Helpful
|
||||

|
||||
|
||||
## Step
|
||||
|
||||
### Prepare
|
||||
- Dapr installed
|
||||
|
||||
### Run Subscriber Server
|
||||
when use Dapr PubSub to subscribe, should have a http or gRPC server to receive the requests from Dapr.
|
||||
|
||||
Use the environment variable, Please set `DAPR_PUBSUB_NAME` as the name of the components: `messagebus` at first.
|
||||
|
||||
Please change directory to pubsub/ and run the following command:
|
||||
```bash
|
||||
dapr run --app-id sub \
|
||||
--app-protocol http \
|
||||
--app-port 8080 \
|
||||
--dapr-http-port 3500 \
|
||||
--log-level debug \
|
||||
--components-path ./config \
|
||||
go run sub.go
|
||||
```
|
||||
|
||||
### Run Publisher
|
||||
Publish is more simply than subscribe. Just Publish the data to target pubsub component with its' name.
|
||||
|
||||
After you start a server by above guide.
|
||||
Please change directory to pubsub/ and run the following command:
|
||||
```bash
|
||||
dapr run --app-id pub \
|
||||
--log-level debug \
|
||||
--components-path ./config \
|
||||
go run pub.go
|
||||
```
|
||||
|
||||
## Result
|
||||
You would see log that in terminal which run the server(subscriber) code.
|
||||
```bash
|
||||
== APP == 2020/08/23 13:21:58 event - PubsubName: messagebus, Topic: demo, ID: 11acaa82-23c4-4244-8969-7360dae52e5d, Data: ping
|
||||
```
|
|
@ -1,11 +0,0 @@
|
|||
apiVersion: dapr.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: messagebus
|
||||
spec:
|
||||
type: pubsub.redis
|
||||
metadata:
|
||||
- name: redisHost
|
||||
value: localhost:6379
|
||||
- name: redisPassword
|
||||
value: ""
|
|
@ -1,33 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
dapr "github.com/dapr/go-sdk/client"
|
||||
)
|
||||
|
||||
var (
|
||||
// set the environment as instructions.
|
||||
pubsubName = os.Getenv("DAPR_PUBSUB_NAME")
|
||||
topicName = "neworder"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
data := []byte("ping")
|
||||
|
||||
client, err := dapr.NewClient()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
if err := client.PublishEvent(ctx, pubsubName, topicName, data); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("data published")
|
||||
|
||||
fmt.Println("Done (CTRL+C to Exit)")
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/dapr/go-sdk/service/common"
|
||||
daprd "github.com/dapr/go-sdk/service/http"
|
||||
)
|
||||
|
||||
// Subscription to tell the dapr what topic to subscribe.
|
||||
// - PubsubName: is the name of the component configured in the metadata of pubsub.yaml.
|
||||
// - Topic: is the name of the topic to subscribe.
|
||||
// - Route: tell dapr where to request the API to publish the message to the subscriber when get a message from topic.
|
||||
var sub := &common.Subscription{
|
||||
PubsubName: "messagebus",
|
||||
Topic: "neworder",
|
||||
Route: "/orders",
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := daprd.NewService(":8080")
|
||||
|
||||
if err := s.AddTopicEventHandler(sub, eventHandler); err != nil {
|
||||
log.Fatalf("error adding topic subscription: %v", err)
|
||||
}
|
||||
|
||||
if err := s.Start(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("error listenning: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func eventHandler(ctx context.Context, e *common.TopicEvent) error {
|
||||
log.Printf("event - PubsubName: %s, Topic: %s, ID: %s, Data: %s", e.PubsubName, e.Topic, e.ID, e.Data)
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue