This blog series is about the Spring Boot Microservice. Here, Part 1 is the starting blog on it, here we will prepare the foundation for understanding Spring Boot Microservices. Let’s dive in.
Page Contents
What is MicroService
A piece of code, which runs independently and serves the specific purpose of the software. For example, a search feature on a website can work independently even if there is a failure in catalog loading or feed loading. Most of the big social media portals like Facebook, Amazon or LinkedIn has Microservices implemented in their Architecture.
This Microservice could be frontend or backend microservice. There are many technologies in the industry about writing microservice, however, if one is looking for writing in Java then SpringBoot is the best option to choose from. But, wait, why only SpringBoot Microservice, why not python Flask or Go GoMicro. Let’s check the answer in next section.
What are Spring boot Microservices
Spring Boot MicroService has become a de-facto standard for writing the Microservices in the industry in recent years. Why SpringBoot has become so popular, because of its simplicity and configuration options it provides to deal with various components of standalone application writing.
- If you want to interact with database tables, there is a JPA Repositories available.
- If you have to read from the rest endpoints, then there is a RestTemplate Annotation available.
- If the application has to read the HttpRequest and parse the parameters, then RequestMapping annotation makes it easier.
- If the application has to send back the HttpResponse, then ResponseBody annotation makes this process simple.
- Any many other benefits…
JPA for Spring Boot Microservice
SpringBoot Microservice will have its own data base. To perform CRUD operations on the database, the Java Persistance API provides a way to do operations on the database tables from Java objects. Spring framework has made accessing the databases by abstracting the implementation of boilerplate code. As a coder you just write repository interfaces, provides custom search methods and Spring will provide you the implementation of writing to or reading from database.
public interface EmployeeRepo extends CrudRepository<Employee,Integer> { List<Employee> findByTech(String tech); List<Employee> findByEmpIdGreaterThan(int empId);
And within the controller code, you just call these methods
repository.findByTech("java"));
Rest API for SpringBoot Microservice
The Spring Boot MicroService proved to be a best option to write a Rest API in Java programming language. It has many features which makes it framework of choice for writing the Rest API. The use of Annotations within Spring or SpringBoot makes the life easier of programmer.
- Support of Reading data from the Rest API by using @RestTemplate annotation.
- Reading the data sent by user and converting this to Java Object or Sending the data from the Service to the user. This is possible by using @RequestMapping and @ResponseBody Annotations.
@RequestMapping(path="/aliens", produces = {"application/xml"}) // produces can specify the type of content would be supported to return back.
@ResponseBody // this signifies the response of the request made, should be printed on page.
If you want to read further on different annotation in spring or Spring Boot check this post.
Deploy the MicroService to Docker Container
Since microservice is meant to be fault resistant of the other components. i.e, it must function independently. If microservice has to function independently, then it must be deployed and running independently in an isolated environment. Where to deploy the Microservice, there are a virtualisation technologies like Docker container, which has made deploying microservices simple. We create mutliple copies of the same service running in different docker containers. If one fails, other continue to serve the requests.
Let’s understand the above application diagram, We have three microservices,
- Profile Microservice – Which manages the user profile.
- Feeds Microservice – Which loads the Feeds for the user.
- Search Microservice – Search option on portal to search users/companies etc. Just like Facebook/LinkedIn search.
Currently due to outage on the application side, two microservices are down, only search service works. Wait, but we have three ❌ here, right ?. i.e. all three services should be down , Isn’t it ?. Here, the docker image of same search service saved the user experience.
How Docker improved Microservice Reliability
Okay, we had two copies of the search service running. Currently one is down, other is serving the requests of the user. Therefore, user would not have all things down experience. Search microservice will continue to run and the self healing system at failed microservice will bring the other services up in the meantime.
A next curious question, could be. How did we configure the microservice to run two, three or even more copies of its own. Here, Virtualization technology like Docker container helps. Docker container is a self contained light weight Virtual machine. Here, self contained means it has the required libraries already installed. or we can even install our microservice to it and distribute the docker image to the other teams within or outside the organisation. How to deploy the service to docker and create multiple copies.
How to deploy Spring Boot Microservice to Docker Container
- Create jar of the springboot application. Simple by gradlew clean install or mvn clean install , according to the build tool you use for writing the application. You may want to check running the spring boot application in Gradle blog post.
- Create Dockerfile file like below. Dockerfile is a simple text file with all the instructions required to build the image.
FROM java:8
EXPOSE 8081
ADD /target/search-service.jar search-service.jar
ENTRYPOINT ["java", "-jar", "search-service.jar"]
- FROM java:8 -> application_type: which version of application, here, it is a java application. It will pull the java 8 from the docker server and add it to the local docker image.
- EXPOSE 8081 -> port 8081 would be exposed to the outside world to access the springboot service on this port.
- ADD /target/searchservice.jar searchservice.jar -> docker will copy the jar from the target folder of the application and keeps one copy of its own.
- ENTRYPOINT [“java”, “-jar”, “search-service.jar”] -> ENTRYPOINT command runs the jar during the bootup process of the docker container.
3. Create the docker image.
docker build --tag=search-service:version1 .
-t flag tags the docker image. this simplify the searching of the image , when we have many docker images running on a host.
The .
at the end of the docker build
command ask the docker to look for Dockerfile in the current directory.
4. Now, lets run the docker image or our search microservice.
docker run -p8081:8081 earch-service:version1
5. Access the springBoot application on the url http://localhost:8081/.
6. And to run the copy of the search-service microservice. Just change the port in the DockerFile EXPOSE 8082. And follow the steps from step 3 to step 5.
Voila…we deployed the two copies of the same microservice. For further blog series on the SpringBoot Microserives, please do check our blog in few days.