The idea about this post is show some concept of Load Balancing & Circuit Breaker using Spring Cloud Netflix API.
Load Balancing automatically distributes incoming application traffic between number of nodes running for given application.
This provide client side load balancing. Its component offers a good set of configuration options such as connection timeouts, retries, retry algorithm etc.
Strategies offered by ribbon are listed below:
- Simple Round Robin LB
- Weighted Response Time LB
- Zone Aware Round Robin LB
- Random LB
Circuit Breaker pattern
Netflix’s Hystrix library provides an implementation of the Circuit Breaker pattern: when we apply a circuit breaker to a method, Hystrix watches for failing calls to that method, and if failures build up to a threshold, Hystrix opens the circuit so that subsequent calls automatically fail. While the circuit is open, Hystrix redirects calls to the method, and they’re passed on to our specified fallback method.
Spring provides Spring Cloud Fiegn API which takes care of the Load Balancing using Ribbon API and Fallback mechanism using Hysterix API.
Declarative REST Client: Feign
Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters used by default in Spring Web. Spring Cloud integrates Ribbon and Eureka to provide a load balanced http client when using Feign.
In other POST "Building a RESTful Web Service" we have developed "auth-service" service application having RESTful web service, there we have also register application to eureka-server.
Now we will develop "auth-client-service" application which implements Load Balancing and Circuit Breaker pattern for "auth-service" application.
In Microservice world client request will be never directly hit "auth-service" endpoints. It will be as below.
Client request -> API Gateway (Zuul) -> Auth-Client-Service (Ribbon & Hysterix) -> Auth-Service.