1. Overview
The Netflix Archaius offers libraries and functionality for connecting to many data sources.
In this tutorial, we’ll learn how to get configurations:
- Using the JDBC API to connect to a database
- From configurations stored in a DynamoDB instance
- By configuring Zookeeper as a dynamic distributed configuration
For the introduction to Netflix Archaius, please have a look at this article.
2. Using Netflix Archaius with a JDBC Connection
As we explained in the introductory tutorial, whenever we want Archaius to handle the configurations, we’ll need to create an Apache’s AbstractConfiguration bean.
The bean will be automatically captured by the Spring Cloud Bridge and added to the Archaius’ Composite Configuration stack.
2.1. Dependencies
All the functionality required to connect to a database using JDBC is included in the core library, so we won’t need any extra dependency apart from the ones we mentioned in the introductory tutorial:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-archaius</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix</artifactId> <version>2.0.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
We can check Maven Central to verify we’re using the latest version of the starter library.
2.2. How to Create the Configuration Bean
In this case, we’ll need to create the AbstractConfiguration bean using a JDBCConfigurationSource instance.
To indicate how to obtain the values from the JDBC database, we’ll have to specify:
- a javax.sql.Datasource object
- a SQL query string that will retrieve at least two columns with the configurations’ keys and its corresponding values
- two columns indicating the properties keys and values, respectively
Let’s go ahead then an create this bean:
@Autowired DataSource dataSource; @Bean public AbstractConfiguration addApplicationPropertiesSource() { PolledConfigurationSource source = new JDBCConfigurationSource(dataSource, "select distinct key, value from properties", "key", "value"); return new DynamicConfiguration(source, new FixedDelayPollingScheduler()); }
2.3. Trying It Out
To keep it simple and still have an operative example, we’ll set up an H2 in-memory database instance with some initial data.
To achieve this, we’ll first add the necessary dependencies:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>2.0.5.RELEASE</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.197</version> <scope>runtime</scope> </dependency>
Note: we can check the latest versions of the h2 and the spring-boot-starter-data-jpa libraries in Maven Central.
Next, we’ll declare the JPA entity that will contain our properties:
@Entity public class Properties { @Id private String key; private String value; }
And we’ll include a data.sql file in our resources to populate the in-memory database with some initial values:
insert into properties values('baeldung.archaius.properties.one', 'one FROM:jdbc_source');
Finally, to check the value of the property at any given point, we can create an endpoint that retrieves the values managed by Archaius:
@RestController public class ConfigPropertiesController { private DynamicStringProperty propertyOneWithDynamic = DynamicPropertyFactory .getInstance() .getStringProperty("baeldung.archaius.properties.one", "not found!"); @GetMapping("/properties-from-dynamic") public Map<String, String> getPropertiesFromDynamic() { Map<String, String> properties = new HashMap<>(); properties.put(propertyOneWithDynamic.getName(), propertyOneWithDynamic.get()); return properties; } }
If the data changes at any point, Archaius will detect it at runtime and will start retrieving the new values.
This endpoint can be used in the next examples as well, of course.
3. How to Create a Configuration Source Using a DynamoDB Instance
As we did in the last section, we’ll create a fully functional project to analyze properly how Archaius manages properties using a DynamoDB instance as a source of configurations.
3.1. Dependencies
Let’s add the following libraries to our pom.xml file:
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-dynamodb</artifactId> <version>1.11.414</version> </dependency> <dependency> <groupId>com.github.derjust</groupId> <artifactId>spring-data-dynamodb</artifactId> <version>5.0.3</version> </dependency> <dependency> <groupId>com.netflix.archaius</groupId> <artifactId>archaius-aws</artifactId> <version>0.7.6</version> </dependency>
We can check Maven Central for the latest dependencies versions, but for the archaius-aws one, we suggest sticking to the version supported by the Spring Cloud Netflix library.
The aws-java-sdk-dynamodb dependency will allow us to set up the DynamoDB client to connect to the database.
With the spring-data-dynamodb library, we will set up the DynamoDB repository.
And lastly, we’ll use the archaius-aws library to create the AbstractConfiguration.
3.2. Using DynamoDB as a Configuration Source
This time, the AbstractConfiguration will be created using a DynamoDbConfigurationSource object:
@Autowired AmazonDynamoDB amazonDynamoDb; @Bean public AbstractConfiguration addApplicationPropertiesSource() { PolledConfigurationSource source = new DynamoDbConfigurationSource(amazonDynamoDb); return new DynamicConfiguration( source, new FixedDelayPollingScheduler()); }
By default Archaius searches for a table named ‘archaiusProperties’, containing a ‘key’ and a ‘value’ attributes in the Dynamo database to use as a source.
If we want to override those values, we’ll have to declare the following system properties:
- com.netflix.config.dynamo.tableName
- com.netflix.config.dynamo.keyAttributeName
- com.netflix.config.dynamo.valueAttributeName
3.3. Creating a Fully Functional Example
As we did in this DynamoDB guide, we’ll start by installing a local DynamoDB instance to test the functionality easily.
We’ll also follow the instructions of the guide to create the AmazonDynamoDB instance that we ‘autowired’ previously.
And to populate the database with some initial data, we’ll first create a DynamoDBTable entity to map the data:
@DynamoDBTable(tableName = "archaiusProperties") public class ArchaiusProperties { @DynamoDBHashKey @DynamoDBAttribute private String key; @DynamoDBAttribute private String value; // ...getters and setters... }
Next, we’ll create a CrudRepository for this entity:
public interface ArchaiusPropertiesRepository extends CrudRepository<ArchaiusProperties, String> {}
And finally, we’ll use the repository and the AmazonDynamoDB instance to create the table and insert the data afterward:
@Autowired private ArchaiusPropertiesRepository repository; @Autowired AmazonDynamoDB amazonDynamoDb; private void initDatabase() { DynamoDBMapper mapper = new DynamoDBMapper(amazonDynamoDb); CreateTableRequest tableRequest = mapper .generateCreateTableRequest(ArchaiusProperties.class); tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L)); TableUtils.createTableIfNotExists(amazonDynamoDb, tableRequest); ArchaiusProperties property = new ArchaiusProperties("baeldung.archaius.properties.one", "one FROM:dynamoDB"); repository.save(property); }
We can call this method right before creating the DynamoDbConfigurationSource.
We’re all set now to run the application.
4. How to Set up a Dynamic Zookeeper Distributed Configuration
As we’ve seen before on out introductory Zookeeper article, one of the benefits of this tool is the possibility of using it as a distributed configuration store.
If we combine it with Archaius, we end up with a flexible and scalable solution for configuration management.
4.1. Dependencies
Let’s follow the official Spring Cloud’s instructions to set up the more stable version of Apache’s Zookeeper.
The only difference is that we only need a part of the functionality provided by Zookeeper, thus we can use the spring-cloud-starter-zookeeper-config dependency instead of the one used in the official guide:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-config</artifactId> <version>2.0.0.RELEASE</version> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.13</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency>
Again, we can check the latest versions of spring-cloud-starter-zookeeper-config and zookeeper dependencies in Maven Central.
Please make sure to avoid the zookeeper beta versions.
4.2. Spring Cloud’s Automatic Configuration
As it’s explained in the official documentation, including the spring-cloud-starter-zookeeper-config dependency is enough to set up the Zookeeper property sources.
By default, only one source is autoconfigured, searching properties under the config/application Zookeeper node. This node is therefore used as a shared configuration source between different applications.
Additionally, if we specify an application name using the spring.application.name property, another source is configured automatically, this time searching properties in the config/<app_name> node.
Each node name under these parent nodes will indicate a property key, and their data will be the property value.
Luckily for us, since Spring Cloud adds these property sources to the context, Archaius manages them automatically. There is no need to create an AbstractConfiguration programmatically.
4.3. Preparing the Initial Data
In this case, we’ll also need a local Zookeeper server to store the configurations as nodes. We can follow this Apache’s guide to set up a standalone server that runs on port 2181.
To connect to the Zookeeper service and create some initial data, we’ll use the Apache’s Curator client:
@Component public class ZookeeperConfigsInitializer { @Autowired CuratorFramework client; @EventListener public void appReady(ApplicationReadyEvent event) throws Exception { createBaseNodes(); if (client.checkExists().forPath("/config/application/baeldung.archaius.properties.one") == null) { client.create() .forPath("/config/application/baeldung.archaius.properties.one", "one FROM:zookeeper".getBytes()); } else { client.setData() .forPath("/config/application/baeldung.archaius.properties.one", "one FROM:zookeeper".getBytes()); } } private void createBaseNodes() throws Exception { if (client.checkExists().forPath("/config") == null) { client.create().forPath("/config"); } if (client.checkExists().forPath("/config/application") == null) { client.create().forPath("/config/application"); } } }
We can check the logs to see the property sources to verify that Netflix Archaius refreshed the properties once they change.
5. Conclusion
In this article, we’ve learned how we can setup advanced configuration sources using Netflix Archaius. We have to take into consideration that it supports other sources as well, such as Etcd, Typesafe, AWS S3 files, and JClouds.
As always, we can check out all the examples in our Github repo.