Quickstart: Configuration

Get started with Dapr’s Configuration building block

Let’s take a look at Dapr’s Configuration building block. A configuration item is often dynamic in nature and tightly coupled to the needs of the application that consumes it. Configuration items are key/value pairs containing configuration data, such as:

  • App ids
  • Partition keys
  • Database names, etc

In this quickstart, you’ll run an order-processor microservice that utilizes the Configuration API. The service:

  1. Gets configuration items from the configuration store.
  2. Subscribes for configuration updates.

Diagram that demonstrates the flow of the configuration API quickstart with key/value pairs used.

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

Once cloned, open a new terminal and run the following command to set values for configuration items orderId1 and orderId2.

  1. docker exec dapr_redis redis-cli MSET orderId1 "101" orderId2 "102"

Step 2: Run the order-processor service

From the root of the Quickstarts clone directory, navigate to the order-processor directory.

  1. cd configuration/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 --components-path ../../../components/ --app-port 6001 -- 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 expected output:

  1. == APP == Configuration for orderId1 : value: "101"
  2. == APP ==
  3. == APP == Configuration for orderId2 : value: "102"
  4. == APP ==
  5. == APP == App unsubscribed from config changes

(Optional) Step 3: Update configuration item values

Once the app has unsubscribed, try updating the configuration item values. Change the orderId1 and orderId2 values using the following command:

  1. docker exec dapr_redis redis-cli MSET orderId1 "103" orderId2 "104"

Run the order-processor service again:

  1. dapr run --app-id order-processor --components-path ../../../components/ --app-port 6001 -- 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 app will return the updated configuration values:

  1. == APP == Configuration for orderId1 : value: "103"
  2. == APP ==
  3. == APP == Configuration for orderId2 : value: "104"
  4. == APP ==

The order-processor service

The order-processor service includes code for:

  • Getting the configuration items from the config store
  • Subscribing to configuration updates (which you made in the CLI earlier)
  • Unsubscribing from configuration updates and exiting the app after 20 seconds of inactivity.

Get configuration items:

  1. # Get config items from the config store
  2. for config_item in CONFIGURATION_ITEMS:
  3. config = client.get_configuration(store_name=DAPR_CONFIGURATION_STORE, keys=[config_item], config_metadata={})
  4. print(f"Configuration for {config_item} : {config.items[config_item]}", flush=True)

Subscribe to configuration updates:

  1. # Subscribe for configuration changes
  2. configuration = await client.subscribe_configuration(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS)

Unsubscribe from configuration updates and exit the application:

  1. # Unsubscribe from configuration updates
  2. unsubscribed = True
  3. for config_item in CONFIGURATION_ITEMS:
  4. unsub_item = client.unsubscribe_configuration(DAPR_CONFIGURATION_STORE, config_item)
  5. #...
  6. if unsubscribed == True:
  7. print("App unsubscribed from config changes", flush=True)

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

Once cloned, open a new terminal and run the following command to set values for configuration items orderId1 and orderId2.

  1. docker exec dapr_redis redis-cli MSET orderId1 "101" orderId2 "102"

Step 2: Run the order-processor service

From the root of the Quickstarts clone directory, navigate to the order-processor directory.

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

Install the dependencies:

  1. npm install

Run the order-processor service alongside a Dapr sidecar.

  1. dapr run --app-id order-processor --components-path ../../../components/ --app-protocol grpc --dapr-grpc-port 3500 -- node index.js

The expected output:

  1. == APP == Configuration for orderId1: {"key":"orderId1","value":"101","version":"","metadata":{}}
  2. == APP == Configuration for orderId2: {"key":"orderId2","value":"102","version":"","metadata":{}}
  3. == APP == App unsubscribed to config changes

(Optional) Step 3: Update configuration item values

Once the app has unsubscribed, try updating the configuration item values. Change the orderId1 and orderId2 values using the following command:

  1. docker exec dapr_redis redis-cli MSET orderId1 "103" orderId2 "104"

Run the order-processor service again:

  1. dapr run --app-id order-processor --components-path ../../../components/ --app-protocol grpc --dapr-grpc-port 3500 -- node index.js

The app will return the updated configuration values:

  1. == APP == Configuration for orderId1: {"key":"orderId1","value":"103","version":"","metadata":{}}
  2. == APP == Configuration for orderId2: {"key":"orderId2","value":"104","version":"","metadata":{}}

The order-processor service

The order-processor service includes code for:

  • Getting the configuration items from the config store
  • Subscribing to configuration updates (which you made in the CLI earlier)
  • Unsubscribing from configuration updates and exiting the app after 20 seconds of inactivity.

Get configuration items:

  1. // Get config items from the config store
  2. //...
  3. const config = await client.configuration.get(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS);
  4. Object.keys(config.items).forEach((key) => {
  5. console.log("Configuration for " + key + ":", JSON.stringify(config.items[key]));
  6. });

Subscribe to configuration updates:

  1. // Subscribe to config updates
  2. try {
  3. const stream = await client.configuration.subscribeWithKeys(
  4. DAPR_CONFIGURATION_STORE,
  5. CONFIGURATION_ITEMS,
  6. (config) => {
  7. console.log("Configuration update", JSON.stringify(config.items));
  8. }
  9. );

Unsubscribe from configuration updates and exit the application:

  1. // Unsubscribe to config updates and exit app after 20 seconds
  2. setTimeout(() => {
  3. stream.stop();
  4. console.log("App unsubscribed to config changes");
  5. process.exit(0);
  6. },

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

Once cloned, open a new terminal and run the following command to set values for configuration items orderId1 and orderId2.

  1. docker exec dapr_redis redis-cli MSET orderId1 "101" orderId2 "102"

Step 2: Run the order-processor service

From the root of the Quickstarts clone directory, navigate to the order-processor directory.

  1. cd configuration/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-http --components-path ../../../components/ --app-port 7001 -- dotnet run --project .

The expected output:

  1. == APP == Configuration for orderId1: {"Value":"101","Version":"","Metadata":{}}
  2. == APP == Configuration for orderId2: {"Value":"102","Version":"","Metadata":{}}
  3. == APP == App unsubscribed from config changes

(Optional) Step 3: Update configuration item values

Once the app has unsubscribed, try updating the configuration item values. Change the orderId1 and orderId2 values using the following command:

  1. docker exec dapr_redis redis-cli MSET orderId1 "103" orderId2 "104"

Run the order-processor service again:

  1. dapr run --app-id order-processor-http --components-path ../../../components/ --app-port 7001 -- dotnet run --project .

The app will return the updated configuration values:

  1. == APP == Configuration for orderId1: {"Value":"103","Version":"","Metadata":{}}
  2. == APP == Configuration for orderId2: {"Value":"104","Version":"","Metadata":{}}

The order-processor service

The order-processor service includes code for:

  • Getting the configuration items from the config store
  • Subscribing to configuration updates (which you made in the CLI earlier)
  • Unsubscribing from configuration updates and exiting the app after 20 seconds of inactivity.

Get configuration items:

  1. // Get config from configuration store
  2. GetConfigurationResponse config = await client.GetConfiguration(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS);
  3. foreach (var item in config.Items)
  4. {
  5. var cfg = System.Text.Json.JsonSerializer.Serialize(item.Value);
  6. Console.WriteLine("Configuration for " + item.Key + ": " + cfg);
  7. }

Subscribe to configuration updates:

  1. // Subscribe to config updates
  2. SubscribeConfigurationResponse subscribe = await client.SubscribeConfiguration(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS);

Unsubscribe from configuration updates and exit the application:

  1. // Unsubscribe to config updates and exit the app
  2. try
  3. {
  4. client.UnsubscribeConfiguration(DAPR_CONFIGURATION_STORE, subscriptionId);
  5. Console.WriteLine("App unsubscribed from config changes");
  6. Environment.Exit(0);
  7. }

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

Once cloned, open a new terminal and run the following command to set values for configuration items orderId1 and orderId2.

  1. docker exec dapr_redis redis-cli MSET orderId1 "101" orderId2 "102"

Step 2: Run the order-processor service

From the root of the Quickstarts clone directory, navigate to the order-processor directory.

  1. cd configuration/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 --components-path ../../../components -- java -jar target/OrderProcessingService-0.0.1-SNAPSHOT.jar

The expected output:

  1. == APP == Configuration for orderId1: {'value':'101'}
  2. == APP == Configuration for orderId2: {'value':'102'}
  3. == APP == App unsubscribed to config changes

(Optional) Step 3: Update configuration item values

Once the app has unsubscribed, try updating the configuration item values. Change the orderId1 and orderId2 values using the following command:

  1. docker exec dapr_redis redis-cli MSET orderId1 "103" orderId2 "104"

Run the order-processor service again:

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

The app will return the updated configuration values:

  1. == APP == Configuration for orderId1: {'value':'103'}
  2. == APP == Configuration for orderId2: {'value':'104'}

The order-processor service

The order-processor service includes code for:

  • Getting the configuration items from the config store
  • Subscribing to configuration updates (which you made in the CLI earlier)
  • Unsubscribing from configuration updates and exiting the app after 20 seconds of inactivity.

Get configuration items:

  1. // Get config items from the config store
  2. try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) {
  3. for (String configurationItem : CONFIGURATION_ITEMS) {
  4. ConfigurationItem item = client.getConfiguration(DAPR_CONFIGURATON_STORE, configurationItem).block();
  5. System.out.println("Configuration for " + configurationItem + ": {'value':'" + item.getValue() + "'}");
  6. }

Subscribe to configuration updates:

  1. // Subscribe for config changes
  2. Flux<SubscribeConfigurationResponse> subscription = client.subscribeConfiguration(DAPR_CONFIGURATON_STORE,
  3. CONFIGURATION_ITEMS.toArray(String[]::new));

Unsubscribe from configuration updates and exit the application:

  1. // Unsubscribe from config changes
  2. UnsubscribeConfigurationResponse unsubscribe = client
  3. .unsubscribeConfiguration(subscriptionId, DAPR_CONFIGURATON_STORE).block();
  4. if (unsubscribe.getIsUnsubscribed()) {
  5. System.out.println("App unsubscribed to config changes");
  6. }

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

Once cloned, open a new terminal and run the following command to set values for configuration items orderId1 and orderId2.

  1. docker exec dapr_redis redis-cli MSET orderId1 "101" orderId2 "102"

Step 2: Run the order-processor service

From the root of the Quickstarts clone directory, navigate to the order-processor directory.

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

Run the order-processor service alongside a Dapr sidecar.

  1. dapr run --app-id order-processor --app-port 6001 --components-path ../../../components -- go run .

The expected output:

  1. == APP == Configuration for orderId1: {"Value":"101","Version":"","Metadata":null}
  2. == APP == Configuration for orderId2: {"Value":"102","Version":"","Metadata":null}
  3. == APP == dapr configuration subscribe finished.
  4. == APP == App unsubscribed to config changes

(Optional) Step 3: Update configuration item values

Once the app has unsubscribed, try updating the configuration item values. Change the orderId1 and orderId2 values using the following command:

  1. docker exec dapr_redis redis-cli MSET orderId1 "103" orderId2 "104"

Run the order-processor service again:

  1. dapr run --app-id order-processor --app-port 6001 --components-path ../../../components -- go run .

The app will return the updated configuration values:

  1. == APP == Configuration for orderId1: {"Value":"103","Version":"","Metadata":null}
  2. == APP == Configuration for orderId2: {"Value":"104","Version":"","Metadata":null}

The order-processor service

The order-processor service includes code for:

  • Getting the configuration items from the config store
  • Subscribing to configuration updates (which you made in the CLI earlier)
  • Unsubscribing from configuration updates and exiting the app after 20 seconds of inactivity.

Get configuration items:

  1. // Get config items from config store
  2. for _, item := range CONFIGURATION_ITEMS {
  3. config, err := client.GetConfigurationItem(ctx, DAPR_CONFIGURATION_STORE, item)
  4. //...
  5. c, _ := json.Marshal(config)
  6. fmt.Println("Configuration for " + item + ": " + string(c))
  7. }

Subscribe to configuration updates:

  1. // Subscribe for config changes
  2. err = client.SubscribeConfigurationItems(ctx, DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS, func(id string, config map[string]*dapr.ConfigurationItem) {
  3. // First invocation when app subscribes to config changes only returns subscription id
  4. if len(config) == 0 {
  5. fmt.Println("App subscribed to config changes with subscription id: " + id)
  6. subscriptionId = id
  7. return
  8. }
  9. })

Unsubscribe from configuration updates and exit the application:

  1. // Unsubscribe to config updates and exit app after 20 seconds
  2. select {
  3. case <-ctx.Done():
  4. err = client.UnsubscribeConfigurationItems(context.Background(), DAPR_CONFIGURATION_STORE, subscriptionId)
  5. //...
  6. {
  7. fmt.Println("App unsubscribed to config changes")
  8. }

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 January 16, 2023: docs: improve consistency on directory mvment prefix (#3061) (05bf098c)