diff --git a/bindings/go/http/batch/app b/bindings/go/http/batch/app deleted file mode 100755 index c37c8bea..00000000 Binary files a/bindings/go/http/batch/app and /dev/null differ diff --git a/bindings/go/http/batch/dapr_output_binding b/bindings/go/http/batch/dapr_output_binding deleted file mode 100755 index cb23c2a4..00000000 Binary files a/bindings/go/http/batch/dapr_output_binding and /dev/null differ diff --git a/bindings/go/sdk/batch/app b/bindings/go/sdk/batch/app deleted file mode 100755 index 85e4a80c..00000000 Binary files a/bindings/go/sdk/batch/app and /dev/null differ diff --git a/bindings/go/sdk/batch/batch b/bindings/go/sdk/batch/batch deleted file mode 100755 index 565b0c1d..00000000 Binary files a/bindings/go/sdk/batch/batch and /dev/null differ diff --git a/bindings/go/sdk/batch/dapr_output_binding b/bindings/go/sdk/batch/dapr_output_binding deleted file mode 100755 index 8f6390f6..00000000 Binary files a/bindings/go/sdk/batch/dapr_output_binding and /dev/null differ diff --git a/pub_sub/go/http/README.md b/pub_sub/go/http/README.md index 7c11d4ae..5b6ee99d 100644 --- a/pub_sub/go/http/README.md +++ b/pub_sub/go/http/README.md @@ -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: - - - -```bash -cd ./order-processor -go build . -``` - - -2. Run the Go subscriber app with Dapr: +1. Run the Go subscriber app with Dapr in the `order-processor` folder: - ```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 . ``` ### Run Go message publisher with Dapr -1. Navigate to the directory and install dependencies: - - - -```bash -cd ./checkout -go build . -``` - -2. Run the Go publisher app with Dapr: +1. Run the Go publisher app with Dapr in the `checkout` folder: - + ```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 . ``` +To stop: + ```bash dapr stop --app-id checkout-http dapr stop --app-id order-processor diff --git a/pub_sub/go/http/checkout/app.go b/pub_sub/go/http/checkout/app.go index d710191e..6fd21652 100644 --- a/pub_sub/go/http/checkout/app.go +++ b/pub_sub/go/http/checkout/app.go @@ -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) } diff --git a/pub_sub/go/http/checkout/dapr_example b/pub_sub/go/http/checkout/dapr_example deleted file mode 100755 index f7eb3336..00000000 Binary files a/pub_sub/go/http/checkout/dapr_example and /dev/null differ diff --git a/pub_sub/go/http/checkout/go.mod b/pub_sub/go/http/checkout/go.mod index a81a53c0..ccc502b3 100644 --- a/pub_sub/go/http/checkout/go.mod +++ b/pub_sub/go/http/checkout/go.mod @@ -1,3 +1,3 @@ -module dapr_example +module checkout_example -go 1.18 +go 1.19 diff --git a/pub_sub/go/http/order-processor/app.go b/pub_sub/go/http/order-processor/app.go index 5e2415f8..519dadb4 100644 --- a/pub_sub/go/http/order-processor/app.go +++ b/pub_sub/go/http/order-processor/app.go @@ -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) } } diff --git a/pub_sub/go/http/order-processor/dapr_example b/pub_sub/go/http/order-processor/dapr_example deleted file mode 100755 index 10b7f109..00000000 Binary files a/pub_sub/go/http/order-processor/dapr_example and /dev/null differ diff --git a/pub_sub/go/http/order-processor/go.mod b/pub_sub/go/http/order-processor/go.mod index 66749be4..44043066 100644 --- a/pub_sub/go/http/order-processor/go.mod +++ b/pub_sub/go/http/order-processor/go.mod @@ -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 diff --git a/pub_sub/go/sdk/README.md b/pub_sub/go/sdk/README.md index 6f2f3614..2a5aaf61 100644 --- a/pub_sub/go/sdk/README.md +++ b/pub_sub/go/sdk/README.md @@ -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: - - - -```bash -cd ./order-processor -go build . -``` - - -2. Run the Go subscriber app with Dapr: +1. Run the Go subscriber app with Dapr in the `order-processor` folder: ### Run Go message publisher with Dapr -1. Navigate to the directory and install dependencies: - - - -```bash -cd ./checkout -go build . -``` - -2. Run the Go publisher app with Dapr: +1 Run the Go publisher app with Dapr in the `checkout` folder: - + ```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 . ``` +To stop: + ```bash dapr stop --app-id checkout-sdk dapr stop --app-id order-processor-sdk diff --git a/pub_sub/go/sdk/checkout/app.go b/pub_sub/go/sdk/checkout/app.go index 0fdd8197..fa132bc4 100644 --- a/pub_sub/go/sdk/checkout/app.go +++ b/pub_sub/go/sdk/checkout/app.go @@ -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) } diff --git a/pub_sub/go/sdk/checkout/dapr_example b/pub_sub/go/sdk/checkout/dapr_example deleted file mode 100755 index fc8bbe52..00000000 Binary files a/pub_sub/go/sdk/checkout/dapr_example and /dev/null differ diff --git a/pub_sub/go/sdk/checkout/go.mod b/pub_sub/go/sdk/checkout/go.mod index 951a92e8..9ad8b584 100644 --- a/pub_sub/go/sdk/checkout/go.mod +++ b/pub_sub/go/sdk/checkout/go.mod @@ -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 diff --git a/pub_sub/go/sdk/checkout/go.sum b/pub_sub/go/sdk/checkout/go.sum index 7e7b1550..79771c5a 100644 --- a/pub_sub/go/sdk/checkout/go.sum +++ b/pub_sub/go/sdk/checkout/go.sum @@ -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= diff --git a/pub_sub/go/sdk/order-processor/app.go b/pub_sub/go/sdk/order-processor/app.go index 383008b0..892160c7 100644 --- a/pub_sub/go/sdk/order-processor/app.go +++ b/pub_sub/go/sdk/order-processor/app.go @@ -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 } diff --git a/pub_sub/go/sdk/order-processor/dapr_example b/pub_sub/go/sdk/order-processor/dapr_example deleted file mode 100755 index edb184b0..00000000 Binary files a/pub_sub/go/sdk/order-processor/dapr_example and /dev/null differ diff --git a/pub_sub/go/sdk/order-processor/go.mod b/pub_sub/go/sdk/order-processor/go.mod index f6c67a74..18889dde 100644 --- a/pub_sub/go/sdk/order-processor/go.mod +++ b/pub_sub/go/sdk/order-processor/go.mod @@ -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 diff --git a/pub_sub/go/sdk/order-processor/go.sum b/pub_sub/go/sdk/order-processor/go.sum index 5903a693..dda5b660 100644 --- a/pub_sub/go/sdk/order-processor/go.sum +++ b/pub_sub/go/sdk/order-processor/go.sum @@ -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= diff --git a/resiliency/README.md b/resiliency/README.md index 331dfee5..2c7e0d73 100644 --- a/resiliency/README.md +++ b/resiliency/README.md @@ -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) diff --git a/resiliency/service-to-component-resiliency.md b/resiliency/service-to-component-resiliency.md index d7b67828..7cac4531 100644 --- a/resiliency/service-to-component-resiliency.md +++ b/resiliency/service-to-component-resiliency.md @@ -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 ### 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 ``` diff --git a/resiliency/service-to-service-resiliency.md b/resiliency/service-to-service-resiliency.md index 96b6bfde..f37cf1db 100644 --- a/resiliency/service-to-service-resiliency.md +++ b/resiliency/service-to-service-resiliency.md @@ -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 ``` - diff --git a/service_invocation/go/http/README.md b/service_invocation/go/http/README.md index fdd02f80..2b33a5f0 100644 --- a/service_invocation/go/http/README.md +++ b/service_invocation/go/http/README.md @@ -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: - - - -```bash -cd ./order-processor -go build . -``` - - - -2. Run the Go order-processor app with Dapr: +1. Run the order-processor app with Dapr in the `order-processor` folder: ### Run Go checkout with Dapr -1. Open a new terminal window and navigate to `checkout` directory: - - - -```bash -cd ./checkout -go build . -``` - - -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: - + ```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 . ``` +To stop: + ```bash dapr stop --app-id checkout dapr stop --app-id order-processor diff --git a/service_invocation/go/http/checkout/app.go b/service_invocation/go/http/checkout/app.go index 80a8600e..6e8293a7 100644 --- a/service_invocation/go/http/checkout/app.go +++ b/service_invocation/go/http/checkout/app.go @@ -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)) } } diff --git a/service_invocation/go/http/checkout/dapr_example b/service_invocation/go/http/checkout/dapr_example deleted file mode 100755 index da92bc0a..00000000 Binary files a/service_invocation/go/http/checkout/dapr_example and /dev/null differ diff --git a/service_invocation/go/http/checkout/go.mod b/service_invocation/go/http/checkout/go.mod index a81a53c0..ccc502b3 100644 --- a/service_invocation/go/http/checkout/go.mod +++ b/service_invocation/go/http/checkout/go.mod @@ -1,3 +1,3 @@ -module dapr_example +module checkout_example -go 1.18 +go 1.19 diff --git a/service_invocation/go/http/config.yaml b/service_invocation/go/http/config.yaml deleted file mode 100644 index ae68e9c7..00000000 --- a/service_invocation/go/http/config.yaml +++ /dev/null @@ -1,8 +0,0 @@ -apiVersion: dapr.io/v1alpha1 -kind: Configuration -metadata: - name: config -spec: - features: - - name: Resiliency - enabled: true \ No newline at end of file diff --git a/service_invocation/go/http/order-processor/app.go b/service_invocation/go/http/order-processor/app.go index d1ca75f0..07398520 100644 --- a/service_invocation/go/http/order-processor/app.go +++ b/service_invocation/go/http/order-processor/app.go @@ -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") + } } diff --git a/service_invocation/go/http/order-processor/dapr_example b/service_invocation/go/http/order-processor/dapr_example deleted file mode 100755 index 917499d9..00000000 Binary files a/service_invocation/go/http/order-processor/dapr_example and /dev/null differ diff --git a/service_invocation/go/http/order-processor/go.mod b/service_invocation/go/http/order-processor/go.mod index 66749be4..44043066 100644 --- a/service_invocation/go/http/order-processor/go.mod +++ b/service_invocation/go/http/order-processor/go.mod @@ -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 diff --git a/state_management/go/http/README.md b/state_management/go/http/README.md index abd77fb5..15f39459 100644 --- a/state_management/go/http/README.md +++ b/state_management/go/http/README.md @@ -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: - - - -```bash -cd ./order-processor -go build . -``` - - -2. Run the Go service app with Dapr: +1. Run the Go service app with Dapr in the `order-processor` folder: - + ```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 . ``` - -```bash -dapr stop --app-id order-processor -``` diff --git a/state_management/go/http/order-processor/app b/state_management/go/http/order-processor/app deleted file mode 100755 index f6433db2..00000000 Binary files a/state_management/go/http/order-processor/app and /dev/null differ diff --git a/state_management/go/http/order-processor/app.go b/state_management/go/http/order-processor/app.go index 5e454621..6d5d11a3 100644 --- a/state_management/go/http/order-processor/app.go +++ b/state_management/go/http/order-processor/app.go @@ -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) } diff --git a/state_management/go/http/order-processor/dapr_example b/state_management/go/http/order-processor/dapr_example deleted file mode 100755 index d5f674ff..00000000 Binary files a/state_management/go/http/order-processor/dapr_example and /dev/null differ diff --git a/state_management/go/http/order-processor/go.mod b/state_management/go/http/order-processor/go.mod index a81a53c0..1d136e3b 100644 --- a/state_management/go/http/order-processor/go.mod +++ b/state_management/go/http/order-processor/go.mod @@ -1,3 +1,3 @@ -module dapr_example +module order_processor_example -go 1.18 +go 1.19 diff --git a/state_management/go/sdk/README.md b/state_management/go/sdk/README.md index 89368fa5..bd0b8fc3 100644 --- a/state_management/go/sdk/README.md +++ b/state_management/go/sdk/README.md @@ -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: - - - -```bash -cd ./order-processor -go build . -``` - - -2. Run the Go service app with Dapr: +1. Run the Go service app with Dapr in the `order-processor` folder: - + ```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 . ``` - -```bash -dapr stop --app-id order-processor -``` diff --git a/state_management/go/sdk/config.yaml b/state_management/go/sdk/config.yaml deleted file mode 100644 index ae68e9c7..00000000 --- a/state_management/go/sdk/config.yaml +++ /dev/null @@ -1,8 +0,0 @@ -apiVersion: dapr.io/v1alpha1 -kind: Configuration -metadata: - name: config -spec: - features: - - name: Resiliency - enabled: true \ No newline at end of file diff --git a/state_management/go/sdk/order-processor/app b/state_management/go/sdk/order-processor/app deleted file mode 100755 index 4ed75541..00000000 Binary files a/state_management/go/sdk/order-processor/app and /dev/null differ diff --git a/state_management/go/sdk/order-processor/app.go b/state_management/go/sdk/order-processor/app.go index 9573ca03..7f6eba84 100644 --- a/state_management/go/sdk/order-processor/app.go +++ b/state_management/go/sdk/order-processor/app.go @@ -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) } diff --git a/state_management/go/sdk/order-processor/dapr_example b/state_management/go/sdk/order-processor/dapr_example deleted file mode 100755 index a757fb5e..00000000 Binary files a/state_management/go/sdk/order-processor/dapr_example and /dev/null differ diff --git a/state_management/go/sdk/order-processor/go.mod b/state_management/go/sdk/order-processor/go.mod index 951a92e8..de87590d 100644 --- a/state_management/go/sdk/order-processor/go.mod +++ b/state_management/go/sdk/order-processor/go.mod @@ -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 diff --git a/state_management/go/sdk/order-processor/go.sum b/state_management/go/sdk/order-processor/go.sum index 7e7b1550..79771c5a 100644 --- a/state_management/go/sdk/order-processor/go.sum +++ b/state_management/go/sdk/order-processor/go.sum @@ -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=