How-To: Manage application configuration

Learn how to get application configuration and watch for changes

Introduction

Consuming application configuration is a common task when writing applications and frequently configuration stores are used to manage this configuration data. A configuration item is often dynamic in nature and is tightly coupled to the needs of the application that consumes it. For example, common uses for application configuration include names of secrets that need to be retrieved, different identifiers, partition or consumer IDs, names of databased to connect to etc. These configuration items are typically stored as key-value items in a database.

Dapr provides a State Management API) that is based on key-value stores. However, application configuration can be changed by either developers or operators at runtime and the developer needs to be notified of these changes in order to take the required action and load the new configuration. Also the configuration data may want to be read only. Dapr’s Configuration API allows developers to consume configuration items that are returned as key/value pair and subscribe to changes whenever a configuration item changes.

*This API is currently in Alpha state and only available on gRPC. An HTTP1.1 supported version with this URL /v1.0/configuration will be available before the API becomes stable. *

This HowTo uses the Redis configuration store component as an exmaple to retrieve a configuration item.

Step 1: Save a configuration item

First, create a configuration item in a supported configuration store. This can be a simple key-value item, with any key of your choice. For this example, we’ll use the Redis configuration store component

Run Redis with Docker

  1. docker run --name my-redis -p 6379:6379 -d redis

Save an item

Using the Redis CLI, connect to the Redis instance:

  1. redis-cli -p 6379

Save a configuration item:

  1. set myconfig "wookie"

Configure a Dapr configuration store

Save the following file component file, for example to the default components folder on your machine. You can use this as the Dapr component YAML for Kubernetes using kubectl or when running with the Dapr CLI. Hint: The Redis configuration component has identical metadata to the Redis statestore component, so you can simply copy and change the component type if you already have a Redis statestore YAML file.

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: redisconfiguration
  5. spec:
  6. type: configuration.redis
  7. metadata:
  8. - name: redisHost
  9. value: localhost:6379

Get configuration items using gRPC API

Using your favorite language, create a Dapr gRPC client from the Dapr proto. The following examples show Java, C#, Python and Javascript clients.

  1. Dapr.ServiceBlockingStub stub = Dapr.newBlockingStub(channel);
  2. stub.GetConfigurationAlpha1(new GetConfigurationRequest{ StoreName = "redisconfig", Keys = new String[]{"myconfig"} });
  1. var call = client.GetConfigurationAlpha1(new GetConfigurationRequest { StoreName = "redisconfig", Keys = new String[]{"myconfig"} });
  1. response = stub.GetConfigurationAlpha1(request={ StoreName: 'redisconfig', Keys = ['myconfig'] })
  1. client.GetConfigurationAlpha1({ StoreName: 'redisconfig', Keys = ['myconfig'] })

Watch configuration items

Using your favorite language, create a Dapr gRPC client from the Dapr proto. Use the proto method SubscribeConfigurationAlpha1 on your client stub to start subscribing to events. The method accepts the following request object:

  1. message SubscribeConfigurationRequest {
  2. // The name of configuration store.
  3. string store_name = 1;
  4. // Optional. The key of the configuration item to fetch.
  5. // If set, only query for the specified configuration items.
  6. // Empty list means fetch all.
  7. repeated string keys = 2;
  8. // The metadata which will be sent to configuration store components.
  9. map<string,string> metadata = 3;
  10. }

Using this method, you can subscribe to changes in specific keys for a given configuration store. gRPC streaming varies widely based on language - see the gRPC examples here for usage.

Last modified November 12, 2021 : Merge pull request #1949 from willtsai/az-staticwebapp-versioning (c40e456)