The idea about this post is show some concept of Load Balancing & Circuit Breaker using Spring Cloud Netflix API.
Load Balancing
Load Balancing automatically distributes incoming application traffic between number of nodes running for given application.
Ribbon :
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.
Code
Dependencies:
Config files:
As we are using spring-cloud config below will be bootstarp.yml in application.
All application related configuration will be defined in authclientservice.yml and stored at centralized location.
Java Code:
Below Rest Controller which will be accessed by client, once request comes it will redirect request to respective service application at respective node using ribbon. If in case its not able to communicate any of the code for given application fallback method will be called using Hysterix.
Code using Feign API
Define Service interface have end-point definition which is exactly same end-point defined in “auth-service” .
Define Feign Client, here you need to define service application name and fallback class.
Now define Fallback code for every end-point which is defined in service interface.
Start and execute below commands to see results.
Auth-Client-Service application is started at 7072 port.
Auth-Service application is running on 9092 and 9093.
1 – We will request rest endpoint defined in “auth-client-service” and see how request is send to “auth-service”, which sends back proper response.
curl -H “Content-type:application/json” -X POST http://localhost:7072/auth/login/authenticate -d ‘{“loginId”:”123″, “password”:”123″}’
curl -H “Content-type:application/json” -X POST http://localhost:7072/auth/login/authenticate -d ‘{“loginId”:”123″, “password”:”123″}’
Load Balancing:
We will execute below set of curl commands multiple times and check logs of “auth-service” running on both node.
curl -H “Content-type:application/json” -X POST http://localhost:7072/auth/login/authenticate -d ‘{“loginId”:”123″, “password”:”123″}’
We have executed above curl command 5 times. If you look into below screenshot you can see request is routed to both node in Round Robin manner (Feign uses Ribbon internally for load balancing).
2 – Now we will stop both nodes of “Auth-Service” and see how fallback method gets executed.
Eureka – Server showing all the nodes are down for Auth-Service.
Executed below curl command.
curl -H “Content-type:application/json” -X POST http://localhost:7072/auth/login/authenticate -d ‘{“loginId”:”123″, “password”:”123″}’
If you look into response its saying required service is unavailable please try after some time, this response is defined in fallback class which we have defined.
Now we will start auth-service application and send same curl request, you should be getting proper response defined in auth-service end-point.
Next we will take a look in Zuul Edge Server.
Cheers – Happy learning 🙂
Ketan Gote
Comments
Post a Comment