The documentation you are viewing is for Dapr v1.13 which is an older version of Dapr. For up-to-date documentation, see the latest version.
Quickstart: Configuration
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:
- Gets configuration items from the configuration store.
- Subscribes for configuration updates.
![Diagram that demonstrates the flow of the configuration API quickstart with key/value pairs used.](/images/configuration-quickstart/configuration-quickstart-flow.png)
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.
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
.
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.
cd configuration/python/sdk/order-processor
Install the dependencies:
pip3 install -r requirements.txt
Run the order-processor
service alongside a Dapr sidecar.
dapr run --app-id order-processor --resources-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 ofpython3 app.py
.
The expected output:
== APP == Configuration for orderId1 : value: "101"
== APP ==
== APP == Configuration for orderId2 : value: "102"
== APP ==
== 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:
docker exec dapr_redis redis-cli MSET orderId1 "103" orderId2 "104"
Run the order-processor
service again:
dapr run --app-id order-processor --resources-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 ofpython3 app.py
.
The app will return the updated configuration values:
== APP == Configuration for orderId1 : value: "103"
== APP ==
== APP == Configuration for orderId2 : value: "104"
== 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:
# Get config items from the config store
for config_item in CONFIGURATION_ITEMS:
config = client.get_configuration(store_name=DAPR_CONFIGURATION_STORE, keys=[config_item], config_metadata={})
print(f"Configuration for {config_item} : {config.items[config_item]}", flush=True)
Subscribe to configuration updates:
# Subscribe for configuration changes
configuration = await client.subscribe_configuration(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS)
Unsubscribe from configuration updates and exit the application:
# Unsubscribe from configuration updates
unsubscribed = True
for config_item in CONFIGURATION_ITEMS:
unsub_item = client.unsubscribe_configuration(DAPR_CONFIGURATION_STORE, config_item)
#...
if unsubscribed == True:
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.
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
.
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.
cd configuration/javascript/sdk/order-processor
Install the dependencies:
npm install
Run the order-processor
service alongside a Dapr sidecar.
dapr run --app-id order-processor --resources-path ../../../components/ --app-protocol grpc --dapr-grpc-port 3500 -- node index.js
The expected output:
== APP == Configuration for orderId1: {"key":"orderId1","value":"101","version":"","metadata":{}}
== APP == Configuration for orderId2: {"key":"orderId2","value":"102","version":"","metadata":{}}
== 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:
docker exec dapr_redis redis-cli MSET orderId1 "103" orderId2 "104"
Run the order-processor
service again:
dapr run --app-id order-processor --resources-path ../../../components/ --app-protocol grpc --dapr-grpc-port 3500 -- node index.js
The app will return the updated configuration values:
== APP == Configuration for orderId1: {"key":"orderId1","value":"103","version":"","metadata":{}}
== 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:
// Get config items from the config store
//...
const config = await client.configuration.get(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS);
Object.keys(config.items).forEach((key) => {
console.log("Configuration for " + key + ":", JSON.stringify(config.items[key]));
});
Subscribe to configuration updates:
// Subscribe to config updates
try {
const stream = await client.configuration.subscribeWithKeys(
DAPR_CONFIGURATION_STORE,
CONFIGURATION_ITEMS,
(config) => {
console.log("Configuration update", JSON.stringify(config.items));
}
);
Unsubscribe from configuration updates and exit the application:
// Unsubscribe to config updates and exit app after 20 seconds
setTimeout(() => {
stream.stop();
console.log("App unsubscribed to config changes");
process.exit(0);
},
Pre-requisites
For this example, you will need:
Step 1: Set up the environment
Clone the sample provided in the Quickstarts repo.
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
.
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.
cd configuration/csharp/sdk/order-processor
Recall NuGet packages:
dotnet restore
dotnet build
Run the order-processor
service alongside a Dapr sidecar.
dapr run --app-id order-processor-http --resources-path ../../../components/ --app-port 7001 -- dotnet run --project .
The expected output:
== APP == Configuration for orderId1: {"Value":"101","Version":"","Metadata":{}}
== APP == Configuration for orderId2: {"Value":"102","Version":"","Metadata":{}}
== 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:
docker exec dapr_redis redis-cli MSET orderId1 "103" orderId2 "104"
Run the order-processor
service again:
dapr run --app-id order-processor-http --resources-path ../../../components/ --app-port 7001 -- dotnet run --project .
The app will return the updated configuration values:
== APP == Configuration for orderId1: {"Value":"103","Version":"","Metadata":{}}
== 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:
// Get config from configuration store
GetConfigurationResponse config = await client.GetConfiguration(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS);
foreach (var item in config.Items)
{
var cfg = System.Text.Json.JsonSerializer.Serialize(item.Value);
Console.WriteLine("Configuration for " + item.Key + ": " + cfg);
}
Subscribe to configuration updates:
// Subscribe to config updates
SubscribeConfigurationResponse subscribe = await client.SubscribeConfiguration(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS);
Unsubscribe from configuration updates and exit the application:
// Unsubscribe to config updates and exit the app
try
{
client.UnsubscribeConfiguration(DAPR_CONFIGURATION_STORE, subscriptionId);
Console.WriteLine("App unsubscribed from config changes");
Environment.Exit(0);
}
Pre-requisites
For this example, you will need:
- Dapr CLI and initialized environment.
- Java JDK 17 (or greater):
- Oracle JDK, or
- OpenJDK
- Apache Maven, version 3.x.
Step 1: Set up the environment
Clone the sample provided in the Quickstarts repo.
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
.
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.
cd configuration/java/sdk/order-processor
Install the dependencies:
mvn clean install
Run the order-processor
service alongside a Dapr sidecar.
dapr run --app-id order-processor --resources-path ../../../components -- java -jar target/OrderProcessingService-0.0.1-SNAPSHOT.jar
The expected output:
== APP == Configuration for orderId1: {'value':'101'}
== APP == Configuration for orderId2: {'value':'102'}
== 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:
docker exec dapr_redis redis-cli MSET orderId1 "103" orderId2 "104"
Run the order-processor
service again:
dapr run --app-id order-processor --resources-path ../../../components -- java -jar target/OrderProcessingService-0.0.1-SNAPSHOT.jar
The app will return the updated configuration values:
== APP == Configuration for orderId1: {'value':'103'}
== 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:
// Get config items from the config store
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) {
for (String configurationItem : CONFIGURATION_ITEMS) {
ConfigurationItem item = client.getConfiguration(DAPR_CONFIGURATON_STORE, configurationItem).block();
System.out.println("Configuration for " + configurationItem + ": {'value':'" + item.getValue() + "'}");
}
Subscribe to configuration updates:
// Subscribe for config changes
Flux<SubscribeConfigurationResponse> subscription = client.subscribeConfiguration(DAPR_CONFIGURATON_STORE,
CONFIGURATION_ITEMS.toArray(String[]::new));
Unsubscribe from configuration updates and exit the application:
// Unsubscribe from config changes
UnsubscribeConfigurationResponse unsubscribe = client
.unsubscribeConfiguration(subscriptionId, DAPR_CONFIGURATON_STORE).block();
if (unsubscribe.getIsUnsubscribed()) {
System.out.println("App unsubscribed to config changes");
}
Pre-requisites
For this example, you will need:
Step 1: Set up the environment
Clone the sample provided in the Quickstarts repo.
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
.
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.
cd configuration/go/sdk/order-processor
Run the order-processor
service alongside a Dapr sidecar.
dapr run --app-id order-processor --app-port 6001 --resources-path ../../../components -- go run .
The expected output:
== APP == Configuration for orderId1: {"Value":"101","Version":"","Metadata":null}
== APP == Configuration for orderId2: {"Value":"102","Version":"","Metadata":null}
== APP == dapr configuration subscribe finished.
== 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:
docker exec dapr_redis redis-cli MSET orderId1 "103" orderId2 "104"
Run the order-processor
service again:
dapr run --app-id order-processor --app-port 6001 --resources-path ../../../components -- go run .
The app will return the updated configuration values:
== APP == Configuration for orderId1: {"Value":"103","Version":"","Metadata":null}
== 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:
// Get config items from config store
for _, item := range CONFIGURATION_ITEMS {
config, err := client.GetConfigurationItem(ctx, DAPR_CONFIGURATION_STORE, item)
//...
c, _ := json.Marshal(config)
fmt.Println("Configuration for " + item + ": " + string(c))
}
Subscribe to configuration updates:
// Subscribe for config changes
err = client.SubscribeConfigurationItems(ctx, DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS, func(id string, config map[string]*dapr.ConfigurationItem) {
// First invocation when app subscribes to config changes only returns subscription id
if len(config) == 0 {
fmt.Println("App subscribed to config changes with subscription id: " + id)
subscriptionId = id
return
}
})
Unsubscribe from configuration updates and exit the application:
// Unsubscribe to config updates and exit app after 20 seconds
select {
case <-ctx.Done():
err = client.UnsubscribeConfigurationItems(context.Background(), DAPR_CONFIGURATION_STORE, subscriptionId)
//...
{
fmt.Println("App unsubscribed to config changes")
}
Demo
Watch this video demoing the Configuration API quickstart:
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
- Use Dapr Configuration with HTTP instead of an SDK.
- Learn more about Configuration building block
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.