Over the past few years, Spring has become the de facto J2EE framework. For most of the Java developers it could be difficult to plan a new project without using Spring framework. A while ago, we started a series of articles about how to start building a microservice using Spring Boot and what else should you do to make that microservice production ready. Again, too much advertising, let’s focus on what we really want to discuss.
Starting from how much Spring has evolved over the time and how many distinct fields it covers today (AOP, Security, Data Access, Web), it was only a matter of time before it was introduced a module dedicated to linking the actual Spring application with the cloud technologies. Thus, it was launched Spring Cloud, that contains a dedicated module for AWS cloud, named Spring Cloud AWS. This component can be seen as a wrapper around the Java AWS SDK that aims to help developers to code in a manner recommended by Spring. Not all the AWS services are supported, but the most important like S3, SQS, SNS, SES, CloudFormation and ElastiCache are integrated.
While there are many reasons to integrate this library into your application like simplified configuration, authentication and access to resources, there is one important aspect in the current versions which you must take into account and about which we have already spoken in 2 posts: how AWS clients are configured. In fact, with Spring Cloud AWS you have zero control over the timeouts, retry strategies and so on.
Not to be misunderstood, we want to make clear the fact that Spring Cloud AWS is a great module in the context of Spring projects, bringing an abstraction layer that offers a common way to handle messages, to send mails, to work with caches and making the application logic agnostic about infrastructure details and thus, ignoring the transport layer or the storage technologies, but strictly speaking about the way the AWS clients are created we do believe some important details are missing and some workarounds are necessary in order to follow the good practices we believe in.
We offer you an example of how we believe it’s better to integrate Spring Cloud AWS in your application. The code example is available here. Clone the repository and start the service as it’s described in the README.md file. Meanwhile, create an SQS queue named test. If the AWS region is not EU-WEST-1, please adjust this aspect in the configuration file. Also, this example is very useful if you need to configure a proxy in the client configuration.
To start the service, run the following command, replacing <> with AWS access and secret keys.
java -Dspring.profiles.active=dev -Daws.accessKeyId=<> -Daws.secretKey=<> -jar target/demo-java-service-1.0-SAMPLE.jar
Then, in a separate terminal, call the service:
curl -X GET --header 'Accept: text/plain' 'http://localhost:8080/hello?name=Cirrus' Hello, Cirrus. This is my first REST service! Simple text message from dev stage
After that, check the AWS SQS console. The test queue should contain one new item, as you can see above:
Hoping that this example it’s useful and the arguments we presented are valid for you, we invite to weight any library or service before starting to integrate in your application. Please don’t hesitate to add your comments or questions below!