Microservices in Java - Spring Cloud and Netflix Overview - Part 2
Posted by Žiko Petrović on 07 May 2020
In Part 1, we have talked about the microservices architecture, the Spring Cloud project, and two important components of the Spring Cloud (Eureka Service Discovery and Zuul Edge Server). Now we will continue our story about the microservices architecture and also introduce other components:
► Dynamic Routing and Load Balancer - Netflix Ribbon
► Circuit Breaker and Monitoring - Netflix Hystrix
► Central Configuration Server - Cloud Config Server
Dynamic Routing and Load Balancer - Netflix Ribbon
Server-side load balancing is involved in monolithic applications where there is a limited number of application instances behind the load balancer. In that case, the load balancer has a public IP and DNS and a client makes a request using the public IP/DNS. Server-side load balancing is mostly a manual effort and we need to add/remove instances manually to the load balancer to work. To overcome the problems of traditional load balancing, client-side load balancing came into the picture.
There are many microservices in the microservice architecture and each microservice may have multiple instances. A single instance of a service is unimaginable in production, so what we generally do in that case is to use a load balancer that balances the payload among multiple instances of a service. Netflix Ribbon from Spring Cloud family provides the ability to set up client-side load balancing along with the service registry component.
Image 1. https://i.stack.imgur.com/cNJuj.png
Apart from the client-side load balancing algorithms, Ribbon also provides other features:
► Service Discovery Integration
► Fault Tolerance
► Configurable load-balancing rules
When talking about load-balancing rules, Ribbon supports RoundRobinRule, AvailabilityFilteringRule, WeightedResponseTimeRule out of the box, together with defining custom rules. Ribbon API enables us to configure the following components of the load balancer:
► Rule – A logic component which specifies the load balancing rule we are using in our application
► Ping – A component which specifies the mechanism we use to determine the server’s real-time availability
► ServerList – Can be dynamic or static
Ribbon API can determine the server’s availability through the contact pinging of servers at regular intervals. When all servers are down thus no server is available to serve the request, the pingUrl() method will fail and we will receive an exception with a message “No instances are available to serve the request”.
Circuit Breaker and Monitoring - Netflix Hystrix
A typical system consists of many services working and collaborating. These services are prone to failure or delayed responses. If a service fails, it may impact other services affecting performance and possibly making other parts of the application inaccessible or in the worst case bring down the whole application.
Hystrix is a library designed to control the interactions between these services providing greater tolerance of latency and failure. Hystrix does this by isolating points of access between the services, stopping cascading failures across them and providing fallback options, all of which improve the system’s overall resiliency. It implements the circuit breakers pattern. Circuit breakers calculate when to open and close the circuit and what to do in case of failure. When all services fail at some time, the circuit breaker handles these failures gracefully. The circuit breakers have three states:
Nowadays, tens of billions of thread-isolated and hundreds of billions of semaphore-isolated calls are executed via Hystrix every day at Netflix and a dramatic improvement in uptime and resilience has been achieved through its use.
Central Configuration Server - Cloud Config Server
In the microservices architecture, there is usually a large number of small microservices and every microservice is usually staggered in more environments. Spring Cloud Config is Spring’s client/server approach for storing and serving distributed configurations across multiple applications and environments. With the Config Server, there is a central place to manage external properties for applications across all environments.
Image 2. Source: https://camo.githubusercontent.com
Spring Cloud Config is the combination of two important components:
► Spring Cloud Config Server – Provides support for maintaining centralized configuration on GIT repository
► Spring Cloud Config Client – Provides support for attaching application on Spring Cloud Config Server
This configuration store is ideally versioned under Git version control and can be modified at application runtime. While it fits very well in the Spring applications using all the supported configuration file formats together with constructs like Environment, PropertySource or @Value, it can be used in any environment running any programming language.
Microservices have many benefits for Agile and DevOps teams. Unlike microservices, a monolithic application is built as a single, autonomous unit and modification made to a small section of code might require building and deploying an entirely new version of the software. Microservices solve these challenges of monolithic systems by being as modular as possible. They are often connected via APIs and can leverage many of the same tools and solutions that have grown in the RESTful and web service ecosystem.
Software Developer at PanonIT