Spring Boot Microservices Tutorial – Best Resources To Learn in 1 Day | CHECK OUT
Last updated on 10th Jul 2020, Blog, Tutorials
Microservice Architectures evolved as a solution to the scalability and innovotation challenges with Monolith architectures.
There are a number of definitions proposed for Microservices
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
Why is there a need for Spring Boot?
Spring Boot enables building production-ready applications quickly and provides non-functional features:
- Embedded servers which are easy to deploy with the containers
- It helps in monitoring the multiples components
- It helps in configuring the components externally
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.
- 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. Microservice architecture is more complex than the legacy system. The microservice environment becomes more complicated because the team has to manage and support many moving parts. Here are some of the top challenges that an organization face in their microservices journey:
- Bounded Context
- Dynamic Scale up and Scale Down
- Fault Tolerance
- Cyclic dependencies
- DevOps Culture
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.
Other challenges of microservices
- As we add more microservices, we have to be sure they can scale together. More granularity means more moving parts, which increase complexity.
- The traditional logging is ineffective because microservices are stateless, distributed, and independent. The logging must be able to correlate events across several platforms.
- When more services interact with each other, the possibility of failure also increases.
Solutions to Challenges with Microservice Architectures
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 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
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.
We would want to be able to dynamically scale up and scale down the number of instances of each of these services.
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.
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.
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:
Monitoring is the control system of the microservices. As the microservices are more complex and harder to understand its performance and troubleshoot the problems. Given the vivid changes to software delivery, it is required to monitor the service. There are five principles of monitoring microservices, as follows:
Get Spring Boot Microservices Course with In-Depth Industry Topics By Expert Trainers
- Instructor-led Sessions
- Real-life Case Studies
- Monitor container and what’s inside them.
- Alert on service performance.
- Monitor services that are elastic and multi-location.
- Monitor APIs.
- Monitor the organizational structure.
These principles allow us to address technological changes associated with the microservices and organizational changes related to them.
Microservices Monitoring Tool
There are three monitoring tools are as follows:
- Hystrix dashboard
- Eureka admin dashboard
- Spring boot admin dashboard
Microservices virtualization is the method to simulate the behavior of specific components in various component-based applications like cloud-based application, SOA, and API driven architecture. Service virtualization also reduces cost and save time. By combining service virtualization, an organization can develop the application which can be delivered from various locations and dissimilar environments.
Components of Microservices
There are the following components of microservices:
- Spring Cloud Config Server
- Netflix Eureka Naming Server
- Hystrix Server
- Netflix ZuulAPI Gateway Server
- Netflix Ribbon
- Zipkin Distributed Tracing Server
Spring Cloud Config Server
Spring Cloud Config Server provides the HTTP resource-based API for external configuration in the distributed system. We can enable the Spring Cloud Config Server by using the annotation @EnableConfigServer.
Netflix Eureka Naming Server
Netflix Eureka Server is a discovery server. It provides the REST interface to the outside for communicating with it. A microservice after coming up, register itself as a discovery client. The Eureka server also has another software module called Eureka Client. Eureka client interacts with the Eureka server for service discovery. The Eureka client also balances the client requests.
Hystrix server acts as a fault-tolerance robust system. It is used to avoid complete failure of an application. It does this by using the Circuit Breaker mechanism. If the application is running without any issue, the circuit remains closed. If there is an error encountered in the application, the Hystrix Server opens the circuit. The Hystrix server stops the further request to calling service. It provides a highly robust system.
Netflix Zuul API Gateway Server
Netflix Zuul Server is a gateway server from where all the client request has passed through. It acts as a unified interface to a client. It also has an inbuilt load balancer to load the balance of all incoming request from the client.
Netflix Ribbon is the client-side Inter-Process Communication (IPC) library. It provides the client-side balancing algorithm. It uses a Round Robin Load Balancing:
- Load balancing
- Fault tolerance
- Multiple protocols(HTTP, TCP, UDP)
- Caching and Batching
Zipkin Distributed Server
Zipkin is an open-source project m project. That provides a mechanism for sending, receiving, and visualization traces.
Setting up Spring Cloud Config Server
Step 1: Create a Maven project using Spring Initializr https://start.spring.io/
Step 2: Choose the Spring Boot version 2.2.0 M6 or higher version. Do not choose the snapshot version.
Step 3: Provide the Group name. In our case, com.javatpoint.microservices.
Step 4: Provide the Artifact id. We have provided spring-cloud-config-server.
Step 5: Add the Spring Boot DevTools and Config Server dependencies.
Step 6: Click on Generate the project button. A zip file will download, extract it in the hard disk.
Step 7: Now, open the eclipse. Import the downloaded maven project. It will download the required files.
In the next step, we will create a simple Git repository and configure the spring cloud config server to pick up the values from the particular Git repository. We need to install the local Git.
Advantages of Microservices
- Microservices are self-contained, independent deployment module.
- The cost of scaling is comparatively less than the monolithic architecture.
- Microservices are independently manageable services. It can enable more and more services as the need arises. It minimizes the impact on existing service.
- It is possible to change or upgrade each service individually rather than upgrading in the entire application.
- Microservices allows us to develop an application which is organic (an application which latterly upgrades by adding more functions or modules) in nature.
- It enables event streaming technology to enable easy integration in comparison to heavyweight interposes communication.
- Microservices follows the single responsibility principle.
- The demanding service can be deployed on multiple servers to enhance performance.
- Less dependency and easy to test.
- Dynamic scaling.
- Faster release cycle.
Disadvantages of Microservices
- Microservices has all the associated complexities of the distributed system.
- There is a higher chance of failure during communication between different services.
- Difficult to manage a large number of services.
- The developer needs to solve the problem, such as network latency and load balancing.
- Complex testing over a distributed environment.
Building Java-based microservices is simplified by Spring Boot for the most use cases. Unlike frameworks like Dropwizard, it is easier to use and offers a richer feature set. Spring Boot offers a huge amount of additional libraries and integrations like Ribbon, Zuul, Hystrix, integrations with databases like MongoDB, Redis, GemFire, Elasticsearch, Cassandra or Hazelcast, just to name a few.
With Maven and Gradle powerful and widely supported Build Systems are supported for Java developers, which are more common and easier to integrate into existing structures than the dedicated build systems for frameworks like the Play Framework. In comparison to classical Web applications, Spring Boot is lean. For the majority of projects, adding a dependency to your project is good enough to get a good result from the beginning, without adaption the default config.
But not everything is perfect in the Spring Boot ecosystem. If you want to adapt the setup of a library, it is very likely that you have to adapt the setup of other libraries as well. One example for this was the integration of OAuth. The header information was not automatically detected by Swagger. In order to embed Hystrix, you just have to add two annotations and the dependencies and all your metrics are automatically detected. If for example, you change the URL of your health check, you have to change the configuration of Hystrix as well. Debugging such issues hidden in the underlying Spring magic can take time, but the advantages offered by Spring Boot are worth it.