Book Image

Spring: Microservices with Spring Boot

By : In28Minutes Official
Book Image

Spring: Microservices with Spring Boot

By: In28Minutes Official

Overview of this book

Microservices helps in decomposing applications into small services and move away from a single monolithic artifact. It helps in building systems that are scalable, flexible, and high resilient. Spring Boot helps in building REST-oriented, production-grade microservices. This book is a quick learning guide on how to build, monitor, and deploy microservices with Spring Boot. You'll be first familiarized with Spring Boot before delving into building microservices. You will learn how to document your microservice with the help of Spring REST docs and Swagger documentation. You will then learn how to secure your microservice with Spring Security and OAuth2. You will deploy your app using a self-contained HTTP server and also learn to monitor a microservice with the help of Spring Boot actuator. This book is ideal for Java developers who knows the basics of Spring programming and want to build microservices with Spring Boot. This book is embedded with useful assessments that will help you revise the concepts you have learned in this book. This book is repurposed for this specific learning experience from material from Packt's Mastering Spring 5.0 by Ranga Rao Karanam.
Table of Contents (7 chapters)

What is Spring Boot?


First of all, let's start with clearing out a few misconceptions about Spring Boot:

  • Spring Boot is not a code generation framework. It does not generate any code.

  • Spring Boot is neither an application server, nor is it a web server. It provides good integration with different ranges of applications and web servers.

  • Spring Boot does not implement any specific frameworks or specifications.

These questions still remain:

  • What is Spring Boot?

  • Why has it become so popular in the last couple of years?

To answer these questions, let's build a quick example. Let's consider an example application that you want to quickly prototype.

Building a Quick Prototype for a Microservice

Let's say we want to build a microservice with Spring MVC and use JPA (with Hibernate as the implementation) to connect to the database.

Let's consider the steps in setting up such an application:

  1. Decide which versions of Spring MVC, JPA and Hibernate to use.

  2. Set up a Spring context to wire all the different layers together.

  3. Set up a web layer with Spring MVC (including Spring MVC configuration):

    • Configure beans for DispatcherServlet, handler, resolvers, view resolvers, and so on

  4. Set up Hibernate in the data layer:

    • Configure beans for SessionFactory, data source, and so on

  5. Decide and implement how to store your application configuration, which varies between different environments.

  6. Decide how you would want to do your unit testing.

  7. Decide and implement your transaction management strategy.

  8. Decide and implement how to implement security.

  9. Set up your logging framework.

  10. Decide and implement how you want to monitor your application in production.

  11. Decide and implement a metrics management system to provide statistics about the application.

  12. Decide and implement how to deploy your application to a web or application server.

At least a few of the steps mentioned have to be completed before we can start with building our business logic. And this might take a few weeks at the least.

When we build microservices, we would want to make a quick start. All the preceding steps will not make it easy to develop a microservice. And that's the problem Spring Boot aims to solve.

The following quote is an extract from the Spring Boot website (http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-documentation):

Spring Boot makes it easy to create stand-alone, production-grade Spring based applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration

Spring Boot enables developers to focus on the business logic behind their microservice. It aims to take care of all the nitty-gritty technical details involved in developing microservices.

Primary Goals

The primary goals of Spring Boot are as follows:

  • Enable quickly getting off the ground with Spring-based projects.

  • Be opinionated. Make default assumptions based on common usage. Provide configuration options to handle deviations from defaults.

  • Provide a wide range of nonfunctional features out of the box.

  • Do not use code generation and avoid using a lot of XML configuration.

Nonfunctional Features

A few of the nonfunctional features provided by Spring Boot are as follows:

  • Default handling of versioning and configuration of a wide range of frameworks, servers, and specifications

  • Default options for application security

  • Default application metrics with possibilities to extend

  • Basic application monitoring using health checks

  • Multiple options for externalized configuration