mirror of https://github.com/dapr/go-sdk.git
updated examples
This commit is contained in:
parent
dcb92e33aa
commit
c0c9f0166b
18
Readme.md
18
Readme.md
|
|
@ -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>
|
This is the dapr SDK (client) for Go.
|
||||||
|
|
||||||
For more info on Dapr and gRPC, visit [this link](https://github.com/dapr/docs/tree/master/howto/create-grpc-app).
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
|
@ -12,20 +10,20 @@ go get github.com/dapr/go-sdk
|
||||||
|
|
||||||
## Usage
|
## 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
|
cd example/serving
|
||||||
dapr run --app-id client --protocol grpc --app-port 4000 go run main.go
|
dapr run --app-id serving --protocol grpc --app-port 4000 go run main.go
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Run the caller
|
2. Run the caller
|
||||||
|
|
||||||
```
|
```
|
||||||
cd example/caller
|
cd example/client
|
||||||
dapr run --app-id caller go run main.go
|
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`
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
daprPortDefault = "50005"
|
daprPortDefault = "4000"
|
||||||
daprPortEnvVarName = "DAPR_GRPC_PORT"
|
daprPortEnvVarName = "DAPR_GRPC_PORT"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewClientWithAddress instantiates dapr client locally using port from DAPR_GRPC_PORT env var
|
// 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) {
|
func NewClient() (client *Client, err error) {
|
||||||
port := os.Getenv(daprPortEnvVarName)
|
port := os.Getenv(daprPortEnvVarName)
|
||||||
if port == "" {
|
if port == "" {
|
||||||
|
|
|
||||||
|
|
@ -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: ""
|
||||||
|
|
@ -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"
|
||||||
|
|
@ -20,43 +20,43 @@ func main() {
|
||||||
defer client.Close(ctx)
|
defer client.Close(ctx)
|
||||||
|
|
||||||
// invoke a method called MyMethod on another dapr enabled service with id client
|
// 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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Println(string(resp))
|
fmt.Println(string(resp))
|
||||||
|
|
||||||
// publish a message to the topic my-topic
|
// publish a message to the topic example-topic
|
||||||
err = client.PublishEvent(ctx, "my-topic", data)
|
err = client.PublishEvent(ctx, "example-topic", data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Println("data published")
|
fmt.Println("data published")
|
||||||
|
|
||||||
// save state with the key key1
|
// 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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Println("data saved")
|
fmt.Println("data saved")
|
||||||
|
|
||||||
// get state for key key1
|
// get state for key key1
|
||||||
dataOut, err := client.GetState(ctx, "my-store", "key1")
|
dataOut, err := client.GetState(ctx, "example-store", "key1")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Println(string(dataOut))
|
fmt.Println(string(dataOut))
|
||||||
|
|
||||||
// delete state for key key1
|
// delete state for key key1
|
||||||
err = client.DeleteState(ctx, "my-store", "key1")
|
err = client.DeleteState(ctx, "example-store", "key1")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Println("data deleted")
|
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
|
// 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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
@ -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: ""
|
||||||
|
|
@ -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"
|
||||||
|
|
@ -20,17 +20,17 @@ type server struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// create listiner
|
// create listener
|
||||||
lis, err := net.Listen("tcp", ":4000")
|
lis, err := net.Listen("tcp", ":4000")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to listen: %v", err)
|
log.Fatalf("failed to listen: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create grpc server
|
// create gRPC server
|
||||||
s := grpc.NewServer()
|
s := grpc.NewServer()
|
||||||
pb.RegisterDaprClientServer(s, &server{})
|
pb.RegisterDaprClientServer(s, &server{})
|
||||||
|
|
||||||
fmt.Println("Client starting...")
|
fmt.Println("client starting...")
|
||||||
|
|
||||||
// and start...
|
// and start...
|
||||||
if err := s.Serve(lis); err != nil {
|
if err := s.Serve(lis); err != nil {
|
||||||
|
|
@ -40,15 +40,15 @@ func main() {
|
||||||
|
|
||||||
// Sample method to invoke
|
// Sample method to invoke
|
||||||
func (s *server) MyMethod() string {
|
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
|
// 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) {
|
func (s *server) OnInvoke(ctx context.Context, in *pbc.InvokeRequest) (*pbc.InvokeResponse, error) {
|
||||||
var response string
|
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 {
|
switch in.Method {
|
||||||
case "MyMethod":
|
case "MyMethod":
|
||||||
|
|
@ -61,29 +61,31 @@ func (s *server) OnInvoke(ctx context.Context, in *pbc.InvokeRequest) (*pbc.Invo
|
||||||
}, nil
|
}, 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
|
// GetTopicSubscriptions will call this method to get the list of topics the app wants to subscribe to.
|
||||||
// To subscribe to a topic named TopicA
|
// 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) {
|
func (s *server) GetTopicSubscriptions(ctx context.Context, in *empty.Empty) (*pb.GetTopicSubscriptionsEnvelope, error) {
|
||||||
return &pb.GetTopicSubscriptionsEnvelope{
|
return &pb.GetTopicSubscriptionsEnvelope{
|
||||||
Topics: []string{"TopicA"},
|
Topics: []string{"example-topic"},
|
||||||
}, nil
|
}, 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
|
// To invoke our app with a binding named storage
|
||||||
func (s *server) GetBindingsSubscriptions(ctx context.Context, in *empty.Empty) (*pb.GetBindingsSubscriptionsEnvelope, error) {
|
func (s *server) GetBindingsSubscriptions(ctx context.Context, in *empty.Empty) (*pb.GetBindingsSubscriptionsEnvelope, error) {
|
||||||
return &pb.GetBindingsSubscriptionsEnvelope{
|
return &pb.GetBindingsSubscriptionsEnvelope{
|
||||||
Bindings: []string{"storage"},
|
Bindings: []string{"example-storage"},
|
||||||
}, nil
|
}, 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) {
|
func (s *server) OnBindingEvent(ctx context.Context, in *pb.BindingEventEnvelope) (*pb.BindingResponseEnvelope, error) {
|
||||||
fmt.Println("Invoked from binding")
|
fmt.Println("Invoked from binding")
|
||||||
return &pb.BindingResponseEnvelope{}, nil
|
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) {
|
func (s *server) OnTopicEvent(ctx context.Context, in *pb.CloudEventEnvelope) (*empty.Empty, error) {
|
||||||
fmt.Println("Topic message arrived")
|
fmt.Println("Topic message arrived")
|
||||||
return &empty.Empty{}, nil
|
return &empty.Empty{}, nil
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue