updated examples

This commit is contained in:
Mark Chmarny 2020-05-02 04:49:33 -07:00
parent dcb92e33aa
commit c0c9f0166b
8 changed files with 81 additions and 33 deletions

View File

@ -1,8 +1,6 @@
# Dapr SDK for Go
# dapr SDK for Go
This is the Dapr SDK for Go, based on the auto-generated proto client.<br>
For more info on Dapr and gRPC, visit [this link](https://github.com/dapr/docs/tree/master/howto/create-grpc-app).
This is the dapr SDK (client) for Go.
## Installation
@ -12,20 +10,20 @@ go get github.com/dapr/go-sdk
## Usage
The `example` folder contains a Dapr enabled app that receives events (client), and a caller that invokes the Dapr API (caller).
The `example` folder contains a dapr enabled app that receives events (serving), and a client app that uses this SDK to invoke dapr API (client).
1. Run the client
1. Run the serving app
```
cd example/client
dapr run --app-id client --protocol grpc --app-port 4000 go run main.go
cd example/serving
dapr run --app-id serving --protocol grpc --app-port 4000 go run main.go
```
2. Run the caller
```
cd example/caller
cd example/client
dapr run --app-id caller go run main.go
```
*Note: If you don't setup a Dapr binding, expect the error message `rpc error: code = Unknown desc = ERR_INVOKE_OUTPUT_BINDING: couldn't find output binding storage`*
> If you don't setup a Dapr binding, expect the error message `rpc error: code = Unknown desc = ERR_INVOKE_OUTPUT_BINDING: couldn't find output binding storage`

View File

@ -11,12 +11,12 @@ import (
)
const (
daprPortDefault = "50005"
daprPortDefault = "4000"
daprPortEnvVarName = "DAPR_GRPC_PORT"
)
// NewClientWithAddress instantiates dapr client locally using port from DAPR_GRPC_PORT env var
// When DAPR_GRPC_PORT client defaults to 50005
// When DAPR_GRPC_PORT client defaults to 4000
func NewClient() (client *Client, err error) {
port := os.Getenv(daprPortEnvVarName)
if port == "" {

View File

@ -0,0 +1,11 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: messagebus
spec:
type: pubsub.redis
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""

View File

@ -0,0 +1,13 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
spec:
type: state.redis
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
- name: actorStateStore
value: "true"

View File

@ -20,43 +20,43 @@ func main() {
defer client.Close(ctx)
// invoke a method called MyMethod on another dapr enabled service with id client
resp, err := client.InvokeService(ctx, "my-client", "MyMethod", data)
resp, err := client.InvokeService(ctx, "serving", "MyMethod", data)
if err != nil {
panic(err)
}
fmt.Println(string(resp))
// publish a message to the topic my-topic
err = client.PublishEvent(ctx, "my-topic", data)
// publish a message to the topic example-topic
err = client.PublishEvent(ctx, "example-topic", data)
if err != nil {
panic(err)
}
fmt.Println("data published")
// save state with the key key1
err = client.SaveState(ctx, "my-store", "key1", data)
err = client.SaveState(ctx, "example-store", "key1", data)
if err != nil {
panic(err)
}
fmt.Println("data saved")
// get state for key key1
dataOut, err := client.GetState(ctx, "my-store", "key1")
dataOut, err := client.GetState(ctx, "example-store", "key1")
if err != nil {
panic(err)
}
fmt.Println(string(dataOut))
// delete state for key key1
err = client.DeleteState(ctx, "my-store", "key1")
err = client.DeleteState(ctx, "example-store", "key1")
if err != nil {
panic(err)
}
fmt.Println("data deleted")
// invoke output binding named 'kafka-topic-name'.
// invoke output binding named 'kafka-topic'.
// make sure you set up a dapr binding, otherwise this will fail
err = client.InvokeBinding(ctx, "kafka-topic-name", data)
err = client.InvokeBinding(ctx, "kafka-topic", data)
if err != nil {
panic(err)
}

View File

@ -0,0 +1,11 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: messagebus
spec:
type: pubsub.redis
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""

View File

@ -0,0 +1,13 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
spec:
type: state.redis
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
- name: actorStateStore
value: "true"

View File

@ -20,17 +20,17 @@ type server struct {
}
func main() {
// create listiner
// create listener
lis, err := net.Listen("tcp", ":4000")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
// create grpc server
// create gRPC server
s := grpc.NewServer()
pb.RegisterDaprClientServer(s, &server{})
fmt.Println("Client starting...")
fmt.Println("client starting...")
// and start...
if err := s.Serve(lis); err != nil {
@ -40,15 +40,15 @@ func main() {
// Sample method to invoke
func (s *server) MyMethod() string {
return "Hi there!"
return "pong"
}
// This method gets invoked when a remote service has called the app through Dapr
// This method gets invoked when a remote service has called the app through dapr
// The payload carries a Method to identify the method, a set of metadata properties and an optional payload
func (s *server) OnInvoke(ctx context.Context, in *pbc.InvokeRequest) (*pbc.InvokeResponse, error) {
var response string
fmt.Println(fmt.Sprintf("Got invoked with: %s", string(in.Data.Value)))
fmt.Println(fmt.Sprintf("got invoked with: %s", string(in.Data.Value)))
switch in.Method {
case "MyMethod":
@ -61,29 +61,31 @@ func (s *server) OnInvoke(ctx context.Context, in *pbc.InvokeRequest) (*pbc.Invo
}, nil
}
// Dapr will call this method to get the list of topics the app wants to subscribe to. In this example, we are telling Dapr
// To subscribe to a topic named TopicA
// GetTopicSubscriptions will call this method to get the list of topics the app wants to subscribe to.
// In this example, we are telling dapr. To subscribe to a topic named example-topic
func (s *server) GetTopicSubscriptions(ctx context.Context, in *empty.Empty) (*pb.GetTopicSubscriptionsEnvelope, error) {
return &pb.GetTopicSubscriptionsEnvelope{
Topics: []string{"TopicA"},
Topics: []string{"example-topic"},
}, nil
}
// Dapper will call this method to get the list of bindings the app will get invoked by. In this example, we are telling Dapr
// GetBindingsSubscriptions will call this method to get the list of bindings the app will get invoked by. In this example, we are telling dapr
// To invoke our app with a binding named storage
func (s *server) GetBindingsSubscriptions(ctx context.Context, in *empty.Empty) (*pb.GetBindingsSubscriptionsEnvelope, error) {
return &pb.GetBindingsSubscriptionsEnvelope{
Bindings: []string{"storage"},
Bindings: []string{"example-storage"},
}, nil
}
// This method gets invoked every time a new event is fired from a registerd binding. The message carries the binding name, a payload and optional metadata
// OnBindingEvent method gets invoked every time a new event is fired from a registered binding.
// The message carries the binding name, a payload and optional metadata
func (s *server) OnBindingEvent(ctx context.Context, in *pb.BindingEventEnvelope) (*pb.BindingResponseEnvelope, error) {
fmt.Println("Invoked from binding")
return &pb.BindingResponseEnvelope{}, nil
}
// This method is fired whenever a message has been published to a topic that has been subscribed. Dapr sends published messages in a CloudEvents 0.3 envelope.
// OnTopicEvent method is fired whenever a message has been published to a topic that has been subscribed.
// dapr sends published messages in a CloudEvents 0.3 envelope.
func (s *server) OnTopicEvent(ctx context.Context, in *pb.CloudEventEnvelope) (*empty.Empty, error) {
fmt.Println("Topic message arrived")
return &empty.Empty{}, nil