Quickstart: State Management

Get started with Dapr’s State Management building block

Let’s take a look at Dapr’s State Management building block. In this Quickstart, you will save, get, and delete state using a Redis state store by either:

State Management - 图1

While this sample uses Redis, you can swap it out for any one of the supported state stores.

Run using Multi-App Run

Select your preferred language-specific Dapr SDK before proceeding with the Quickstart.

Pre-requisites

For this example, you will need:

Step 1: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Step 2: Manipulate service state

In a terminal window, navigate to the order-processor directory.

  1. cd state_management/python/sdk/order-processor

Install the dependencies:

  1. pip3 install -r requirements.txt

Run the order-processor service alongside a Dapr sidecar using Multi-App Run.

  1. dapr run -f .

Note: Since Python3.exe is not defined in Windows, you may need to change python3 to python in the dapr.yaml file before running dapr run -f .

The order-processor service writes, reads, and deletes an orderId key/value pair to the statestore instance defined in the statestore.yaml component. As soon as the service starts, it performs a loop.

  1. with DaprClient() as client:
  2. # Save state into the state store
  3. client.save_state(DAPR_STORE_NAME, orderId, str(order))
  4. logging.info('Saving Order: %s', order)
  5. # Get state from the state store
  6. result = client.get_state(DAPR_STORE_NAME, orderId)
  7. logging.info('Result after get: ' + str(result.data))
  8. # Delete state from the state store
  9. client.delete_state(store_name=DAPR_STORE_NAME, key=orderId)
  10. logging.info('Deleting Order: %s', order)

Step 3: View the order-processor outputs

Notice, as specified in the code above, the code saves application state in the Dapr state store, reads it, then deletes it.

Order-processor output:

  1. == APP == INFO:root:Saving Order: {'orderId': '1'}
  2. == APP == INFO:root:Result after get: b"{'orderId': '1'}"
  3. == APP == INFO:root:Deleting Order: {'orderId': '1'}
  4. == APP == INFO:root:Saving Order: {'orderId': '2'}
  5. == APP == INFO:root:Result after get: b"{'orderId': '2'}"
  6. == APP == INFO:root:Deleting Order: {'orderId': '2'}
  7. == APP == INFO:root:Saving Order: {'orderId': '3'}
  8. == APP == INFO:root:Result after get: b"{'orderId': '3'}"
  9. == APP == INFO:root:Deleting Order: {'orderId': '3'}
  10. == APP == INFO:root:Saving Order: {'orderId': '4'}
  11. == APP == INFO:root:Result after get: b"{'orderId': '4'}"
  12. == APP == INFO:root:Deleting Order: {'orderId': '4'}
dapr.yaml Multi-App Run template file

When you run dapr init, Dapr creates a default Multi-App Run template file named dapr.yaml. Running dapr run -f starts all applications in your project. In this sample, the dapr.yaml file contains the following:

  1. version: 1
  2. common:
  3. resourcesPath: ../../resources/
  4. apps:
  5. - appID: order-processor
  6. appDirPath: ./order-processor/
  7. command: ["python3" , "app.py"]
statestore.yaml component file

When you run dapr init, Dapr also creates a default Redis statestore.yaml and runs a Redis container on your local machine, located:

  • On Windows, under %UserProfile%\.dapr\components\statestore.yaml
  • On Linux/MacOS, under ~/.dapr/components/statestore.yaml

With the statestore.yaml component, you can easily swap out the state store without making code changes.

The Redis statestore.yaml file included for this quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: statestore
  5. spec:
  6. type: state.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""
  13. - name: actorStateStore
  14. value: "true"

In the YAML file:

  • metadata/name is how your application talks to the component (called DAPR_STORE_NAME in the code sample).
  • spec/metadata defines the connection to the Redis instance used by the component.

Pre-requisites

For this example, you will need:

Step 1: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Install the dependencies for the order-processor app:

  1. cd ./order-processor
  2. npm install
  3. cd ..

Step 2: Manipulate service state

In a terminal window, navigate to the order-processor directory.

  1. cd state_management/javascript/sdk/order-processor

Install the dependencies:

  1. npm install

Run the order-processor service alongside a Dapr sidecar.

  1. dapr run -f .

The order-processor service writes, reads, and deletes an orderId key/value pair to the statestore instance defined in the statestore.yaml component. As soon as the service starts, it performs a loop.

  1. const client = new DaprClient()
  2. // Save state into a state store
  3. await client.state.save(DAPR_STATE_STORE_NAME, order)
  4. console.log("Saving Order: ", order)
  5. // Get state from a state store
  6. const savedOrder = await client.state.get(DAPR_STATE_STORE_NAME, order.orderId)
  7. console.log("Getting Order: ", savedOrder)
  8. // Delete state from the state store
  9. await client.state.delete(DAPR_STATE_STORE_NAME, order.orderId)
  10. console.log("Deleting Order: ", order)

Step 3: View the order-processor outputs

Notice, as specified in the code above, the code saves application state in the Dapr state store, reads it, then deletes it.

Order-processor output:

  1. == APP == > order-processor@1.0.0 start
  2. == APP == > node index.js
  3. == APP == Saving Order: { orderId: 1 }
  4. == APP == Saving Order: { orderId: 2 }
  5. == APP == Saving Order: { orderId: 3 }
  6. == APP == Saving Order: { orderId: 4 }
  7. == APP == Saving Order: { orderId: 5 }
  8. == APP == Getting Order: { orderId: 1 }
  9. == APP == Deleting Order: { orderId: 1 }
  10. == APP == Getting Order: { orderId: 2 }
  11. == APP == Deleting Order: { orderId: 2 }
  12. == APP == Getting Order: { orderId: 3 }
  13. == APP == Deleting Order: { orderId: 3 }
  14. == APP == Getting Order: { orderId: 4 }
  15. == APP == Deleting Order: { orderId: 4 }
  16. == APP == Getting Order: { orderId: 5 }
  17. == APP == Deleting Order: { orderId: 5 }
dapr.yaml Multi-App Run template file

When you run dapr init, Dapr creates a default Multi-App Run template file named dapr.yaml. Running dapr run -f starts all applications in your project. In this sample, the dapr.yaml file contains the following:

  1. version: 1
  2. common:
  3. resourcesPath: ../../resources/
  4. apps:
  5. - appID: order-processor
  6. appDirPath: ./order-processor/
  7. command: ["npm", "run", "start"]
statestore.yaml component file

When you run dapr init, Dapr creates a default Redis statestore.yaml and runs a Redis container on your local machine, located:

  • On Windows, under %UserProfile%\.dapr\components\statestore.yaml
  • On Linux/MacOS, under ~/.dapr/components/statestore.yaml

With the statestore.yaml component, you can easily swap out the state store without making code changes.

The Redis statestore.yaml file included for this quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: statestore
  5. spec:
  6. type: state.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""
  13. - name: actorStateStore
  14. value: "true"

In the YAML file:

  • metadata/name is how your application talks to the component (called DAPR_STORE_NAME in the code sample).
  • spec/metadata defines the connection to the Redis instance used by the component.

Pre-requisites

For this example, you will need:

Step 1: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Step 2: Manipulate service state

In a terminal window, navigate to the order-processor directory.

  1. cd state_management/csharp/sdk/order-processor

Install the dependencies:

  1. dotnet restore
  2. dotnet build

Run the order-processor service alongside a Dapr sidecar.

  1. dapr run -f .

The order-processor service writes, reads, and deletes an orderId key/value pair to the statestore instance defined in the statestore.yaml component. As soon as the service starts, it performs a loop.

  1. var client = new DaprClientBuilder().Build();
  2. // Save state into the state store
  3. await client.SaveStateAsync(DAPR_STORE_NAME, orderId.ToString(), order.ToString());
  4. Console.WriteLine("Saving Order: " + order);
  5. // Get state from the state store
  6. var result = await client.GetStateAsync<string>(DAPR_STORE_NAME, orderId.ToString());
  7. Console.WriteLine("Getting Order: " + result);
  8. // Delete state from the state store
  9. await client.DeleteStateAsync(DAPR_STORE_NAME, orderId.ToString());
  10. Console.WriteLine("Deleting Order: " + order);

Step 3: View the order-processor outputs

Notice, as specified in the code above, the code saves application state in the Dapr state store, reads it, then deletes it.

Order-processor output:

  1. == APP == Saving Order: Order { orderId = 1 }
  2. == APP == Getting Order: Order { orderId = 1 }
  3. == APP == Deleting Order: Order { orderId = 1 }
  4. == APP == Saving Order: Order { orderId = 2 }
  5. == APP == Getting Order: Order { orderId = 2 }
  6. == APP == Deleting Order: Order { orderId = 2 }
  7. == APP == Saving Order: Order { orderId = 3 }
  8. == APP == Getting Order: Order { orderId = 3 }
  9. == APP == Deleting Order: Order { orderId = 3 }
  10. == APP == Saving Order: Order { orderId = 4 }
  11. == APP == Getting Order: Order { orderId = 4 }
  12. == APP == Deleting Order: Order { orderId = 4 }
  13. == APP == Saving Order: Order { orderId = 5 }
  14. == APP == Getting Order: Order { orderId = 5 }
  15. == APP == Deleting Order: Order { orderId = 5 }
dapr.yaml Multi-App Run template file

When you run dapr init, Dapr creates a default Multi-App Run template file named dapr.yaml. Running dapr run -f starts all applications in your project. In this sample, the dapr.yaml file contains the following:

  1. version: 1
  2. common:
  3. resourcesPath: ../../../resources/
  4. apps:
  5. - appID: order-processor
  6. appDirPath: ./order-processor/
  7. command: ["dotnet", "run"]
statestore.yaml component file

When you run dapr init, Dapr creates a default Redis statestore.yaml and runs a Redis container on your local machine, located:

  • On Windows, under %UserProfile%\.dapr\components\statestore.yaml
  • On Linux/MacOS, under ~/.dapr/components/statestore.yaml

With the statestore.yaml component, you can easily swap out the state store without making code changes.

The Redis statestore.yaml file included for this quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: statestore
  5. spec:
  6. type: state.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""
  13. - name: actorStateStore
  14. value: "true"

In the YAML file:

  • metadata/name is how your application talks to the component (called DAPR_STORE_NAME in the code sample).
  • spec/metadata defines the connection to the Redis instance used by the component.

Pre-requisites

For this example, you will need:

Step 1: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Step 2: Manipulate service state

In a terminal window, navigate to the order-processor directory.

  1. cd state_management/java/sdk/order-processor

Install the dependencies:

  1. mvn clean install

Run the order-processor service alongside a Dapr sidecar.

  1. dapr run -f .

The order-processor service writes, reads, and deletes an orderId key/value pair to the statestore instance defined in the statestore.yaml component. As soon as the service starts, it performs a loop.

  1. try (DaprClient client = new DaprClientBuilder().build()) {
  2. for (int i = 1; i <= 10; i++) {
  3. int orderId = i;
  4. Order order = new Order();
  5. order.setOrderId(orderId);
  6. // Save state into the state store
  7. client.saveState(DAPR_STATE_STORE, String.valueOf(orderId), order).block();
  8. LOGGER.info("Saving Order: " + order.getOrderId());
  9. // Get state from the state store
  10. State<Order> response = client.getState(DAPR_STATE_STORE, String.valueOf(orderId), Order.class).block();
  11. LOGGER.info("Getting Order: " + response.getValue().getOrderId());
  12. // Delete state from the state store
  13. client.deleteState(DAPR_STATE_STORE, String.valueOf(orderId)).block();
  14. LOGGER.info("Deleting Order: " + orderId);
  15. TimeUnit.MILLISECONDS.sleep(1000);
  16. }

Step 3: View the order-processor outputs

Notice, as specified in the code above, the code saves application state in the Dapr state store, reads it, then deletes it.

Order-processor output:

  1. == APP == INFO:root:Saving Order: {'orderId': '1'}
  2. == APP == INFO:root:Result after get: b"{'orderId': '1'}"
  3. == APP == INFO:root:Deleting Order: {'orderId': '1'}
  4. == APP == INFO:root:Saving Order: {'orderId': '2'}
  5. == APP == INFO:root:Result after get: b"{'orderId': '2'}"
  6. == APP == INFO:root:Deleting Order: {'orderId': '2'}
  7. == APP == INFO:root:Saving Order: {'orderId': '3'}
  8. == APP == INFO:root:Result after get: b"{'orderId': '3'}"
  9. == APP == INFO:root:Deleting Order: {'orderId': '3'}
  10. == APP == INFO:root:Saving Order: {'orderId': '4'}
  11. == APP == INFO:root:Result after get: b"{'orderId': '4'}"
  12. == APP == INFO:root:Deleting Order: {'orderId': '4'}
dapr.yaml Multi-App Run template file

When you run dapr init, Dapr creates a default Multi-App Run template file named dapr.yaml. Running dapr run -f starts all applications in your project. In this sample, the dapr.yaml file contains the following:

  1. version: 1
  2. common:
  3. resourcesPath: ../../resources/
  4. apps:
  5. - appID: order-processor
  6. appDirPath: ./order-processor/
  7. command: ["java", "-jar", "target/OrderProcessingService-0.0.1-SNAPSHOT.jar"]
statestore.yaml component file

When you run dapr init, Dapr creates a default Redis statestore.yaml and runs a Redis container on your local machine, located:

  • On Windows, under %UserProfile%\.dapr\components\statestore.yaml
  • On Linux/MacOS, under ~/.dapr/components/statestore.yaml

With the statestore.yaml component, you can easily swap out the state store without making code changes.

The Redis statestore.yaml file included for this Quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: statestore
  5. spec:
  6. type: state.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""
  13. - name: actorStateStore
  14. value: "true"

In the YAML file:

  • metadata/name is how your application talks to the component (called DAPR_STORE_NAME in the code sample).
  • spec/metadata defines the connection to the Redis instance used by the component.

Pre-requisites

For this example, you will need:

Step 1: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Step 2: Manipulate service state

In a terminal window, navigate to the order-processor directory.

  1. cd state_management/go/sdk/order-processor

Install the dependencies:

  1. go build .

Run the order-processor service alongside a Dapr sidecar.

  1. dapr run -f .

The order-processor service writes, reads, and deletes an orderId key/value pair to the statestore instance defined in the statestore.yaml component. As soon as the service starts, it performs a loop.

  1. client, err := dapr.NewClient()
  2. // Save state into the state store
  3. _ = client.SaveState(ctx, STATE_STORE_NAME, strconv.Itoa(orderId), []byte(order))
  4. log.Print("Saving Order: " + string(order))
  5. // Get state from the state store
  6. result, _ := client.GetState(ctx, STATE_STORE_NAME, strconv.Itoa(orderId))
  7. fmt.Println("Getting Order: " + string(result.Value))
  8. // Delete state from the state store
  9. _ = client.DeleteState(ctx, STATE_STORE_NAME, strconv.Itoa(orderId))
  10. log.Print("Deleting Order: " + string(order))

Step 3: View the order-processor outputs

Notice, as specified in the code above, the code saves application state in the Dapr state store, reads it, then deletes it.

Order-processor output:

  1. == APP == dapr client initializing for: 127.0.0.1:53689
  2. == APP == 2022/04/01 09:16:03 Saving Order: {"orderId":1}
  3. == APP == Getting Order: {"orderId":1}
  4. == APP == 2022/04/01 09:16:03 Deleting Order: {"orderId":1}
  5. == APP == 2022/04/01 09:16:03 Saving Order: {"orderId":2}
  6. == APP == Getting Order: {"orderId":2}
  7. == APP == 2022/04/01 09:16:03 Deleting Order: {"orderId":2}
  8. == APP == 2022/04/01 09:16:03 Saving Order: {"orderId":3}
  9. == APP == Getting Order: {"orderId":3}
  10. == APP == 2022/04/01 09:16:03 Deleting Order: {"orderId":3}
  11. == APP == 2022/04/01 09:16:03 Saving Order: {"orderId":4}
  12. == APP == Getting Order: {"orderId":4}
  13. == APP == 2022/04/01 09:16:03 Deleting Order: {"orderId":4}
  14. == APP == 2022/04/01 09:16:03 Saving Order: {"orderId":5}
  15. == APP == Getting Order: {"orderId":5}
  16. == APP == 2022/04/01 09:16:03 Deleting Order: {"orderId":5}
dapr.yaml Multi-App Run template file

When you run dapr init, Dapr creates a default Multi-App Run template file named dapr.yaml. Running dapr run -f starts all applications in your project. In this sample, the dapr.yaml file contains the following:

  1. version: 1
  2. common:
  3. resourcesPath: ../../resources/
  4. apps:
  5. - appID: order-processor
  6. appDirPath: ./order-processor/
  7. command: ["go", "run", "."]
statestore.yaml component file

When you run dapr init, Dapr creates a default Redis statestore.yaml and runs a Redis container on your local machine, located:

  • On Windows, under %UserProfile%\.dapr\components\statestore.yaml
  • On Linux/MacOS, under ~/.dapr/components/statestore.yaml

With the statestore.yaml component, you can easily swap out the state store without making code changes.

The Redis statestore.yaml file included for this Quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: statestore
  5. spec:
  6. type: state.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""
  13. - name: actorStateStore
  14. value: "true"

In the YAML file:

  • metadata/name is how your application talks to the component (called DAPR_STORE_NAME in the code sample).
  • spec/metadata defines the connection to the Redis instance used by the component.

Run one application at a time

Select your preferred language-specific Dapr SDK before proceeding with the Quickstart.

Pre-requisites

For this example, you will need:

Step 1: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Step 2: Manipulate service state

In a terminal window, navigate to the order-processor directory.

  1. cd state_management/python/sdk/order-processor

Install the dependencies:

  1. pip3 install -r requirements.txt

Run the order-processor service alongside a Dapr sidecar.

  1. dapr run --app-id order-processor --resources-path ../../../resources/ -- python3 app.py

Note: Since Python3.exe is not defined in Windows, you may need to use python app.py instead of python3 app.py.

The order-processor service writes, reads, and deletes an orderId key/value pair to the statestore instance defined in the statestore.yaml component. As soon as the service starts, it performs a loop.

  1. with DaprClient() as client:
  2. # Save state into the state store
  3. client.save_state(DAPR_STORE_NAME, orderId, str(order))
  4. logging.info('Saving Order: %s', order)
  5. # Get state from the state store
  6. result = client.get_state(DAPR_STORE_NAME, orderId)
  7. logging.info('Result after get: ' + str(result.data))
  8. # Delete state from the state store
  9. client.delete_state(store_name=DAPR_STORE_NAME, key=orderId)
  10. logging.info('Deleting Order: %s', order)

Step 3: View the order-processor outputs

Notice, as specified in the code above, the code saves application state in the Dapr state store, reads it, then deletes it.

Order-processor output:

  1. == APP == INFO:root:Saving Order: {'orderId': '1'}
  2. == APP == INFO:root:Result after get: b"{'orderId': '1'}"
  3. == APP == INFO:root:Deleting Order: {'orderId': '1'}
  4. == APP == INFO:root:Saving Order: {'orderId': '2'}
  5. == APP == INFO:root:Result after get: b"{'orderId': '2'}"
  6. == APP == INFO:root:Deleting Order: {'orderId': '2'}
  7. == APP == INFO:root:Saving Order: {'orderId': '3'}
  8. == APP == INFO:root:Result after get: b"{'orderId': '3'}"
  9. == APP == INFO:root:Deleting Order: {'orderId': '3'}
  10. == APP == INFO:root:Saving Order: {'orderId': '4'}
  11. == APP == INFO:root:Result after get: b"{'orderId': '4'}"
  12. == APP == INFO:root:Deleting Order: {'orderId': '4'}
statestore.yaml component file

When you run dapr init, Dapr creates a default Redis statestore.yaml and runs a Redis container on your local machine, located:

  • On Windows, under %UserProfile%\.dapr\components\statestore.yaml
  • On Linux/MacOS, under ~/.dapr/components/statestore.yaml

With the statestore.yaml component, you can easily swap out the state store without making code changes.

The Redis statestore.yaml file included for this quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: statestore
  5. spec:
  6. type: state.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""
  13. - name: actorStateStore
  14. value: "true"

In the YAML file:

  • metadata/name is how your application talks to the component (called DAPR_STORE_NAME in the code sample).
  • spec/metadata defines the connection to the Redis instance used by the component.

Pre-requisites

For this example, you will need:

Step 1: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Step 2: Manipulate service state

In a terminal window, navigate to the order-processor directory.

  1. cd state_management/javascript/sdk/order-processor

Install dependencies, which will include the @dapr/dapr package from the JavaScript SDK:

  1. npm install

Verify you have the following files included in the service directory:

  • package.json
  • package-lock.json

Run the order-processor service alongside a Dapr sidecar.

  1. dapr run --app-id order-processor --resources-path ../../../resources/ -- npm run start

The order-processor service writes, reads, and deletes an orderId key/value pair to the statestore instance defined in the statestore.yaml component. As soon as the service starts, it performs a loop.

  1. const client = new DaprClient()
  2. // Save state into a state store
  3. await client.state.save(DAPR_STATE_STORE_NAME, order)
  4. console.log("Saving Order: ", order)
  5. // Get state from a state store
  6. const savedOrder = await client.state.get(DAPR_STATE_STORE_NAME, order.orderId)
  7. console.log("Getting Order: ", savedOrder)
  8. // Delete state from the state store
  9. await client.state.delete(DAPR_STATE_STORE_NAME, order.orderId)
  10. console.log("Deleting Order: ", order)

Step 3: View the order-processor outputs

Notice, as specified in the code above, the code saves application state in the Dapr state store, reads it, then deletes it.

Order-processor output:

  1. == APP == > order-processor@1.0.0 start
  2. == APP == > node index.js
  3. == APP == Saving Order: { orderId: 1 }
  4. == APP == Saving Order: { orderId: 2 }
  5. == APP == Saving Order: { orderId: 3 }
  6. == APP == Saving Order: { orderId: 4 }
  7. == APP == Saving Order: { orderId: 5 }
  8. == APP == Getting Order: { orderId: 1 }
  9. == APP == Deleting Order: { orderId: 1 }
  10. == APP == Getting Order: { orderId: 2 }
  11. == APP == Deleting Order: { orderId: 2 }
  12. == APP == Getting Order: { orderId: 3 }
  13. == APP == Deleting Order: { orderId: 3 }
  14. == APP == Getting Order: { orderId: 4 }
  15. == APP == Deleting Order: { orderId: 4 }
  16. == APP == Getting Order: { orderId: 5 }
  17. == APP == Deleting Order: { orderId: 5 }
statestore.yaml component file

When you run dapr init, Dapr creates a default Redis statestore.yaml and runs a Redis container on your local machine, located:

  • On Windows, under %UserProfile%\.dapr\components\statestore.yaml
  • On Linux/MacOS, under ~/.dapr/components/statestore.yaml

With the statestore.yaml component, you can easily swap out the state store without making code changes.

The Redis statestore.yaml file included for this quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: statestore
  5. spec:
  6. type: state.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""
  13. - name: actorStateStore
  14. value: "true"

In the YAML file:

  • metadata/name is how your application talks to the component (called DAPR_STORE_NAME in the code sample).
  • spec/metadata defines the connection to the Redis instance used by the component.

Pre-requisites

For this example, you will need:

Step 1: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Step 2: Manipulate service state

In a terminal window, navigate to the order-processor directory.

  1. cd state_management/csharp/sdk/order-processor

Recall NuGet packages:

  1. dotnet restore
  2. dotnet build

Run the order-processor service alongside a Dapr sidecar.

  1. dapr run --app-id order-processor --resources-path ../../../resources/ -- dotnet run

The order-processor service writes, reads, and deletes an orderId key/value pair to the statestore instance defined in the statestore.yaml component. As soon as the service starts, it performs a loop.

  1. var client = new DaprClientBuilder().Build();
  2. // Save state into the state store
  3. await client.SaveStateAsync(DAPR_STORE_NAME, orderId.ToString(), order.ToString());
  4. Console.WriteLine("Saving Order: " + order);
  5. // Get state from the state store
  6. var result = await client.GetStateAsync<string>(DAPR_STORE_NAME, orderId.ToString());
  7. Console.WriteLine("Getting Order: " + result);
  8. // Delete state from the state store
  9. await client.DeleteStateAsync(DAPR_STORE_NAME, orderId.ToString());
  10. Console.WriteLine("Deleting Order: " + order);

Step 3: View the order-processor outputs

Notice, as specified in the code above, the code saves application state in the Dapr state store, reads it, then deletes it.

Order-processor output:

  1. == APP == Saving Order: Order { orderId = 1 }
  2. == APP == Getting Order: Order { orderId = 1 }
  3. == APP == Deleting Order: Order { orderId = 1 }
  4. == APP == Saving Order: Order { orderId = 2 }
  5. == APP == Getting Order: Order { orderId = 2 }
  6. == APP == Deleting Order: Order { orderId = 2 }
  7. == APP == Saving Order: Order { orderId = 3 }
  8. == APP == Getting Order: Order { orderId = 3 }
  9. == APP == Deleting Order: Order { orderId = 3 }
  10. == APP == Saving Order: Order { orderId = 4 }
  11. == APP == Getting Order: Order { orderId = 4 }
  12. == APP == Deleting Order: Order { orderId = 4 }
  13. == APP == Saving Order: Order { orderId = 5 }
  14. == APP == Getting Order: Order { orderId = 5 }
  15. == APP == Deleting Order: Order { orderId = 5 }
statestore.yaml component file

When you run dapr init, Dapr creates a default Redis statestore.yaml and runs a Redis container on your local machine, located:

  • On Windows, under %UserProfile%\.dapr\components\statestore.yaml
  • On Linux/MacOS, under ~/.dapr/components/statestore.yaml

With the statestore.yaml component, you can easily swap out the state store without making code changes.

The Redis statestore.yaml file included for this quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: statestore
  5. spec:
  6. type: state.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""
  13. - name: actorStateStore
  14. value: "true"

In the YAML file:

  • metadata/name is how your application talks to the component (called DAPR_STORE_NAME in the code sample).
  • spec/metadata defines the connection to the Redis instance used by the component.

Pre-requisites

For this example, you will need:

Step 1: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Step 2: Manipulate service state

In a terminal window, navigate to the order-processor directory.

  1. cd state_management/java/sdk/order-processor

Install the dependencies:

  1. mvn clean install

Run the order-processor service alongside a Dapr sidecar.

  1. dapr run --app-id order-processor --resources-path ../../../resources -- java -jar target/OrderProcessingService-0.0.1-SNAPSHOT.jar

The order-processor service writes, reads, and deletes an orderId key/value pair to the statestore instance defined in the statestore.yaml component. As soon as the service starts, it performs a loop.

  1. try (DaprClient client = new DaprClientBuilder().build()) {
  2. for (int i = 1; i <= 10; i++) {
  3. int orderId = i;
  4. Order order = new Order();
  5. order.setOrderId(orderId);
  6. // Save state into the state store
  7. client.saveState(DAPR_STATE_STORE, String.valueOf(orderId), order).block();
  8. LOGGER.info("Saving Order: " + order.getOrderId());
  9. // Get state from the state store
  10. State<Order> response = client.getState(DAPR_STATE_STORE, String.valueOf(orderId), Order.class).block();
  11. LOGGER.info("Getting Order: " + response.getValue().getOrderId());
  12. // Delete state from the state store
  13. client.deleteState(DAPR_STATE_STORE, String.valueOf(orderId)).block();
  14. LOGGER.info("Deleting Order: " + orderId);
  15. TimeUnit.MILLISECONDS.sleep(1000);
  16. }

Step 3: View the order-processor outputs

Notice, as specified in the code above, the code saves application state in the Dapr state store, reads it, then deletes it.

Order-processor output:

  1. == APP == INFO:root:Saving Order: {'orderId': '1'}
  2. == APP == INFO:root:Result after get: b"{'orderId': '1'}"
  3. == APP == INFO:root:Deleting Order: {'orderId': '1'}
  4. == APP == INFO:root:Saving Order: {'orderId': '2'}
  5. == APP == INFO:root:Result after get: b"{'orderId': '2'}"
  6. == APP == INFO:root:Deleting Order: {'orderId': '2'}
  7. == APP == INFO:root:Saving Order: {'orderId': '3'}
  8. == APP == INFO:root:Result after get: b"{'orderId': '3'}"
  9. == APP == INFO:root:Deleting Order: {'orderId': '3'}
  10. == APP == INFO:root:Saving Order: {'orderId': '4'}
  11. == APP == INFO:root:Result after get: b"{'orderId': '4'}"
  12. == APP == INFO:root:Deleting Order: {'orderId': '4'}
statestore.yaml component file

When you run dapr init, Dapr creates a default Redis statestore.yaml and runs a Redis container on your local machine, located:

  • On Windows, under %UserProfile%\.dapr\components\statestore.yaml
  • On Linux/MacOS, under ~/.dapr/components/statestore.yaml

With the statestore.yaml component, you can easily swap out the state store without making code changes.

The Redis statestore.yaml file included for this Quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: statestore
  5. spec:
  6. type: state.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""
  13. - name: actorStateStore
  14. value: "true"

In the YAML file:

  • metadata/name is how your application talks to the component (called DAPR_STORE_NAME in the code sample).
  • spec/metadata defines the connection to the Redis instance used by the component.

Pre-requisites

For this example, you will need:

Step 1: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Step 2: Manipulate service state

In a terminal window, navigate to the order-processor directory.

  1. cd state_management/go/sdk/order-processor

Install the dependencies and build the application:

  1. go build .

Run the order-processor service alongside a Dapr sidecar.

  1. dapr run --app-id order-processor --resources-path ../../../resources -- go run .

The order-processor service writes, reads, and deletes an orderId key/value pair to the statestore instance defined in the statestore.yaml component. As soon as the service starts, it performs a loop.

  1. client, err := dapr.NewClient()
  2. // Save state into the state store
  3. _ = client.SaveState(ctx, STATE_STORE_NAME, strconv.Itoa(orderId), []byte(order))
  4. log.Print("Saving Order: " + string(order))
  5. // Get state from the state store
  6. result, _ := client.GetState(ctx, STATE_STORE_NAME, strconv.Itoa(orderId))
  7. fmt.Println("Getting Order: " + string(result.Value))
  8. // Delete state from the state store
  9. _ = client.DeleteState(ctx, STATE_STORE_NAME, strconv.Itoa(orderId))
  10. log.Print("Deleting Order: " + string(order))

Step 3: View the order-processor outputs

Notice, as specified in the code above, the code saves application state in the Dapr state store, reads it, then deletes it.

Order-processor output:

  1. == APP == dapr client initializing for: 127.0.0.1:53689
  2. == APP == 2022/04/01 09:16:03 Saving Order: {"orderId":1}
  3. == APP == Getting Order: {"orderId":1}
  4. == APP == 2022/04/01 09:16:03 Deleting Order: {"orderId":1}
  5. == APP == 2022/04/01 09:16:03 Saving Order: {"orderId":2}
  6. == APP == Getting Order: {"orderId":2}
  7. == APP == 2022/04/01 09:16:03 Deleting Order: {"orderId":2}
  8. == APP == 2022/04/01 09:16:03 Saving Order: {"orderId":3}
  9. == APP == Getting Order: {"orderId":3}
  10. == APP == 2022/04/01 09:16:03 Deleting Order: {"orderId":3}
  11. == APP == 2022/04/01 09:16:03 Saving Order: {"orderId":4}
  12. == APP == Getting Order: {"orderId":4}
  13. == APP == 2022/04/01 09:16:03 Deleting Order: {"orderId":4}
  14. == APP == 2022/04/01 09:16:03 Saving Order: {"orderId":5}
  15. == APP == Getting Order: {"orderId":5}
  16. == APP == 2022/04/01 09:16:03 Deleting Order: {"orderId":5}
statestore.yaml component file

When you run dapr init, Dapr creates a default Redis statestore.yaml and runs a Redis container on your local machine, located:

  • On Windows, under %UserProfile%\.dapr\components\statestore.yaml
  • On Linux/MacOS, under ~/.dapr/components/statestore.yaml

With the statestore.yaml component, you can easily swap out the state store without making code changes.

The Redis statestore.yaml file included for this Quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: statestore
  5. spec:
  6. type: state.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""
  13. - name: actorStateStore
  14. value: "true"

In the YAML file:

  • metadata/name is how your application talks to the component (called DAPR_STORE_NAME in the code sample).
  • spec/metadata defines the connection to the Redis instance used by the component.

Tell us what you think!

We’re continuously working to improve our Quickstart examples and value your feedback. Did you find this quickstart helpful? Do you have suggestions for improvement?

Join the discussion in our discord channel.

Next steps

Explore Dapr tutorials >>

Last modified March 21, 2024: Merge pull request #4082 from newbe36524/v1.13 (f4b0938)