spring boot microservices

Spring Boot Microservices are extremely easy to build. As an extremely popular microservices framework, Spring Boot allows Rapid Application Development.

Using Spring Boot, you can build production-ready Java microservices in less time. With Spring Boot, even development teams with less experience in Java or Spring framework can start delivering business value.

This is exactly the benefit of a mature microservices framework. Basically, a mature framework takes away the pain of developers, allowing them to instead focus on writing business logic.

Features of Spring Boot

Spring Boot works on the same concepts as the widely popular Spring framework. It takes the Spring framework as a base thereby inheriting most of design principles behind Spring. However, it also provides features on top that allow developers to quickly build production-ready microservices.

Some of the key features of Spring Boot are as follows:

Autoconfiguration

Spring Boot auto-configures dependencies by simply looking at the classpath or the property files.

For instance, if we include JPA and a database such as MySQL in our maven or gradle dependencies, Spring Boot will automatically configure the datasource and make the bean available in the Spring context. In other words, no additional configuration is required.

Basically, Spring Boot uses sensible defaults based on the dependencies and the properties described by the developer.

This is an extremely powerful feature that allows developers to avoid writing and maintaining a bunch of boilerplate code.

Spring Boot Starter Modules

With Spring Boot, you can use starter modules for better dependency management. This prevents versioning hell where one version of a certain dependency was not compatible with another version.

In other words, with starter modules, dependencies for a particular version of Spring Boot are curated and bunched as part of a starter dependency module.

You also don’t need to write the versions for each dependency. This is because the version is derived from the parent starter.

Below is how the parent dependency looks in a maven project looks like.

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
</parent>

Spring Boot Initializer

Spring Boot highly advocates speed and simplicity. Bootstrapping a Spring Boot project is all about using the Spring Initializer.

With Spring Initializr you can pick and choose the dependencies required for your project. And then, you can download a pre-configured project that can be imported to your IDE of choice.

For more details, check out a detailed guide on the Spring Boot Initializr.

Building Spring Boot Microservices

Spring Boot Microservices for production often require a bunch of important aspects.

The below illustration shows some of the major ones.

spring boot microservices

Good news is that the framework still provides a lot of support in making your Spring Boot Microservices ready for production.

For example, a typical use case for a Spring Boot Microservice could be to expose RESTful APIs.

Below are the steps required to build such a microservice from scratch:

Bootstrapping

Microservices development should ideally start from a mature point where the dependencies are pre-configured. When developing Spring Boot Microservices at scale, you don’t want to struggle with finding the right dependencies or creating starter templates.

Spring Boot Initializr allows you to do exactly that. With its form-based input, it becomes extremely easy for developers to assemble a starter application that just works out-of-the-box.

The new Spring Initializr provides an even better user-experience minus some of the older features.

Spring Boot Application Startup

Dependency management and understanding how a Spring Boot application starts up is important to work with its advanced features.

Understanding the Structure of Spring Boot Application describes the major dependencies in our Project as well the process by which Spring Boot applications starts up.

Spring Boot Database Connectivity

Spring Boot can work with a large number of databases. Options range from typical SQL databases such as MySQL, Oracle as well as NoSQL options such as MongoDB.

However, for developing microservices rapidly, it could be an advantage to start with a Java in memory database such as H2 Database. Basically, the advantage of working with an in memory database such as H2 is to make it easier to tweak your table schema during development phase.

We start by doing Spring Boot H2 Database setup. This allows us to rapidly develop new features and test database schema changes without worrying about working with complex database servers.

Spring Boot – Inserting Records in Table

Any production-level application has to deal with data. In other words, managing data is one of the fundamental reasons for building applications.

As an example, inserting user-generated data into database tables is one of the most common use-cases. Also, many times we want to start up our applications with some initial data.

With Spring Boot, you can use the CommandLineRunner interface to insert dummy records to the database at application start-up.

We look at how to configure Spring Boot CommandLineRunner to insert records to H2 Database during application start-up.

Spring Boot REST Repositories

With Spring Data REST, you can expose JPA repositories as RESTful interfaces with little-to-no configuration. Just an entity class and a repository interface is enough to make the setup work.

In other words, you don’t have to write any logic to expose your data as REST interface complete with Hyper-media capabilities.

This is a very powerful feature to have if your data model closely resembles your resource model.

We look at configuring Spring Data REST repositories along with all the available customization options.

Usually, however, your data model is different to your resource model. In such cases, a more conventional approach for building RESTful web-services is required. However, Spring Boot provides support for that as well.

Spring Boot RESTful API

Creating RESTful APIs is one of the most common use-cases of building Spring Boot Microservices. As mentioned earlier, Spring Boot provides excellent support for building RESTful Web Services.

Creating REST API using Spring Boot describes how you can create RESTful APIs using Spring Boot. We also look at how to structure an application into controllers, services, DTOs and repositories.

REST API using Spring Boot – Insert and Update Data describes how you insert and update data using Spring Boot. We look at HTTP POST and PUT methods. Also, we create necessary controllers, services, DTOs and repositories for handling create and update operations.

Spring Boot Exception Handling

Exception Handling is an important aspect of any production-level application. In other words, exceptions are a part of programming and handling them properly is essential to providing a good user-experience.

The requirement is no-different for Spring Boot Microservices.

Spring Boot provides excellent support for exception handling. We look at Spring Boot Exception Handling along with the standard approach to customizing error responses based on the exceptions.

Spring Boot Bean Validations

Validating the data to check whether it is consistent according to your data rules is fundamental to designing robust applications. In fact, bad data can lead to numerous quality issues. It can also impact the proper functioning of other microservices.

Spring Boot provides out-of-the-box support for Java Bean Validation API. We look at handling validations in Spring Boot along with customizing the output messages.

Spring Boot Swagger 2 Integration

Swagger is one of the leading frameworks to document APIs. Documentation is an extremely important requirement to make your API easily accessible to a potential consumer. Swagger provides a standard way of doing so.

Swagger integrates seamlessly with Spring Boot. Using few annotations, you can create ready-to-use documentation for your APIs created using Spring Boot. Moreover, as you update your API, the Swagger documentation also updates automatically.

In Setting Up Swagger with Spring Boot, we look at how you can get Swagger up and running with your application.

In Advanced Swagger 2 Annotations, we look at additional features that Swagger provides. These features enhance the quality of the documentation.

Spring Boot Hibernate Envers Integration

In a typical production application, we often need to maintain an audit table or a history table. This could be important for compliance reasons as well as the ability to trace issues by looking into the history of records.

Hibernate Envers is a framework that allows us to maintain such a history without writing explicit code to do so.

In Spring Boot Hibernate Envers Integration, we look at how to enable Hibernate Envers in a typical Spring Boot application.

Spring Boot JPA Auditing

Apart from knowing about historic data, we also need to maintain audit log of our tables. Information such as last updated timestamp or the last updated user id can be extremely important in certain situations.

With the AuditorAware interface that comes along with JPA, it becomes quite easy to build such a setup.

In Spring Boot JPA Auditing, we look at how to enable auditing in Spring Boot applications.

Spring Boot Docker Deployment

While local environment is ideal for development purposes, we ultimately need to deploy our applications to a production environment. Docker containers is a popular approach to deploy Spring Boot Microservices to a production server.

In Spring Boot Docker Deployment, we work our way to create a Docker image for our Spring Boot Starter application. We also understand the basics of the Dockerfile. Finally, we run the Docker Image as a container.

Spring Boot Security

Security plays an extremely important role in any applications. It’s importance only increases in a microservices architecture where the attack surface becomes larger.

However, Spring Boot comes with in-built modules to take care of the security needs of your application.

In Spring Boot Basic Security, we look at the simplest approach to enable security in your Spring Boot Microservice.

Next, in Spring Boot Security using UserDetailsService, we integrate our Spring Security approach with a user and role database. This makes the security solution much more robust.

In Spring Boot Method-Level Security, we look at authorization and security at method-level to protect our resources based on user roles.

Spring Boot MongoDB Integration

Spring Boot can also very easily integrate with NoSQL Document Databases such as MongoDB.

The Spring Data family of projects contains the Spring Data MongoDB project that allows Spring Boot MongoDB integration with minimum configuration.

In Creating RESTful API using Spring Data MongoDB and Spring Boot, we look at this integration and develop an application from scratch.

Spring Boot RestTemplate

A typical microservices architecture requires API Communication. One microservices may call an API provided by another microservice in order to carry out some of its operations.

With Spring Boot RestTemplate, the task of communication between microservices through RESTful APIs becomes very easy.

Conclusion

Spring Boot is one of the best frameworks to deliver microservices. Spring Boot Microservices are easy to develop and lead to faster turnaround of business functionality. With Spring Boot Microservices, it is also extremely easy to implement key features required for a production-level microservice with relative ease.

In fact, as a framework, Spring Boot has come a long way. The popularity of Java and Spring in general also ensures that the framework is going to only mature even more over time.


Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

10 Comments

Vinay · May 23, 2020 at 7:34 pm

The article is very good!
It’s really curated content to understand end to end spring boot project setup and best practices.
really appreciate the effort!
Thanks.

    Saurabh Dashora · May 24, 2020 at 9:44 am

    Thanks for the kind words Vinay! Happy to help.

Tugbobo Godfrey · June 3, 2020 at 1:11 am

I love your articles. You have a clear style of writing and your articles are wonderfully curated. I honestly enjoyed them.
Thanks.

    Saurabh Dashora · June 3, 2020 at 2:14 am

    Hi Tugbobo,

    Thanks for your kind words. Really glad to know the articles helped you.

Shreyas · July 4, 2021 at 1:06 pm

Greatly articulated! Loved this article as much as the others. Could you please do one on Microservices in Python ecosystem?

    Saurabh Dashora · July 5, 2021 at 11:59 am

    Thanks for your kind feedback Shreyas!! Would definitely try to accommodate something on Python microservices.

      Arshad · September 3, 2021 at 11:51 pm

      One of the best article on Spring Boot ….thanks a lot Saurabh & God bless You !! Can you please make some post end to end spring boot Microservices project with communication with 3rd party Api and if possible Kafka from scrach to advance just like you did for Spring boot & Microservices.

Anonymous · September 17, 2021 at 3:46 pm

Hi Saurabh your article helped me to understand the swagger. Could you please post an article on kafka with spring boot microservices

    Saurabh Dashora · September 18, 2021 at 2:02 am

    Glad that the article could help you. Kafka with Spring Boot is definitely in the plan.

Leave a Reply

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