Microservice Example : Getting started with Spring Boot

Microservice Example : Getting started with Spring Boot

#005

Hello friends, hope you are following our Microservices Beginner to Expert series. Today’ I shall be discussing about Spring boot technology which is well proven implementation pattern of Microservice architectural style.

Spring Boot is an implementation framework which supports Microservice architectural style. This framework helps developers with lot of boilerplate code and configurations that can be avoided when focusing on specific application functionality only. This framework not only increases the development speed but also helps with ops features like health-checks, metric collection , api documentation

Spring Boot framework is built on top of Java technology and tomcat server. Hopefully, most of the developers are familiar with usual tools and development IDEs to use these technologies.

What do you need in your arsenal for getting start with ?

There are multiple options that you can develop Spring boot applications, following options can be one of your choice.:

  • Command Line tool
  • Spring Tool Suite (STS)
  • Other eclipse based tools
  • Spring Initializer http://start.spring.io

The most common tool and popular IDE is STS. Now, let’s dig into Spring Boot application development a bit .

The key components of a boot application are as following

  • xml
  • Application code in directory path src/main/java
  • Test code in src/test/java
  • properties or application.yaml in directory path src/main/resources for data externalization
  • Imbedded HTTP listener

Spring boot extensively uses maven. Let us examine the critical sections of pom.xml

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.0.RELEASE</version>

<relativePath/> <!– lookup parent from repository –>

</parent> 

The spring-boot-starter-parent is a Bill Of Materials (BOM) pattern and the advantage is that developers do not need to worry about finding the right compatible versions of different libraries like Spring , Jersey, JUnit, Jakson etc.

<dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter</artifactId>

</dependency>

You can get details of all starters from reference documentation : https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-starter

Download our first example from GitHub and explore for further understanding.

https://github.com/debajyoti1d1mukherjee/ctt-springboot/tree/master/bootstandalonedemo

This DemoApplication contains some special annotations:

@SpringBootApplication: This is a special annotation and is a combination of  @Configuration, @ComponentScan , @EnableAutoConfiguration.  @Configuration annotation hints that the class is a configuration class and it is also meta-annotated with @Component and therefore is a candidate for component scanning . @EnableAutoConfiguration does a bit of magic.,it scans the classpath and creates missing beans. For each spring-boot-starter-* dependency in pom.xml file, spring boot executes a default AutoConfiguration class. AutoConfiguration classes use the *AutoConfiguration lexical pattern  where * represents the library. For example the autoconfigurations for JPA is done through JpaRepositoriesAutoConfiguration.

@EnableSwagger2 : This enables swagger Rest api documentation.  The following maven dependencies are required for this.

<dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-swagger2</artifactId>

            <version>2.6.1</version>

            <scope>compile</scope>

</dependency>

 <dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-swagger-ui</artifactId>

            <version>2.6.1</version>

            <scope>compile</scope>

</dependency>

In our example the api documentation can be accessed at the url : http://localhost:8080/swagger-ui.html#/. Of course after running the app locally 🙂

SpringApplicaton.run(a class name ): This launches Spring ApplicationContext. When we run a spring application, we needs to provide where is application context. The boot class contain any extra configuration required , any bean definition. When we run the application of our first example, it just print a string in the console.

Nevertheless, you can run the Spring Application from STS IDE itself like a simple Java Application, the way you run Java program. Spring framework will do rest for you internally i.e. running a server and launching the app with local hosted URL 🙂 Bingo !

Providing few more examples that you may refer further for better understanding.

Second example:

https://github.com/debajyoti1d1mukherjee/ctt-springboot/tree/master/bootwebdemo

In this example we have used a rest endpoint which when invoked provides a string response.

The following dependency is added to pom.xml:

<dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

</dependency>

This dependency automatically adds all required libraries for spring rest , spring mvc in classpath.  When we run the application , and requests for http://localhost:8080/ctt a string response will be provided by the application. In this example the package type was jar , mentioned in pom.xml.

Third  example:

https://github.com/debajyoti1d1mukherjee/ctt-springboot/tree/ , master/bootwebdemoadvanced

Here we have changed the package type to war. The application class should extend “extends SpringBootServletInitializer” and override necessary methods. The rest of the things are similar. When we want to access the service, we need to provide the context root which was not needed in previous case.

Fourth  example:

https://github.com/debajyoti1d1mukherjee/ctt-springboot/tree/master/bootjpa

This example uses “spring-boot-starter-data-jpa” to store and retrieve data from HSQLDB. The rest of the things are already explained before.

So by now I am sure that you have understood that spring boot supports the following:

  • Easy dependency management.
  • Easy configuration management.
  • Accelerates application development.
  • Since each boot application implements a specific business requirement and has its own embedded server , it heavily supports microservice architectural style.

So that’s it. We will discuss about boot actuators  , profiling , containerization in our next blog. So please read the series and provide us your valuable feedback.

Note: If you are familiar with git , you can download by git clone or else you can download the code from the links by HTTP. You then import the downloaded code into your IDE. Right-click on the project and then run as spring boot application.

 

Author: Debajyoti Mukherjee

Debajyoti Mukherjee has more than 11 years if IT experience in application design , application development. He has worked in large MNCs and has worked for eminent clients. He is proficient in custom development , cloud platform , microservice based application migration.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.