Using DAPR Postgres Configuration Store
DAPR – PostGres Configuration Store .
With the help of DAPR Configuration building block we can fetch the configuration items from the configuration store and also subscribe for configuration updates .
A configuration item is often dynamic in nature and tightly coupled to the needs of the application that consumes it.
For Setup Postgres Configuration Store . Following the official Link : Postgres Configuration Store
- Get Configuration Items from the Postgres Configuration Store.Β Β To fetch the configuration item we can use the following code in JAVA to get the item.
try (DaprClient client = (new DaprClientBuilder()).build()) {
for (String configurationItem : CONFIGURATION_ITEMS) {
ConfigurationItem item = client.getConfiguration(DAPR_CONFIGURATON_STORE, configurationItem).block();
System.out.println("Configuration for " + configurationItem + ": {'value':'" + item.getValue() + "'}");
}
} catch (Exception e) {
System.out.println("Could not get config item, err:" + e.getMessage());
System.exit(1);
}
2. Subscribe to configuration Updates from Postgres Configuration Store.
Since, we need to mention the name of PG-Notify channel to which we are subscribing , update the function with the following parameters .
try (DaprClient client = (new DaprClientBuilder()).build()) {
// Create a Metadata for storing PG-notify channel name
Map<String,String> metadata = new HashMap<>();
metadata.put("pgNotifyChannel","config");
// Subscribe for config changes
// ******** Change the function parametes here ***********
Flux subscription = client.subscribeConfiguration(DAPR_CONFIGURATON_STORE,
CONFIGURATION_ITEMS,metadata);
// Read config changes for 20 seconds
subscription.subscribe((response) -> {
// First ever response contains the subscription id
if (response.getItems() == null || response.getItems().isEmpty()) {
subscriptionId = response.getSubscriptionId();
System.out.println("App subscribed to config changes with subscription id: " + subscriptionId);
} else {
response.getItems().forEach((k, v) -> {
System.out.println("Configuration update for " + k + ": {'value':'" + v.getValue() + "'}");
});
}
});
3 . Unsubscribe from Postgres configuration updates.
// Unsubscribe from config changes
UnsubscribeConfigurationResponse unsubscribe = client
.unsubscribeConfiguration(subscriptionId, DAPR_CONFIGURATON_STORE).block();
if (unsubscribe.getIsUnsubscribed()) {
System.out.println("App unsubscribed to config changes");
}