update with latest api and go-sdk (#733)

Co-authored-by: Yongguang Zhu <Yongguang.Zhu@microsoft.com>
This commit is contained in:
Yongguang Zhu 2020-08-14 06:27:56 +08:00 committed by GitHub
parent 40d02e77a3
commit 40050a51ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 61 additions and 47 deletions

View File

@ -5,10 +5,10 @@ gRPC is useful for low-latency, high performance scenarios and has deep language
You can find a list of autogenerated client [here](https://github.com/dapr/docs#sdks).
The Dapr runtime implements a [proto service](https://github.com/dapr/dapr/blob/master/pkg/proto/dapr/dapr.proto) that apps can communicate with via gRPC.
The Dapr runtime implements a [proto service](https://github.com/dapr/dapr/blob/master/dapr/proto/runtime/v1/dapr.proto) that apps can communicate with via gRPC.
In addition to talking to Dapr via gRPC, Dapr can communicate with an application via gRPC.
To do that, the app simply needs to host a gRPC server and implement the [Dapr client service](https://github.com/dapr/dapr/blob/master/pkg/proto/daprclient/daprclient.proto).
To do that, the app simply needs to host a gRPC server and implement the [Dapr appcallback service](https://github.com/dapr/dapr/blob/master/dapr/proto/runtime/v1/appcallback.proto).
## Configuring Dapr to communicate with app via gRPC
@ -48,7 +48,7 @@ This tells Dapr to communicate with your app via gRPC over port `5005`.
When running in standalone mode, use the `--protocol` flag to tell Dapr to use gRPC to talk to the app:
```bash
dapr run --protocol grpc --app-port 5005 node app.js
dapr run --protocol grpc --app-port 5005 -- node app.js
```
## Invoking Dapr - Go example
@ -61,39 +61,38 @@ The following steps will show you how to create a Dapr client and call the Save
package main
import (
pb "github.com/dapr/go-sdk/dapr"
"context"
"log"
"os"
dapr "github.com/dapr/go-sdk/client"
)
```
2. Create the client
```go
// Get the Dapr port and create a connection
daprPort := os.Getenv("DAPR_GRPC_PORT")
daprAddress := fmt.Sprintf("localhost:%s", daprPort)
conn, err := grpc.Dial(daprAddress, grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
defer conn.Close()
// Create the client
client := pb.NewDaprClient(conn)
// just for this demo
ctx := context.Background()
data := []byte("ping")
// create the client
client, err := dapr.NewClient()
if err != nil {
logger.Panic(err)
}
defer client.Close()
```
3. Invoke the Save State method
```go
_, err = client.SaveState(context.Background(), &pb.SaveStateEnvelope{
Requests: []*pb.StateRequest{
&pb.StateRequest{
Key: "myKey",
Value: &any.Any{
Value: []byte("My State"),
},
},
},
})
// save state with the key key1
err = client.SaveStateData(ctx, "statestore", "key1", "1", data)
if err != nil {
logger.Panic(err)
}
logger.Println("data saved")
```
Hooray!
@ -110,7 +109,17 @@ The following steps will show you how to create an app that exposes a server for
package main
import (
pb "github.com/dapr/go-sdk/daprclient"
"context"
"fmt"
"log"
"net"
"github.com/golang/protobuf/ptypes/any"
"github.com/golang/protobuf/ptypes/empty"
commonv1pb "github.com/dapr/go-sdk/dapr/proto/common/v1"
pb "github.com/dapr/go-sdk/dapr/proto/runtime/v1"
"google.golang.org/grpc"
)
```
@ -121,48 +130,53 @@ import (
type server struct {
}
// Sample method to invoke
func (s *server) MyMethod() string {
return "Hi there!"
// EchoMethod is a simple demo method to invoke
func (s *server) EchoMethod() string {
return "pong"
}
// 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 *pb.InvokeEnvelope) (*any.Any, error) {
func (s *server) OnInvoke(ctx context.Context, in *commonv1pb.InvokeRequest) (*commonv1pb.InvokeResponse, error) {
var response string
switch in.Method {
case "MyMethod":
response = s.MyMethod()
case "EchoMethod":
response = s.EchoMethod()
}
return &any.Any{
Value: []byte(response),
return &commonv1pb.InvokeResponse{
ContentType: "text/plain; charset=UTF-8",
Data: &any.Any{Value: []byte(response)},
}, 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
func (s *server) GetTopicSubscriptions(ctx context.Context, in *empty.Empty) (*pb.GetTopicSubscriptionsEnvelope, error) {
return &pb.GetTopicSubscriptionsEnvelope{
Topics: []string{"TopicA"},
func (s *server) ListTopicSubscriptions(ctx context.Context, in *empty.Empty) (*pb.ListTopicSubscriptionsResponse, error) {
return &pb.ListTopicSubscriptionsResponse{
Subscriptions: []*pb.TopicSubscription{
{Topic: "TopicA"},
},
}, nil
}
// Dapr 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{
func (s *server) ListInputBindings(ctx context.Context, in *empty.Empty) (*pb.ListInputBindingsResponse, error) {
return &pb.ListInputBindingsResponse{
Bindings: []string{"storage"},
}, nil
}
// This 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) {
// 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
func (s *server) OnBindingEvent(ctx context.Context, in *pb.BindingEventRequest) (*pb.BindingEventResponse, error) {
fmt.Println("Invoked from binding")
return &pb.BindingResponseEnvelope{}, nil
return &pb.BindingEventResponse{}, 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 1.0 envelope.
func (s *server) OnTopicEvent(ctx context.Context, in *pb.CloudEventEnvelope) (*empty.Empty, error) {
// 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.
func (s *server) OnTopicEvent(ctx context.Context, in *pb.TopicEventRequest) (*empty.Empty, error) {
fmt.Println("Topic message arrived")
return &empty.Empty{}, nil
}
@ -174,14 +188,14 @@ func (s *server) OnTopicEvent(ctx context.Context, in *pb.CloudEventEnvelope) (*
```go
func main() {
// create listener
lis, err := net.Listen("tcp", ":4000")
lis, err := net.Listen("tcp", ":50001")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
// create grpc server
s := grpc.NewServer()
pb.RegisterDaprClientServer(s, &server{})
pb.RegisterAppCallbackServer(s, &server{})
fmt.Println("Client starting...")
@ -199,7 +213,7 @@ This creates a gRPC server for your app on port 4000.
To run locally, use the Dapr CLI:
```
dapr run --app-id goapp --app-port 4000 --protocol grpc go run main.go
dapr run --app-id goapp --app-port 4000 --protocol grpc -- go run main.go
```
On Kubernetes, set the required `dapr.io/protocol: "grpc"` and `dapr.io/port: "4000` annotations in your pod spec template as mentioned above.