A simple Google search reveals tons of articles trying to explain the concepts, the advantages and the migration process from a monolithic to a distributed architecture based on microservices, so we’ll assume you are already familiar with these aspects and we’ll skip this part. This blog post is focused on how to create a very simple Java service that exposes a REST API and then, in the next articles, we’ll come with a couple of advice and best practices about configuring your service and running it in Elastic Beanstalk.
What we’ll use to create the service:
– Spring/Spring IO for building the API
– Maven for dependency management and building the artifacts
– AWS Console for interacting with Elastic Beanstalk
The source code for this “Hello World” application is available for download here. We’ll let you load it in your favorite IDE and browse through it to become familiar with it. We’ll continue with presenting how to run it locally. This step is important since we want to test it before deploying and making available to the clients. Also, running it locally in a similar environment like that will be used in production increase the chances to detect faster any issue.
Now, let’s return to our service and wrap our code into a jar binary file to be run as a Java application. Let’s browse where is located the pom.xml file and build our code:
After the build is successfully done, we can see that under target folder, there are several files (and folders) created, one being named demo-java-service-1.0-SAMPLE.jar.
We are now ready to start the application and for that, the single thing to do is to run the jar mention above as a Java application.
java -jar target/demo-java-service-1.0-SAMPLE.jar
To see if the service is up, run the following command in a terminal:
curl -0 http://localhost:8080/hello?name=John Hello, John. This is my first REST service !
Once we tested it locally and anything is OK, we can to deploy our code on a remote service to make it available for others.
Let’s start to create an Elastic Beanstalk application using the wizard provided in the Beanstalk Console. A step by step example to create an application is available in the AWS Beanstalk documentation. When you create the environment, for the environment type, select Web server tier -> Java platform and on Application code section upload the jar created above. Also, please make sure you chose the Single Instance instead of Load Balancer environment type. Launch the environment and wait until the bootstrap process is done – status is displayed in the Dashboard section:
Also, check the environment URL available in the Dashboard section to confirm service started. After that, check in the EC2 console (EC2 Console – Security Groups section – select security group of this environment – Inbound rules) that incoming traffic on port 8080 is allowed.
Let’s test the service is working:
curl -0 http://<environment_URL>:8080/?name=John Hello, John. This is my first REST service !
At this moment, we have a very simple application that exposes an API in a REST manner, able to read different parameters and input values according to the stage the application runs. As we mention at the beginning, this application is just an example, but on the same time is a starting point to create a service that does something useful. Stay tuned for future posts with advice about how to make it configurable, how to aim to 100% availability and how to ensure an easy maintenance to your service.
If you have any question or feedback, we welcome your comments below!