Fixes some Go samples (#782)

* Fixes service invocation Go samples

Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com>

* Fixes + updates to the pubsub sdk apps too

Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com>

* More updates

Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com>

---------

Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com>
This commit is contained in:
Alessandro (Ale) Segala 2023-02-02 16:31:32 -08:00 committed by GitHub
parent 332ac444a4
commit b91d817ca5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
44 changed files with 352 additions and 359 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,39 +1,23 @@
# Dapr pub/sub
In this quickstart, you'll create a publisher microservice and a subscriber microservice to demonstrate how Dapr enables a publish-subcribe API. The publisher generates messages for a specific topic, while subscribers listen for messages on specific topics.
In this quickstart, you'll create a publisher microservice and a subscriber microservice to demonstrate how Dapr enables a publish-subcribe pattern. The publisher generates messages for a specific topic, while the subscriber listen for messages in specific topics.
Visit [this](https://docs.dapr.io/developing-applications/building-blocks/pubsub/) link for more information about Dapr and Pub-Sub.
Check out the documentation about [Dapr pubsub](https://docs.dapr.io/developing-applications/building-blocks/pubsub/) for more details.
> **Note:** This example leverages HTTP `requests` only. If you are looking for the example using the Dapr Client SDK (recommended) [click here](../sdk/).
> **Note:** This example leverages HTTP `requests` only. You can find an example of using the Dapr Client SDK (recommended) in the [`sdk` folder](../sdk/).
This quickstart includes one publisher:
This quickstart includes one publisher: Go client message generator `checkout`.
- Go client message generator `checkout`
And one subscriber:
- Go subscriber `order-processor`
And one subscriber: Go subscriber `order-processor`.
### Run Go message subscriber with Dapr
1. Navigate to the directory and install dependencies:
<!-- STEP
name: Build Go file
-->
```bash
cd ./order-processor
go build .
```
<!-- END_STEP -->
2. Run the Go subscriber app with Dapr:
1. Run the Go subscriber app with Dapr in the `order-processor` folder:
<!-- STEP
name: Run Go subscriber
expected_stdout_lines:
- '== APP == Subscriber received: {"orderId":10}'
- '== APP == Subscriber received: {"orderId":10}'
- "Exited App successfully"
expected_stderr_lines:
output_match_mode: substring
@ -41,28 +25,22 @@ background: true
sleep: 15
-->
```bash
cd ./order-processor
dapr run --app-port 6001 --app-id order-processor --app-protocol http --dapr-http-port 3501 --components-path ../../../components -- go run .
dapr run \
--app-port 6001 \
--app-id order-processor \
--app-protocol http \
--dapr-http-port 3501 \
--components-path ../../../components \
-- go run .
```
<!-- END_STEP -->
### Run Go message publisher with Dapr
1. Navigate to the directory and install dependencies:
<!-- STEP
name: Build Go file
-->
```bash
cd ./checkout
go build .
```
<!-- END_STEP -->
2. Run the Go publisher app with Dapr:
1. Run the Go publisher app with Dapr in the `checkout` folder:
<!-- STEP
name: Run Go publisher
@ -75,14 +53,20 @@ output_match_mode: substring
background: true
sleep: 15
-->
```bash
cd ./checkout
dapr run --app-id checkout-http --app-protocol http --dapr-http-port 3500 --components-path ../../../components -- go run .
dapr run \
--app-id checkout-http \
--dapr-http-port 3500 \
--components-path ../../../components \
-- go run .
```
<!-- END_STEP -->
To stop:
```bash
dapr stop --app-id checkout-http
dapr stop --app-id order-processor

View File

@ -10,33 +10,38 @@ import (
"time"
)
const PUBSUB_NAME = "orderpubsub"
const PUBSUB_TOPIC = "orders"
const pubsubComponentName = "orderpubsub"
const pubsubTopic = "orders"
func main() {
daprHost := "http://localhost"
if value, ok := os.LookupEnv("DAPR_HOST"); ok {
daprHost = value
daprHost := os.Getenv("DAPR_HOST")
if daprHost == "" {
daprHost = "http://localhost"
}
daprHttpPost := "3500"
if value, ok := os.LookupEnv("DAPR_HTTP_PORT"); ok {
daprHttpPost = value
daprHttpPort := os.Getenv("DAPR_HTTP_PORT")
if daprHttpPort == "" {
daprHttpPort = "3500"
}
client := http.Client{
Timeout: 15 * time.Second,
}
for i := 1; i <= 10; i++ {
order := `{"orderId":` + strconv.Itoa(i) + `}`
client := http.Client{}
req, err := http.NewRequest("POST", daprHost+":"+daprHttpPost+"/v1.0/publish/"+PUBSUB_NAME+"/"+PUBSUB_TOPIC, strings.NewReader(order))
req, err := http.NewRequest("POST", daprHost+":"+daprHttpPort+"/v1.0/publish/"+pubsubComponentName+"/"+pubsubTopic, strings.NewReader(order))
if err != nil {
log.Fatal(err.Error())
os.Exit(1)
}
// Publish an event using Dapr pub/sub
if _, err = client.Do(req); err != nil {
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
fmt.Println("Published data: ", order)
fmt.Println("Published data:", order)
time.Sleep(1000)
}

View File

@ -1,3 +1,3 @@
module dapr_example
module checkout_example
go 1.18
go 1.19

View File

@ -31,11 +31,11 @@ func getOrder(w http.ResponseWriter, r *http.Request) {
}
jsonBytes, err := json.Marshal(jsonData)
if err != nil {
log.Fatal("Error in reading the result obj")
log.Fatal(err.Error())
}
_, err = w.Write(jsonBytes)
if err != nil {
log.Fatal("Error in writing the result obj")
log.Fatal(err.Error())
}
}
@ -47,33 +47,36 @@ func postOrder(w http.ResponseWriter, r *http.Request) {
var result Result
err = json.Unmarshal(data, &result)
if err != nil {
log.Fatal(err)
log.Fatal(err.Error())
}
fmt.Println("Subscriber received: ", string(result.Data))
fmt.Println("Subscriber received:", string(result.Data))
obj, err := json.Marshal(data)
if err != nil {
log.Fatal("Error in reading the result obj")
log.Fatal(err.Error())
}
_, err = w.Write(obj)
if err != nil {
log.Fatal("Error in writing the result obj")
log.Fatal(err.Error())
}
}
func main() {
appPort := "6001"
if value, ok := os.LookupEnv("APP_PORT"); ok {
appPort = value
appPort := os.Getenv("APP_PORT")
if appPort == "" {
appPort = "6001"
}
r := mux.NewRouter()
// Handle the /dapr/subscribe route which Dapr invokes to get the list of subscribed endpoints
r.HandleFunc("/dapr/subscribe", getOrder).Methods("GET")
// Dapr subscription routes orders topic to this route
r.HandleFunc("/orders", postOrder).Methods("POST")
if err := http.ListenAndServe(":"+appPort, r); err != nil {
// Start the server; this is a blocking call
err := http.ListenAndServe(":"+appPort, r)
if err != http.ErrServerClosed {
log.Panic(err)
}
}

View File

@ -1,5 +1,5 @@
module dapr_example
module order_processor_example
go 1.18
go 1.19
require github.com/gorilla/mux v1.8.0

View File

@ -1,39 +1,23 @@
# Dapr pub/sub
In this quickstart, you'll create a publisher microservice and a subscriber microservice to demonstrate how Dapr enables a publish-subcribe pattern. The publisher generates messages of a specific topic, while subscribers listen for messages of specific topics.
In this quickstart, you'll create a publisher microservice and a subscriber microservice to demonstrate how Dapr enables a publish-subcribe pattern. The publisher generates messages for a specific topic, while the subscriber listen for messages in specific topics.
Visit [this](https://docs.dapr.io/developing-applications/building-blocks/pubsub/) link for more information about Dapr and Pub-Sub.
Check out the documentation about [Dapr pubsub](https://docs.dapr.io/developing-applications/building-blocks/pubsub/) for more details.
> **Note:** This example leverages the Dapr client SDK. If you are looking for the example using only HTTP `requests` [click here](../http).
> **Note:** This example leverages the Dapr Client SDK. You can find an example using plain HTTP in the [`http` folder](../http/).
This quickstart includes one publisher:
This quickstart includes one publisher: Go client message generator `checkout`
- Go client message generator `checkout`
And one subscriber:
- Go subscriber `order-processor`
And one subscriber: Go subscriber `order-processor`
### Run Go message subscriber with Dapr
1. Navigate to the directory and install dependencies:
<!-- STEP
name: Build Go file
-->
```bash
cd ./order-processor
go build .
```
<!-- END_STEP -->
2. Run the Go subscriber app with Dapr:
1. Run the Go subscriber app with Dapr in the `order-processor` folder:
<!-- STEP
name: Run Go subscriber
expected_stdout_lines:
- '== APP == Subscriber received: map[orderId:10]'
- '== APP == Subscriber received: map[orderId:10]'
- "Exited App successfully"
expected_stderr_lines:
output_match_mode: substring
@ -43,45 +27,46 @@ sleep: 15
```bash
cd ./order-processor
dapr run --app-port 6002 --app-id order-processor-sdk --app-protocol http --dapr-http-port 3501 --components-path ../../../components -- go run .
dapr run \
--app-port 6002 \
--app-id order-processor-sdk \
--app-protocol http \
--dapr-http-port 3501 \
--components-path ../../../components \
-- go run .
```
<!-- END_STEP -->
### Run Go message publisher with Dapr
1. Navigate to the directory and install dependencies:
<!-- STEP
name: Build Go file
-->
```bash
cd ./checkout
go build .
```
<!-- END_STEP -->
2. Run the Go publisher app with Dapr:
1 Run the Go publisher app with Dapr in the `checkout` folder:
<!-- STEP
name: Run Go publisher
expected_stdout_lines:
- '== APP == Published data: {"orderId":1}'
- '== APP == Published data: {"orderId":2}'
- '== APP == Published data: {"orderId":1}'
- '== APP == Published data: {"orderId":2}'
- "Exited App successfully"
expected_stderr_lines:
output_match_mode: substring
background: true
sleep: 15
-->
```bash
cd ./checkout
dapr run --app-id checkout-sdk --app-protocol http --dapr-http-port 3500 --components-path ../../../components -- go run .
dapr run \
--app-id checkout-sdk \
--dapr-http-port 3500 \
--components-path ../../../components \
-- go run .
```
<!-- END_STEP -->
To stop:
```bash
dapr stop --app-id checkout-sdk
dapr stop --app-id order-processor-sdk

View File

@ -9,27 +9,29 @@ import (
dapr "github.com/dapr/go-sdk/client"
)
var (
PUBSUB_NAME = "orderpubsub"
PUBSUB_TOPIC = "orders"
const (
pubsubComponentName = "orderpubsub"
pubsubTopic = "orders"
)
func main() {
// Create a new client for Dapr using the SDK
client, err := dapr.NewClient()
if err != nil {
panic(err)
}
defer client.Close()
ctx := context.Background()
// Publish events using Dapr pubsub
for i := 1; i <= 10; i++ {
order := `{"orderId":` + strconv.Itoa(i) + `}`
// Publish an event using Dapr pub/sub
if err := client.PublishEvent(ctx, PUBSUB_NAME, PUBSUB_TOPIC, []byte(order)); err != nil {
err := client.PublishEvent(context.Background(), pubsubComponentName, pubsubTopic, []byte(order))
if err != nil {
panic(err)
}
fmt.Println("Published data: ", order)
fmt.Println("Published data:", order)
time.Sleep(1000)
}

Binary file not shown.

View File

@ -1,11 +1,10 @@
module dapr_example
module checkout_sdk_example
go 1.18
go 1.19
require github.com/dapr/go-sdk v1.5.0
require github.com/dapr/go-sdk v1.6.0
require (
github.com/dapr/dapr v1.8.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/net v0.0.0-20220621193019-9d032be2e588 // indirect

View File

@ -11,10 +11,8 @@ github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XP
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/dapr/dapr v1.8.0 h1:ZAAoBe6wuFp7k4tIHB7ajZXVTtGeDeVqIPrldzo3dF0=
github.com/dapr/dapr v1.8.0/go.mod h1:yAsDiK5oecG0htw2S8JG9RFaeHJVdlTfZyOrL57AvRM=
github.com/dapr/go-sdk v1.5.0 h1:OVkrupquJEOL1qRtwKcMVrFKYhw4UJQvgOJNduo2VxE=
github.com/dapr/go-sdk v1.5.0/go.mod h1:Cvz3taCVu22WCNEUbc9/szvG/yJxWPAV4dcaG+zDWA4=
github.com/dapr/go-sdk v1.6.0 h1:jg5A2khSCHF8bGZsig5RWN/gD0jjitszc2V6Uq2pPdY=
github.com/dapr/go-sdk v1.6.0/go.mod h1:KLQBltoD9K0w5hKTihdcyg9Epob9gypwL5dYcQzPro4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@ -51,6 +49,8 @@ github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@ -140,8 +140,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -18,21 +18,26 @@ var sub = &common.Subscription{
}
func main() {
appPort := "6002"
if value, ok := os.LookupEnv("APP_PORT"); ok {
appPort = value
appPort := os.Getenv("APP_PORT")
if appPort == "" {
appPort = "6002"
}
// Create the new server on appPort and add a topic listener
s := daprd.NewService(":" + appPort)
if err := s.AddTopicEventHandler(sub, eventHandler); err != nil {
err := s.AddTopicEventHandler(sub, eventHandler)
if err != nil {
log.Fatalf("error adding topic subscription: %v", err)
}
if err := s.Start(); err != nil && err != http.ErrServerClosed {
// Start the server
err = s.Start()
if err != nil && err != http.ErrServerClosed {
log.Fatalf("error listenning: %v", err)
}
}
func eventHandler(ctx context.Context, e *common.TopicEvent) (retry bool, err error) {
fmt.Println("Subscriber received: ", e.Data)
fmt.Println("Subscriber received:", e.Data)
return false, nil
}

View File

@ -1,11 +1,10 @@
module dapr_example
module order_processor_sdk_example
go 1.18
go 1.19
require github.com/dapr/go-sdk v1.5.0
require github.com/dapr/go-sdk v1.6.0
require (
github.com/dapr/dapr v1.8.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/pkg/errors v0.9.1 // indirect

View File

@ -11,10 +11,8 @@ github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XP
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/dapr/dapr v1.8.0 h1:ZAAoBe6wuFp7k4tIHB7ajZXVTtGeDeVqIPrldzo3dF0=
github.com/dapr/dapr v1.8.0/go.mod h1:yAsDiK5oecG0htw2S8JG9RFaeHJVdlTfZyOrL57AvRM=
github.com/dapr/go-sdk v1.5.0 h1:OVkrupquJEOL1qRtwKcMVrFKYhw4UJQvgOJNduo2VxE=
github.com/dapr/go-sdk v1.5.0/go.mod h1:Cvz3taCVu22WCNEUbc9/szvG/yJxWPAV4dcaG+zDWA4=
github.com/dapr/go-sdk v1.6.0 h1:jg5A2khSCHF8bGZsig5RWN/gD0jjitszc2V6Uq2pPdY=
github.com/dapr/go-sdk v1.6.0/go.mod h1:KLQBltoD9K0w5hKTihdcyg9Epob9gypwL5dYcQzPro4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@ -54,6 +52,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@ -143,8 +143,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -5,5 +5,6 @@ Dapr provides a capability for defining and applying fault tolerance resiliency
In this Quickstart, you will observe Dapr resiliency capabilities by simulating a system failure. You have the option of using the service invocation quickstarts to demonstrate Dapr resiliency between service-to-service communication, or the state management quickstart to demonstrate resiliency between apps and components.
### Select resiliency quickstart
- For service-to-service resiliency, see [here](./service-to-service-resiliency.md)
- For service to component resiliency, see [here](./service-to-component-resiliency.md)
- For service-to-component resiliency, see [here](./service-to-component-resiliency.md)

View File

@ -2,11 +2,11 @@
In this QuickStart, you will run a microservice application that continuously persists and retrieves state via Dapr's state management API. When operations to the state store begin to fail, Dapr resiliency policies are applied.
Visit [this](https://docs.dapr.io/operations/resiliency/resiliency-overview//) link for more information about Dapr resiliency.
Visit the documentation about [Dapr resiliency](https://docs.dapr.io/operations/resiliency/resiliency-overview/) link for more information
This quickstart includes one service:
- Client service `order-processor`
- Client service `order-processor`
- Redis component spec `statestore.yaml`
- Resiliency spec `resiliency.yaml`
@ -14,7 +14,8 @@ This quickstart includes one service:
1. Navigate to the app directory, install dependencies, and run the service with resiliency enabled via the config.yaml:
### CSharp example:
### C# example:
```bash
cd ../state_management/csharp/sdk/order-processor
dotnet restore
@ -23,6 +24,7 @@ dapr run --app-id order-processor --config ../config.yaml --components-path ../.
```
### Go example:
```bash
cd ../state_management/go/sdk/order-processor
go build .
@ -30,6 +32,7 @@ dapr run --app-id order-processor --config ../config.yaml --components-path ../.
```
### Java example:
```bash
cd ../state_management/java/sdk/order-processor
mvn clean install
@ -37,6 +40,7 @@ dapr run --app-id order-processor --config ../config.yaml --components-path ../.
```
### JavaScript example:
```bash
cd ../state_management/javascript/sdk/order-processor
npm install
@ -44,6 +48,7 @@ dapr run --app-id order-processor ../config.yaml --components-path ../../../comp
```
### Python example:
```bash
cd ../state_management/python/sdk/order-processor
pip3 install -r requirements.txt
@ -51,6 +56,7 @@ dapr run --app-id order-processor ../config.yaml --components-path ../../../comp
```
### Expected output:
```bash
== APP == Saving Order: { orderId: '1' }
== APP == Getting Order: { orderId: '1' }
@ -64,6 +70,7 @@ dapr run --app-id order-processor ../config.yaml --components-path ../../../comp
<!-- END_STEP -->
### Simulate a component failure by stopping the Redis container instance
In a new terminal window, stop the Redis container that's running on your machine:
```bash
@ -73,6 +80,7 @@ docker stop dapr_redis
### Observe retry and circuit breaker policies are applied:
Policies defined in the resiliency.yaml spec:
```yaml
retryForever:
policy: constant
@ -87,6 +95,7 @@ circuitBreakers:
```
Applied policies:
```bash
INFO[0006] Error processing operation component[statestore] output. Retrying...
INFO[0026] Circuit breaker "simpleCB-statestore" changed state from closed to open
@ -100,7 +109,7 @@ INFO[0031] Circuit breaker "simpleCB-statestore" changed state from half-open to
docker start dapr_redis
```
### Observe orders have resumed sequentially:
### Observe orders have resumed sequentially
```bash
INFO[0036] Recovered processing operation component[statestore] output.
@ -117,6 +126,7 @@ INFO[0036] Recovered processing operation component[statestore] output.
```
### Stop the app with Dapr
```bash
dapr stop --app-id order-processor
```

View File

@ -2,7 +2,7 @@
In this QuickStart, you will run two microservice applications. One microservice (`checkout`) will continuously make Dapr service invocation requests to the other microservice (`order-processor`). When requests begin to fail, Dapr resiliency policies are applied.
Visit [this](https://docs.dapr.io/operations/resiliency/resiliency-overview//) link for more information about Dapr resiliency.
Visit the documentation about [Dapr resiliency](https://docs.dapr.io/operations/resiliency/resiliency-overview/) link for more information
This quickstart includes one service:
@ -14,8 +14,10 @@ This quickstart includes one service:
1. Open two terminal windows. In one terminal window, navigate to the `checkout` service. In the other terminal window, navigate to the `order-processor` service. Install dependencies for each service and run both services with resiliency enabled via the config.yaml:
### CSharp example:
##### Order Processor Service:
### C# example
##### Order Processor Service:
```bash
cd ../service_invocation/csharp/http/order-processor
dotnet restore
@ -23,7 +25,8 @@ dotnet build
dapr run --app-port 7001 --app-id order-processor --config ../config.yaml --components-path ../../../components/ --app-protocol http --dapr-http-port 3501 -- dotnet run
```
##### Checkout Service:
##### Checkout Service:
```bash
cd ../service_invocation/csharp/http/checkout
dotnet restore
@ -31,93 +34,102 @@ dotnet build
dapr run --app-id checkout --config ../config.yaml --components-path ../../../components/ --app-protocol http --dapr-http-port 3500 -- dotnet run
```
### Go example:
##### Order Processor Service:
### Go example
##### Order Processor Service:
```bash
cd ../service_invocation/go/http/order-processor
go build .
dapr run --app-port 6001 --app-id order-processor --config ../config.yaml --components-path ../../../components/ --app-protocol http --dapr-http-port 3501 -- go run .
```
##### Checkout Service:
##### Checkout Service:
```bash
cd ../service_invocation/go/http/checkout
go build .
dapr run --app-id checkout --config ../config.yaml --components-path ../../../components/ --app-protocol http --dapr-http-port 3500 -- go run .
```
### Java example:
##### Order Processor Service:
### Java example
##### Order Processor Service
```bash
cd ../service_invocation/java/http/order-processor
mvn clean install
dapr run --app-id order-processor --config ../config.yaml --components-path ../../../components/ --app-port 9001 --app-protocol http --dapr-http-port 3501 -- java -jar target/OrderProcessingService-0.0.1-SNAPSHOT.jar
```
##### Checkout Service:
##### Checkout Service
```bash
cd ../service_invocation/java/http/checkout
mvn clean install
dapr run --app-id checkout --config ../config.yaml --components-path ../../../components/ --app-protocol http --dapr-http-port 3500 -- java -jar target/CheckoutService-0.0.1-SNAPSHOT.jar
```
### JavaScript example:
##### Order Processor Service:
### JavaScript example
##### Order Processor Service
```bash
cd ../service_invocation/javascript/http/order-processor
npm install
dapr run --app-port 5001 --app-id order-processor --config ../config.yaml --components-path ../../../components/ --app-protocol http --dapr-http-port 3501 -- npm start
```
##### Checkout Service:
##### Checkout Service
```bash
cd ../service_invocation/javascript/http/checkout
npm install
dapr run --app-id checkout --config ../config.yaml --components-path ../../../components/ --app-protocol http --dapr-http-port 3500 -- npm start
```
### Python example:
##### Order Processor Service:
### Python example
##### Order Processor Service
```bash
cd ../service_invocation/python/http/order-processor
pip3 install -r requirements.txt
dapr run --app-port 8001 --app-id order-processor --config ../config.yaml --components-path ../../../components/ --app-protocol http --dapr-http-port 3501 -- python3 app.py
```
##### Checkout Service:
##### Checkout Service
```bash
cd ../service_invocation/python/http/checkout
pip3 install -r requirements.txt
dapr run --app-id checkout --config ../config.yaml --components-path ../../../components/ --app-protocol http --dapr-http-port 3500 -- python3 app.py
```
### Expected output:
### Expected output
Once both services are running, the `order-processor` service will recieve orders from the `checkout`service continuously using Dapr's service invoke API.
##### `order-processor` output:
##### `order-processor` output
```bash
== APP == Order received: { orderId: 1 }
== APP == Order received: { orderId: 2 }
== APP == Order received: { orderId: 3 }
```
### Simulate ann application failure by stopping the `order-processor` service
### Simulate ann application failure by stopping the `order-processor` service
Simulate a system failure by stopping the `order-processor` service running in your second terminal window.
##### Windows
```script
CTRL + C
```
##### Mac
```script
CMD + C
```
### Observe retry and circuit breaker policies are applied:
### Observe retry and circuit breaker policies are applied
Policies defined in the resiliency.yaml spec:
```yaml
retryForever:
policy: constant
@ -131,7 +143,8 @@ circuitBreakers:
trip: consecutiveFailures >= 5
```
##### Applied policies:
##### Applied policies
```bash
INFO[0005] Error processing operation endpoint[order-processor, order-processor:orders]. Retrying...
INFO[0025] Circuit breaker "order-processor:orders" changed state from closed to open
@ -140,40 +153,53 @@ INFO[0030] Circuit breaker "order-processor:orders" changed state from half-open
```
### Simulate the application recovering by restarting the order-processor service:
Simulate the `order-processor` service recovering by restarting the application using the `dapr run` command:
### CSharp example:
##### Order Processor Service:
### C# example
##### Order Processor Service
```bash
dapr run --app-port 7001 --app-id order-processor --config ../config.yaml --components-path --app-protocol http --dapr-http-port 3501 -- dotnet run
```
### Go example:
##### Order Processor Service:
### Go example
##### Order Processor Service
```bash
dapr run --app-port 6001 --app-id order-processor --config ../config.yaml --components-path --app-protocol http --dapr-http-port 3501 -- go run .
```
### Java example:
##### Order Processor Service:
### Java example
##### Order Processor Service
```bash
dapr run --app-id order-processor --config ../config.yaml --components-path --app-port 9001 --app-protocol http --dapr-http-port 3501 -- java -jar target/OrderProcessingService-0.0.1-SNAPSHOT.jar
```
### JavaScript example:
##### Order Processor Service:
### JavaScript example
##### Order Processor Service
```bash
dapr run --app-port 5001 --app-id order-processor --config ../config.yaml --components-path --app-protocol http --dapr-http-port 3501 -- npm start
```
### Python example:
##### Order Processor Service:
### Python example
##### Order Processor Service:
```bash
dapr run --app-port 8001 --app-id order-processor --config ../config.yaml --components-path --app-protocol http --dapr-http-port 3501 -- python3 app.py
```
### Observe orders have resumed sequentially:
##### `order-processor` output:
```bash
== APP == Order received: { orderId: 4 }
== APP == Order received: { orderId: 5 }
@ -182,8 +208,8 @@ dapr run --app-port 8001 --app-id order-processor --config ../config.yaml --comp
```
### Stop the apps with Dapr
```bash
dapr stop --app-id order-processor
dapr stop --app-id checkout
```

View File

@ -1,38 +1,21 @@
# Service Invocation
In this quickstart, you'll create a checkout service and an order processor service to demonstrate how to use the service invocation API. The checkout service uses Dapr's http proxying capability to invoke a method on the order processing service.
In this quickstart, you'll create a checkout service and an order-processor service to demonstrate how to use the service invocation API. The checkout service uses Dapr's HTTP proxying capability to invoke a method on the order processing service.
Visit [this](https://docs.dapr.io/developing-applications/building-blocks/service-invocation/) link for more information about Dapr and service invocation.
Check out the documentation about [service invocation](https://docs.dapr.io/developing-applications/building-blocks/service-invocation/) for more details.
This quickstart includes one checkout service:
This quickstart includes one checkout service: Go client service `checkout`
- Go client service `checkout`
And one order processor service:
- Go order-processor service `order-processor`
And one order processor service: Go order-processor service `order-processor`
### Run Go order-processor with Dapr
1. Open a new terminal window and navigate to `order-processor` directory:
<!-- STEP
name: Build Go file
-->
```bash
cd ./order-processor
go build .
```
<!-- END_STEP -->
2. Run the Go order-processor app with Dapr:
1. Run the order-processor app with Dapr in the `order-processor` folder:
<!-- STEP
name: Run order-processor service
expected_stdout_lines:
- '== APP == Order received : {"orderId":10}'
- '== APP == Order received: {"orderId":10}'
- "Exited App successfully"
expected_stderr_lines:
output_match_mode: substring
@ -42,46 +25,44 @@ sleep: 15
```bash
cd ./order-processor
dapr run --app-port 6001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- go run .
dapr run \
--app-port 6001 \
--app-id order-processor \
--app-protocol http \
--dapr-http-port 3501 \
-- go run .
```
<!-- END_STEP -->
### Run Go checkout with Dapr
1. Open a new terminal window and navigate to `checkout` directory:
<!-- STEP
name: Build Go file
-->
```bash
cd ./checkout
go build .
```
<!-- END_STEP -->
2. Run the Go checkout app with Dapr:
1. Open a new terminal window and navigate to `checkout` directory, then run the Go checkout app with Dapr:
<!-- STEP
name: Run checkout service
expected_stdout_lines:
- '== APP == Order passed: {"orderId":1}'
- '== APP == Order passed: {"orderId":2}'
- '== APP == Order passed: {"orderId":1}'
- '== APP == Order passed: {"orderId":2}'
- "Exited App successfully"
expected_stderr_lines:
output_match_mode: substring
background: true
sleep: 15
-->
```bash
cd ./checkout
dapr run --app-id checkout --app-protocol http --dapr-http-port 3500 -- go run .
dapr run \
--app-id checkout \
--dapr-http-port 3500 \
-- go run .
```
<!-- END_STEP -->
To stop:
```bash
dapr stop --app-id checkout
dapr stop --app-id order-processor

View File

@ -8,41 +8,44 @@ import (
"os"
"strconv"
"strings"
"time"
)
func main() {
var DAPR_HOST, DAPR_HTTP_PORT string
var okHost, okPort bool
if DAPR_HOST, okHost = os.LookupEnv("DAPR_HOST"); !okHost {
DAPR_HOST = "http://localhost"
daprHost := os.Getenv("DAPR_HOST")
if daprHost == "" {
daprHost = "http://localhost"
}
if DAPR_HTTP_PORT, okPort = os.LookupEnv("DAPR_HTTP_PORT"); !okPort {
DAPR_HTTP_PORT = "3500"
daprHttpPort := os.Getenv("DAPR_HTTP_PORT")
if daprHttpPort == "" {
daprHttpPort = "3500"
}
client := &http.Client{
Timeout: 15 * time.Second,
}
for i := 1; i <= 20; i++ {
order := "{\"orderId\":" + strconv.Itoa(i) + "}"
client := &http.Client{}
req, err := http.NewRequest("POST", DAPR_HOST+":"+DAPR_HTTP_PORT+"/orders", strings.NewReader(order))
order := `{"orderId":` + strconv.Itoa(i) + "}"
req, err := http.NewRequest("POST", daprHost+":"+daprHttpPort+"/orders", strings.NewReader(order))
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
log.Fatal(err.Error())
}
// Adding app id as part of th header
req.Header.Add("dapr-app-id", "order-processor")
// Invoking a service
response, err := client.Do(req)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
log.Fatal(err.Error())
}
// Read the response
result, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
response.Body.Close()
fmt.Println("Order passed: ", string(result))
fmt.Println("Order passed:", string(result))
}
}

View File

@ -1,3 +1,3 @@
module dapr_example
module checkout_example
go 1.18
go 1.19

View File

@ -1,8 +0,0 @@
apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: config
spec:
features:
- name: Resiliency
enabled: true

View File

@ -12,17 +12,24 @@ import (
func getOrder(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
log.Println("Error reading body:", err.Error())
}
fmt.Println("Order received : ", string(data))
fmt.Println("Order received:", string(data))
_, err = w.Write(data)
if err != nil {
log.Println("Error in writing the result obj")
log.Println("Error writing the response:", err.Error())
}
}
func main() {
// Create a new router and respond to POST /orders requests
r := mux.NewRouter()
r.HandleFunc("/orders", getOrder).Methods("POST")
_ = http.ListenAndServe(":6001", r)
// Start the server listening on port 6001
// This is a blocking call
err := http.ListenAndServe(":6001", r)
if err != http.ErrServerClosed {
log.Println("Error starting HTTP server")
}
}

View File

@ -1,5 +1,5 @@
module dapr_example
module order_processor_example
go 1.18
go 1.19
require github.com/gorilla/mux v1.8.0

View File

@ -1,50 +1,35 @@
# Dapr state management (HTTP Client)
In this quickstart, you'll create a microservice to demonstrate Dapr's state management API. The service generates messages to store data in a state store. See [Why state management](#why-state-management) to understand when this pattern might be a good choice for your software architecture.
In this quickstart, you'll create a microservice to demonstrate Dapr's state management API. The service generates messages to store data in a state store.
Visit [this](https://docs.dapr.io/developing-applications/building-blocks/state-management/) link for more information about Dapr and State Management.
Visit the Dapr documentation on [State Management](https://docs.dapr.io/developing-applications/building-blocks/state-management/) for more information.
> **Note:** This example leverages HTTP `requests` only. If you are looking for the example using the Dapr Client SDK (recommended) [click here](../sdk/).
> **Note:** This example leverages plain HTTP. You can find an example using the Dapr Client SDK (recommended) in the [`sdk` folder](../sdk/).
This quickstart includes one service:
- Go client service `order-processor`
This quickstart includes one service: Go client service `order-processor`
### Run Go service with Dapr
1. Open a new terminal window and navigate to `order-processor` directory:
<!-- STEP
name: Build Go file
-->
```bash
cd ./order-processor
go build .
```
<!-- END_STEP -->
2. Run the Go service app with Dapr:
1. Run the Go service app with Dapr in the `order-processor` folder:
<!-- STEP
name: Run order-processor service
expected_stdout_lines:
- '== APP == Getting Order: "{\"orderId\":1}"'
- '== APP == Getting Order: "{\"orderId\":2}"'
- '== APP == Retrieved Order: "{\"orderId\":1}"'
- '== APP == Retrieved Order: "{\"orderId\":2}"'
- "Exited App successfully"
expected_stderr_lines:
output_match_mode: substring
background: true
sleep: 15
-->
```bash
cd ./order-processor
dapr run --app-id order-processor --components-path ../../../components -- go run .
dapr run \
--app-id order-processor \
--components-path ../../../components \
-- go run .
```
<!-- END_STEP -->
```bash
dapr stop --app-id order-processor
```

View File

@ -12,43 +12,63 @@ import (
"time"
)
const stateStoreComponentName = "statestore"
func main() {
var DAPR_HOST, DAPR_HTTP_PORT string
var okHost, okPort bool
if DAPR_HOST, okHost = os.LookupEnv("DAPR_HOST"); !okHost {
DAPR_HOST = "http://localhost"
daprHost := os.Getenv("DAPR_HOST")
if daprHost == "" {
daprHost = "http://localhost"
}
if DAPR_HTTP_PORT, okPort = os.LookupEnv("DAPR_HTTP_PORT"); !okPort {
DAPR_HTTP_PORT = "3500"
daprHttpPort := os.Getenv("DAPR_HTTP_PORT")
if daprHttpPort == "" {
daprHttpPort = "3500"
}
client := http.Client{
Timeout: 15 * time.Second,
}
DAPR_STATE_STORE := "statestore"
for i := 1; i <= 100; i++ {
orderId := i
order := "{\"orderId\":" + strconv.Itoa(orderId) + "}"
order := `{"orderId":` + strconv.Itoa(orderId) + "}"
state, _ := json.Marshal([]map[string]string{
{"key": strconv.Itoa(orderId), "value": order},
{
"key": strconv.Itoa(orderId),
"value": order,
},
})
responseBody := bytes.NewBuffer(state)
// Save state into a state store
_, _ = http.Post(DAPR_HOST+":"+DAPR_HTTP_PORT+"/v1.0/state/"+DAPR_STATE_STORE, "application/json", responseBody)
log.Println("Saving Order: " + order)
res, err := client.Post(daprHost+":"+daprHttpPort+"/v1.0/state/"+stateStoreComponentName, "application/json", bytes.NewReader(state))
if err != nil {
panic(err)
}
res.Body.Close()
fmt.Println("Saved Order:", order)
// Get state from a state store
getResponse, err := http.Get(DAPR_HOST + ":" + DAPR_HTTP_PORT + "/v1.0/state/" + DAPR_STATE_STORE + "/" + strconv.Itoa(orderId))
getResponse, err := client.Get(daprHost + ":" + daprHttpPort + "/v1.0/state/" + stateStoreComponentName + "/" + strconv.Itoa(orderId))
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
panic(err)
}
result, _ := ioutil.ReadAll(getResponse.Body)
fmt.Println("Getting Order: ", string(result))
result, err := ioutil.ReadAll(getResponse.Body)
if err != nil {
panic(err)
}
fmt.Println("Retrieved Order:", string(result))
getResponse.Body.Close()
// Delete state from the state store
req, _ := http.NewRequest(http.MethodDelete, DAPR_HOST+":"+DAPR_HTTP_PORT+"/v1.0/state/"+DAPR_STATE_STORE+"/"+strconv.Itoa(orderId), nil)
client := &http.Client{}
_, _ = client.Do(req)
log.Println("Deleting Order: " + order)
req, err := http.NewRequest(http.MethodDelete, daprHost+":"+daprHttpPort+"/v1.0/state/"+stateStoreComponentName+"/"+strconv.Itoa(orderId), nil)
if err != nil {
panic(err)
}
res, err = client.Do(req)
if err != nil {
panic(err)
}
res.Body.Close()
log.Println("Deleted Order:", order)
time.Sleep(5000)
}

View File

@ -1,3 +1,3 @@
module dapr_example
module order_processor_example
go 1.18
go 1.19

View File

@ -1,50 +1,35 @@
# Dapr state management
In this quickstart, you'll create a microservice to demonstrate Dapr's state management API. The service generates messages to store data in a state store. See [Why state management](#why-state-management) to understand when this pattern might be a good choice for your software architecture.
In this quickstart, you'll create a microservice to demonstrate Dapr's state management API. The service generates messages to store data in a state store.
Visit [this](https://docs.dapr.io/developing-applications/building-blocks/state-management/) link for more information about Dapr and State Management.
Visit the Dapr documentation on [State Management](https://docs.dapr.io/developing-applications/building-blocks/state-management/) for more information.
> **Note:** This example leverages the Dapr client SDK. If you are looking for the example using only HTTP `requests` [click here](../http).
> **Note:** This example leverages the Dapr Client SDK. You can find an example using plain HTTP in the [`http` folder](../http/).
This quickstart includes one service:
- Go client service `order-processor`
This quickstart includes one service: Go client service `order-processor`
### Run Go service with Dapr
1. Open a new terminal window and navigate to `order-processor` directory:
<!-- STEP
name: Build Go file
-->
```bash
cd ./order-processor
go build .
```
<!-- END_STEP -->
2. Run the Go service app with Dapr:
1. Run the Go service app with Dapr in the `order-processor` folder:
<!-- STEP
name: Run order-processor service
expected_stdout_lines:
- '== APP == Getting Order: {"orderId":1}'
- '== APP == Getting Order: {"orderId":2}'
- '== APP == Retrieved Order: {"orderId":1}'
- '== APP == Retrieved Order: {"orderId":2}'
- "Exited App successfully"
expected_stderr_lines:
output_match_mode: substring
background: true
sleep: 15
-->
```bash
cd ./order-processor
dapr run --app-id order-processor --components-path ../../../components -- go run .
dapr run \
--app-id order-processor \
--components-path ../../../components \
-- go run .
```
<!-- END_STEP -->
```bash
dapr stop --app-id order-processor
```

View File

@ -1,8 +0,0 @@
apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: config
spec:
features:
- name: Resiliency
enabled: true

View File

@ -10,28 +10,38 @@ import (
dapr "github.com/dapr/go-sdk/client"
)
const stateStoreComponentName = "statestore"
func main() {
client, err := dapr.NewClient()
if err != nil {
log.Fatal(err)
}
for i := 1; i <= 100; i++ {
orderId := i
order := "{\"orderId\":" + strconv.Itoa(orderId) + "}"
client, err := dapr.NewClient()
STATE_STORE_NAME := "statestore"
if err != nil {
panic(err)
}
ctx := context.Background()
order := `{"orderId":` + strconv.Itoa(orderId) + "}"
// Save state into the state store
_ = client.SaveState(ctx, STATE_STORE_NAME, strconv.Itoa(orderId), []byte(order), nil)
log.Print("Saving Order: " + string(order))
err = client.SaveState(context.Background(), stateStoreComponentName, strconv.Itoa(orderId), []byte(order), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Saved Order:", string(order))
// Get state from the state store
result, _ := client.GetState(ctx, STATE_STORE_NAME, strconv.Itoa(orderId), nil)
fmt.Println("Getting Order: " + string(result.Value))
result, err := client.GetState(context.Background(), stateStoreComponentName, strconv.Itoa(orderId), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Retrieved Order:", string(result.Value))
// Delete state from the state store
_ = client.DeleteState(ctx, STATE_STORE_NAME, strconv.Itoa(orderId), nil)
log.Print("Deleting Order: " + string(order))
err = client.DeleteState(context.Background(), stateStoreComponentName, strconv.Itoa(orderId), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Deleted Order:", string(order))
time.Sleep(5000)
}

View File

@ -1,11 +1,10 @@
module dapr_example
module order_processor_sdk_example
go 1.18
go 1.19
require github.com/dapr/go-sdk v1.5.0
require github.com/dapr/go-sdk v1.6.0
require (
github.com/dapr/dapr v1.8.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/net v0.0.0-20220621193019-9d032be2e588 // indirect

View File

@ -11,10 +11,8 @@ github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XP
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/dapr/dapr v1.8.0 h1:ZAAoBe6wuFp7k4tIHB7ajZXVTtGeDeVqIPrldzo3dF0=
github.com/dapr/dapr v1.8.0/go.mod h1:yAsDiK5oecG0htw2S8JG9RFaeHJVdlTfZyOrL57AvRM=
github.com/dapr/go-sdk v1.5.0 h1:OVkrupquJEOL1qRtwKcMVrFKYhw4UJQvgOJNduo2VxE=
github.com/dapr/go-sdk v1.5.0/go.mod h1:Cvz3taCVu22WCNEUbc9/szvG/yJxWPAV4dcaG+zDWA4=
github.com/dapr/go-sdk v1.6.0 h1:jg5A2khSCHF8bGZsig5RWN/gD0jjitszc2V6Uq2pPdY=
github.com/dapr/go-sdk v1.6.0/go.mod h1:KLQBltoD9K0w5hKTihdcyg9Epob9gypwL5dYcQzPro4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@ -51,6 +49,8 @@ github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@ -140,8 +140,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=