Introduction
Hello AWeSome techies ! Most of us would be familiarized with application configuration by one or the other way & that too with different set of profile depending on the environment or the technical boundaries. A configuration is a collection of settings that influence the behavior of our applications.
Today, we will learn how to retrieve AWS AppConfig using spring boot microservice.
Incase you are not sure how to create, configure & deploy AWS AppConfig through AWS management console then I would advise you to go through this article.
Tool(s)/Framework(s)
IDE — IntelliJ Idea/Eclipse
Language — Java 8 or above
Framework — Spring boot
SDK — AWS SDK
AWS Services — AppConfig
Cloud Provider — Amazon Web Service(AWS)
Build Tool — Maven
Repository
https://github.com/shethapurv/aws/tree/main/aws-app-config-service
Maven Dependencies
Please refer to the maven dependency given below which is required to get config from AWS AppConfig service.
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-appconfig</artifactId>
<version>1.12.272</version>
</dependency>
Important Concepts
Before getting into the implementation, let’s try to understand few concepts/classes & its significance.
aws-java-sdk-appconfig — We will use this maven dependency which will help us to connect to AWS AppConfig service from our spring boot application.
AmazonAppConfigClient- We will use this class from AWS SDK to connect to AWS AppConfig service from our spring boot microservice.
GetConfigurationRequest - We will use this class to build the request to retrieve configuration from AWS AppConfig.
Spring Boot Microservices Communication with AWS AppConfig
As we have understood the important concepts, let’s jump to the implementation.
Implementation
We will create a AWSAppConfig class which we will use to connect to AWS AppConfig service & to retrieve configuration based on the environment & profile. Let’s have a look,
GetConfigurationRequest.application — set the application id for which configuration is created.
GetConfigurationRequest.configuration — To indicate which configuration to pickup.
GetConfigurationRequest.environment — To get configuration for specific environment
public AWSAppConfig() {
appConfig = AmazonAppConfigClient._builder_().build();
request = new GetConfigurationRequest();
request. **setApplication** ("aws-app-config-service");
request. **setConfiguration** ("dev");
request. **setEnvironment** ("dev");
}
Now, we will implement a method which should return us a configuration from AWS AppConfig.
AmazonAppConfigClient.getConfiguration — this will get the configuration based on the configuration & environment set in the request.
public String **getConfiguration** () throws UnsupportedEncodingException {
GetConfigurationResult result = **appConfig.getConfiguration** (request);
String message = String._format_("contentType: %s", result.getContentType());
_LOGGER_.info(message);
if (!Objects._equals_("application/json", result.getContentType())) {
throw new IllegalStateException("config is expected to be JSON");
}
String content = new String(result.getContent().array(), "ASCII");
return content;
}
By now, you might think, all this is fine but how to verify whether it is really fetching the configuration or not! Don’t worry, let’s verify the same in next section.
Testing
Let’s implement an API endpoint using AWSAppConfig to retrieve configuration.
@RestController
@RequestMapping(path = " **/aws**", method = RequestMethod._GET_)
public class AWSAppConfigController {
@Autowired
AWSAppConfig awsAppConfig;
@RequestMapping(path = " **/getConfig**", method = RequestMethod._GET_)
public String getAWSAppConfig() throws UnsupportedEncodingException {
return **awsAppConfig.getConfiguration();**
}
}
Now, let’s run the application & hit the url localhost:8080/aws/getConfig. Ta..da.. we got the configuration from AWS AppConfig.
References
https://docs.aws.amazon.com/appconfig/latest/userguide/what-is-appconfig.html
If this post was helpful, please clap for few times or follow to show your support which keeps me constantly motivated to share my learnings.
Learning, sharing & growing together.
Top comments (0)