Microservices with Spring Boot and Java - Part 1 - Getting Started


NEW COURSE RELEASED! - 21 HOURS, 200+ VIDEOS - Master DevOps with Docker, Kubernetes and Azure DevOps


This guide will help you learn the basics of microservices and microservices architectures. We will also start looking at a basic implementation of a microservice with Spring Boot.

We will create a couple of microservices and get them to talk to each other using Eureka Naming Server and Ribbon for Client Side Load Balancing.

This is a 5 Part Article Series

Microservices with Spring Boot

In part 1 of this series, lets get introduced to the concept of microservices and understand how to create great microservices with Spring Boot and Spring Cloud.

You will learn

  • What is a Monolith?
  • What is a Microservice?
  • What are the Challenges with Microservices?
  • How does Spring Boot and Spring Cloud make developing Microservices easy?
  • How to implement client side load balancing with Ribbon?
  • How to implement a Naming Server (Eureka Naming Server)?
  • How to connect the microservices with Naming Server and Ribbon?

Free Courses - Learn in 10 Steps

Microservices Overview - A Big Picture

In this series of articles, we would create two microservices:

  • Forex Service - Abbreviated as FS
  • Currency Conversion Service - Abbreviated as CCS

Do not worry if you are not clear about a few things. The idea is to give a big picture before we get our hands dirty and create the microservices step by step

Forex Service

Forex Service (FS) is the Service Provider. It provides currency exchange values for various currency. Let’s assume that it talks to a Forex Exchange and provides the current conversion value between currencies.

An example request and response is shown below:

GET to http://localhost:8000/currency-exchange/from/EUR/to/INR

{
  id: 10002,
  from: "EUR",
  to: "INR",
  conversionMultiple: 75,
  port: 8000,
}

The request above is the currency exchange value for EUR to INR. In the response, conversionMultiple is 75.

We will talk about port in the response a little later.

Currency Conversion Service

Currency Conversion Service (CCS) can convert a bucket of currencies into another currency. It uses the Forex Service to get current currency exchange values. CCS is the Service Consumer.

An example request and response is shown below:

GET to http://localhost:8100/currency-converter/from/EUR/to/INR/quantity/10000

{
  id: 10002,
  from: "EUR",
  to: "INR",
  conversionMultiple: 75,
  quantity: 10000,
  totalCalculatedAmount: 750000,
  port: 8000,
}

The request above is to find the value of 10000 EUR in INR. The totalCalculatedAmount is 750000 INR.

The diagram below shows the communication between CCS and FS.

Image

Eureka Naming Server and Ribbon

Based on the load, we can have multiple instances of the Currency Conversion Service and the Forex Service running. Image Image

And the number of instances for each service might vary with time. Below picture shows a specific instance where there are 5 instances of the Forex Service. Image

What needs to happen in the above situation is load should be uniformly distributed among these 5 instances. Image

In this series of articles, we will use Ribbon for Load Balancing and Eureka Naming server for registering all microservices. Image

Do not worry if you are not clear about a few things. The idea is to give a big picture before we get our hands dirty and create the microservices step by step

What is a Monolith Application?

Have you ever worked in a project

  • Which is released (taken to production) once every few months
  • Which has a wide range of features and functionality
  • Which has a team of more than 50 working for it
  • Where debugging problems is a big challenge
  • Where bringing in new technology and new process is almost impossible

These are typical characteristics of a Monolith applications.

Monolith applications are typically huge - more 100,000 line of code. In some instances even more than million lines of code.

Monoliths are characterized by

  • Large Application Size
  • Long Release Cycles
  • Large Teams

Typical Challenges include

  • Scalability Challenges
  • New Technology Adoption
  • New Processes - Agile?
  • Difficult to Automation Test
  • Difficult ot Adapt to Modern Development Practices
  • Adapting to Device Explosion

Microservices

Microservice Architectures evolved as a solution to the scalability and innovotation challenges with Monolith architectures.

There are a number of definitions proposed for Microservices

Small autonomous services that work together - Sam Newman

Developing a single application as a suite of small services each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around business capabilities and independently deployable by fully automated deployment machinery. There is a bare minimum of centralized management of these services, which may be written in different programming languages and use different data storage technologies - James Lewis and Martin Fowler

While there is no single accepted definition for microservices, for me, there are a few important characteristics:

  • REST - Built around RESTful Resources. Communication can be HTTP or event based.
  • Small Well Chosen Deployable Units - Bounded Contexts
  • Cloud Enabled - Dynamic Scaling

How does Microservice Architecture look like?

This is how a monolith would look like. One application for everything.

This is how the same application would look like when developed using Microservices Architecture.

Microservice Architectures involve a number of small, well designed, components interacting with messages.

Advantages of Microservices

Advantages

  • New Technology & Process Adaption becomes easier. You can try new technologies with the newer microservices that we create.
  • Faster Release Cycles
  • Scaling with Cloud

Challenges with Microservice Architectures

While developing a number of smaller components might look easy, there are a number of inherent complexities that are associated with microservices architectures.

Lets look at some of the challenges:

  • Quick Setup needed : You cannot spend a month setting up each microservice. You should be able to create microservices quickly.
  • Automation : Because there are a number of smaller components instead of a monolith, you need to automate everything - Builds, Deployment, Monitoring etc.
  • Visibility : You now have a number of smaller components to deploy and maintain. Maybe 100 or maybe 1000 components. You should be able to monitor and identify problems automatically. You need great visibility around all the components.
  • Bounded Context : Deciding the boundaries of a microservice is not an easy task. Bounded Contexts from Domain Driven Design is a good starting point. Your understanding of the domain evolves over a period of time. You need to ensure that the microservice boundaries evolve.
  • Configuration Management : You need to maintain configurations for hundreds of components across environments. You would need a Configuration Management solution
  • Dynamic Scale Up and Scale Down : The advantages of microservices will only be realized if your applications can scaled up and down easily in the cloud.
  • Pack of Cards : If a microservice at the bottom of the call chain fails, it can have knock on effects on all other microservices. Microservices should be fault tolerant by Design.
  • Debugging : When there is a problem that needs investigation, you might need to look into multiple services across different components. Centralized Logging and Dashboards are essential to make it easy to debug problems.
  • Consistency : You cannot have a wide range of tools solving the same problem. While it is important to foster innovation, it is also important to have some decentralized governance around the languages, platforms, technology and tools used for implementing/deploying/monitoring microservices.

85,000 subscribers are learning AWS, Docker, Kubernetes, Spring Boot and Microservices on our Youtube Channel.

  SUBSCRIBE and Start Learning Now!


Solutions to Challenges with Microservice Architectures

Spring Boot

Enable building production ready applications quickly

Provide non-functional features

  • embedded servers (easy deployment with containers)
  • metrics (monitoring)
  • health checks (monitoring)
  • externalized configuration

Spring Cloud

Spring Cloud provides solutions to cloud enable your microservices. It leverages and builds on top of some of the Cloud solutions opensourced by Netflix (Netflix OSS).

Important Spring Cloud Modules

Dynamic Scale Up and Down. Using a combination of

  • Naming Server (Eureka)
  • Ribbon (Client Side Load Balancing)
  • Feign (Easier REST Clients)

Visibility and Monitoring with

  • Zipkin Distributed Tracing
  • Netflix API Gateway

Configuration Management with

  • Spring Cloud Config Server

Fault Tolerance with

  • Hystrix

Microservice Series of Articles

In this series of articles, we would create two microservices:

  • Forex Service - Abbreviated as FS
  • Currency Conversion Service - Abbreviated as CCS

The diagram below shows the communication between CCS and FS. We would establish communication between these two components.

Image

We would want to be able to dynamically scale up and scale down the number of instances of each of these services.

Image Image

And the number of instances for each service might vary with time. Below picture shows a specific instance where there are 5 instances of the Forex Service. Image

Implementing a solution for dynamic scale up and down needs to answer two questions

  • How does the Currency Conversion Service (CCS) know how many instances of Forex Service (FS) are active?
  • How does the Currency Conversion Service (CCS) distribute the load between the active instances.

Because we want this to be dynamic, we cannot hardcode the urls of FS in CCS. Thats why we bring in a Naming Server.

Image

All instances of the components (CCS and FS) register with the Eureka Naming Server. When FS needs to call the CCS, it will ask Eureka Naming Server for the active instances. We will use Ribbon to do Client Side Load Balancing between the different instances of FS.

A high level sequence diagram of what would happen when there is a request from CCS to FS is shown below: Image

Next Steps

Continue Learning Microservices with Spring Boot

What to Learn Next?

NEW COURSE RELEASED! - 21 HOURS, 200+ VIDEOS - Master DevOps with Docker, Kubernetes and Azure DevOps


350,000 Learners are learning everyday with our Best Selling Courses : Spring Boot Microservices, Spring, Spring Boot, Web Services, Hibernate, Full Stack React, Full Stack Angular, Python, Spring Interview Guide, Java Interview, Java Functional Programming, AWS, Docker, Kubernetes, PCF, AWS Fargate and Azure


85,000 Subscribers are learning from our Free Videos on YouTube : JSP Servlets, Spring, Spring Boot, Spring MVC, Hibernate, Eclipse, Maven, JUnit, Mockito, Full Stack - React, Full Stack - Angular, Docker, Kubernetes, AWS, AWS Fargate, PCF and Azure


Here are the recommend articles to read next : Spring Interview Questions, Spring Boot Interview Questions, Microservices, Hibernate, Spring Security, REST API with Spring Boot, Full Stack with React, SOAP Web Services, Exception Handling, Embedded Servers, Spring Data Rest, Spring vs Spring MVC vs Spring Boot, Building Web Application and Spring Data JPA. You can checkout all our 100+ articles here - All Articles.


Do not know where to start your learning journey? Check out our amazing learning paths: Learning Path 01 - Spring and Spring Boot Web Applications and API Developer, Learning Path 02 - Full Stack Developer with Spring Boot, React & Angular, Learning Path 03 - Cloud Microservices Developer with Docker and Kubernetes, Learning Path 04 - Learn Cloud with Spring Boot, AWS, Azure and PCF and Learning Path 05 - Learn AWS with Microservices, Docker and Kubernetes


Related Posts

Devops Tutorial | DevOps with Docker, Kubernetes and Azure DevOps

What is DevOps? How is it different from Agile? What are the popular DevOps Tools? What is the role of Docker, Kubernetes and Azure DevOps in DevOps. Let's get started with a simple usecase.

Deploy Java Spring Boot Applications to AWS, Azure, GCP with Docker and Kubernetes

In this article, we focus our attention on the cloud. How to learn the cloud and deploy Java Spring Boot Applications to AWS, Azure, GCP with Docker and Kubernetes?

Microservices Architectures - Non Functional Requirements - Performance

In this article, we explore an important non functional requirement called performance.

Spring Boot Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. This resources will help you learn and gain expertise at Spring Boot.

20+ Spring Boot Projects with Code Examples

At in28Minutes, we have created more than 20 projects with code examples on Github. We have 50+ articles explaining these projects. These code examples will you learn and gain expertise at Spring Boot.

REST API Best Practices - With Design Examples from Java and Spring Web Services

Designing Great REST API is important to have great microservices. How do you design your REST API? What are the best practices?

Index - 500+ Videos

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. Here's a list of video tutorials and courses for you

Creating Spring Boot and React Java Full Stack Application with Maven

This guide helps you create a Java full stack application with all the CRUD (Create, Read, Update and Delete) features using React as Frontend framework and Spring Boot as the backend REST API. We use Maven as the build tool.

Creating a SOAP Web Service with Spring Boot Starter Web Services

Let's learn how to create a SOAP Web Service with Spring Boot Starter Web Services. We will take a Contract First approach by definining an XSD and exposing a WSDL from it.