Background
Spring Cloud provides all the tools that you require to create cloud ready microservices. One of the infrastructure components that Spring-Cloud provides is a Configuration server to centralize the properties of an application, however it is possible that you that you may be using other solutions to manage the properties. One such solution is Netflix Archaius and if you work with Netflix Archaius there is a neat way that Spring-Cloud provides to integrate with it.Integration With Archaius
Spring Cloud provides a Spring Boot Auto-configuration for Archaius which gets triggered on finding the Archaius related libraries with the application. So first to pull in the Archaius libraries, which can be done through the following dependency entry in the POM file:<dependency> <groupId>com.netflix.archaius</groupId> <artifactId>archaius-core</artifactId> </dependency>Not that the version of the dependency need not be specified, this information flows in from the dependency management information in the parent POM’s.
With this new library in place, Archaius Configuration, all that now needs to be done is to define Spring beans which extend Apache Commons Configuration AbstractConfiguration class and these would automatically get configured by Spring Cloud. As an example consider the following AbstractConfiguration which has one property in it:
@Bean public AbstractConfiguration sampleArchaiusConfiguration() throws Exception { ConcurrentMapConfiguration concurrentMapConfiguration = new ConcurrentMapConfiguration(); concurrentMapConfiguration.addProperty("testkey", "testvalue"); return concurrentMapConfiguration; }
That is essentially it, this property should now be visible as an Archaius property and can be accessed along these lines:
DynamicPropertyFactory.getInstance().getStringProperty("testkey", "").get()
Also there are a few more neat features provided through Archaius integration in Spring-Cloud:
- The Spring managed properties are visible as Archaius properties
- An endpoint(/archaius) is provided by Spring-Cloud where all the registered archaius properties can be viewed
Hi Biju , Good Explanation!
ReplyDeleteCan you please provide sample code for Spring boot and archaius integration ?