mirror of https://github.com/dapr/go-sdk.git
http binding
This commit is contained in:
parent
f184c9c943
commit
4f128a8329
|
|
@ -25,5 +25,3 @@ dapr run --app-id serving --protocol grpc --app-port 4000 go run main.go
|
|||
cd example/client
|
||||
dapr run --app-id caller go run main.go
|
||||
```
|
||||
|
||||
> 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`
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ package client
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
|
|
@ -16,6 +16,10 @@ const (
|
|||
daprPortEnvVarName = "DAPR_GRPC_PORT"
|
||||
)
|
||||
|
||||
var (
|
||||
logger = log.New(os.Stdout, "", 0)
|
||||
)
|
||||
|
||||
// NewClientWithAddress instantiates dapr client locally using port from DAPR_GRPC_PORT env var
|
||||
// When DAPR_GRPC_PORT client defaults to 4000
|
||||
func NewClient() (client *Client, err error) {
|
||||
|
|
@ -34,7 +38,7 @@ func NewClientWithPort(port string) (client *Client, err error) {
|
|||
|
||||
// NewClientWithAddress instantiates dapr client configured for the specific address
|
||||
func NewClientWithAddress(address string) (client *Client, err error) {
|
||||
fmt.Printf("dapr client initializing for: %s", address)
|
||||
logger.Printf("dapr client initializing for: %s", address)
|
||||
conn, err := grpc.Dial(address, grpc.WithInsecure())
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error creating connection to '%s': %v", address, err)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
apiVersion: dapr.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: example-http-binding
|
||||
spec:
|
||||
type: bindings.http
|
||||
metadata:
|
||||
- name: url
|
||||
value: https://http2.pro/api/v1
|
||||
- name: method
|
||||
value: GET
|
||||
|
||||
|
|
@ -19,13 +19,6 @@ func main() {
|
|||
}
|
||||
defer client.Close(ctx)
|
||||
|
||||
// invoke a method called MyMethod on another dapr enabled service with id client
|
||||
resp, err := client.InvokeService(ctx, "serving", "MyMethod", data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(string(resp))
|
||||
|
||||
// publish a message to the topic messagebus
|
||||
err = client.PublishEvent(ctx, "messagebus", data)
|
||||
if err != nil {
|
||||
|
|
@ -54,9 +47,16 @@ func main() {
|
|||
}
|
||||
fmt.Println("data deleted")
|
||||
|
||||
// invoke output binding named 'example-binding'.
|
||||
// make sure you set up a dapr binding, otherwise this will fail
|
||||
err = client.InvokeBinding(ctx, "example-binding", data)
|
||||
// invoke a method called MyMethod on another dapr enabled service with id client
|
||||
resp, err := client.InvokeService(ctx, "serving", "MyMethod", data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(string(resp))
|
||||
|
||||
// invoke output binding named 'example-http-binding'
|
||||
// uses https://http2.pro/doc/api to check for HTTP/2
|
||||
err = client.InvokeBinding(ctx, "example-http-binding", data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue