Showing posts with label Netflix. Show all posts
Showing posts with label Netflix. Show all posts

Thursday, August 29, 2019

MicroServices: Spring cloud ribbon with Discovery Server

In this article, I am going to share my knowledge on Spring Cloud Ribbon and how can we use Ribbon with RestTemplate as well as with Feign Client. We will also see how Enabling discovery Sever will improve the scalability of Microservice.

Before jumping into Spring Cloud, I am assuming you must be having knowledge of Eureka Server, Feign Client, and Client-Side load balancer. If not then read my below blog before jumping to Spring Cloud ribbon. Also, I am going to use my existing code to implement Ribbon.

URLs:
In my previous blog, I have already talked about the Eureka Server and how other applications are taking advantages of Eureka Server to fetch the host/port of client application.

We have seen that three microservices application are up and running i.e.
  • Discovery Server
  • Product Service
  • Price Service
Where Product and Price service will register themselves to Discovery Server and Product-service will always communicate to Discovery Server to get the exact location of Price-service and then only it will talk to the price-service application.

Imagine there is a high load on the price-service application and to handle it, we have started two more instances of price-service.

  • How will you make sure your product-service should talk to all three instances of price-service and divides the load equally to each server?
  • How will you manage the heartbeat of the application so that product-service should not hit INACTIVE instance of price-service which just got shut down because of some internal reason?
  • How will you get to know how many instances are up and running of price-service?
For all the question, there is only answer which is Netflix Cloud Ribbon. It's a Spring cloud library which primarily provides client-side load balancing algorithms.

let's implement it and see how it can solve our problem.

Add below dependency to product-server pom.xml file as it is the one which is going to consume price-service API.

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>


Case 1: Ribbon + Eureka Server + RestTemplate

As our application is already acting as Eureka Client and using RestTemplate to fetch price record. Let's just add @LoadBalanced annotation to RestTemplate to enables Ribbon functionality.

It will allow the product-service application to use price-service as the address of price-service application and will discover the host/port of all instances of price-service from discovery-server.



Note:

@EnableCircuitBreaker <-> Used to enable Netflix Hystrix
@EnableHystrixDashboard <-> Used to check circuit state on Dashboard.
@EnableFeignClients <-> To enable feign client (Needed for case 2 scenario)

Start discovery-server, product-service and start two instances of price-service. You can do it easily by just overriding port number under Eclipse Run As Configuration.



Let's confirm it, whether all the instances are up and running or not by hitting discovery-server URL (http://localhost:8761/)



Now hit product-service URL multiple times from the browser and then go and check both the price-service logs. you will observe that few requests are coming to one instance and others on second one.



Great! Netflix Cloud Ribbon is successfully implemented and working absolutely fine.

If you start another price-service instance and hit the product-service URL again then you will find the request logs in the third instance too without doing any modification/configuration to any files.


Case 2: Ribbon + Eureka Server + Feign Client

If you are not aware of the feign client then you can read my blog here.

I'll just talk about Ribbon Integration with existing FeignClient application assuming you are already aware of Feign Client and implemented it.

In product-service application, I have already exposed another Endpoint (http://localhost:7001/products/feign/1) which consume price-service API using Feign Client.

To enable Netflix Ribbon, Add @RibbonClient annotation to the feighClient interface and pass your consuming service name (price-service).



Now restart your application and hit new endpoint. you will observe that the requests are distributed to all the instances of price-service.

Case 3: Ribbon + (RestTemaple/FeignClient) + NO Eureka Server


Can we use Netflix ribbon without integrating Eureka Server, The answer is YES but it would not be a good design. So, I would not recommend it.

When your application is not integrated with Eureka Server, in that case you have to list down all the address manually to properties file.

Remove @RibbonClients annotation.
Add below entry to your application.properties file under 
product-service.

#Enable this property if you are not using Eureka Server
price-service.ribbon.listOfServers=http://localhost:8002,http://localhost:7002

Imagine there are 1000 instances of price-services are up and running so we have to add all the instances manually. It could be a nightmare if we have to do it. :)

That's all for Netflix Ribbon now and do let me know if you have any confusion/query or you think I am not right somewhere. Please feel free to comment. Thank you!

As usual, you can download the spring-boot-microservices from GITHUB.

Sunday, June 16, 2019

Netflix Hystrix Circuit Breaker

Netflix Hystrix Circuit Breaker


In one of my previous blogs, I have already discussed the Circuit breaker pattern and its usage, Today, we will see how can we implement it in our application using Spring Cloud Netflix Hystrix.

In this document, I’ll walk you through the process of applying circuit breakers to potentially-failing method calls using the Netflix Hystrix fault tolerance library.

Hystrix is watching methods for failing calls to related services. If there is such a failure, it will open the circuit and forward the call to a fallback method.

To understand it in a better way, I’ll take the same problem statement that I have already discussed in my previous blog i.e. E-commerce Portal. 



I am assuming you must be aware of Spring boot framework as this implementation is completely based on it.

As per problem statement, we need two applications i.e. Product Service & Price Service but in this tutorial, I’ll talk about just Product Service as Price Service is simple web application exposing a single API and you can clone it from GitHub.

I’ll talk about Product Service in which we will integrate Circuit breaker pattern and Cache Service implementation and internally, it will invoke Price Service to get the Price Detail.

Optional: You can download the below project from GitHub or you can try it by creating a new one. I would recommend you to try it from scratch.

Product Service:

Create Spring boot application and add below dependency to your pom.xml file.


<!-- Optional. Application health monitor check -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<!-- Compulsory for circuit breaker implementation -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

</dependency>

<!-- Optional. Dashboard in case you want to see the circuit state UI -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>

</dependency>


To enable Circuit Breaker in spring boot application, add @EnableCircuitBreaker annotation on product-service entry-point class. 



Now use @HystrixCommand annotation on themethod we want to apply timeout and fallback method.

I have annotated @HystrixCommand(fallbackMethod = “getProductDetailFromCache”) on getProduct(String Id) so that if it doesn’t receive the response within the certain time limit or request get failed while calling price service API then fallback() should get called over here and fetch the price value from Cache Service. 

Make sure, the fallback method should be defined in the same class and should have the same signature.


We can customize the @HystrixCommand default behavior by configuring properties using @HystrixProperty annotations. Will check in depth in another blog. 

Add RestController class and other required class as per source code present in GitHub.

Add server.port to the application.properties file.

Start both applications and try to fetch the product Detail. In our case, our product-service app URL is:

http://localhost:7001/products/1



Now try to fetch product which does not exist


You will observe that over here circuit breaker is still in the closed state even after the exception. 

Why? The reason is that we have added  ignoreExceptions = { ProductNotFoundException.class } to @HystrixCommand so that it should not trip circuit if the product is not present.

Now, Stop the price-service application and hit the product URL again:


Check the log:


Congratulations! Our application is working as expected.

Monitoring Circuit Breakers using Hystrix Dashboard

Hystrix comes with a decent dashboard where we can monitor the status of Hystrix commands

To enable it, Add Hystrix dashboard dependency to the pom.xml file 


<!-- Optional. Application health monitor check -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Optional. Dashboard in case you want to see the circuit state UI -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>


Add @EnableHystrixDashboard annotation on the entry point class

Add management.endpoints.web.exposure.include=hystrix.stream to application.properties file.



Start the product-service application and then go to http://localhost:port/hystrix to view the dashboard. 



In Hystrix Dashboard home page enter http://localhost:7001/actuator/hystrix.stream as stream URL and give Product Service as Title and click on Monitor Stream button. 




Now, we can see that the Circuit status along with how many calls succeed and how many failures occurred, etc. 


Spring-boot microservices can be downloaded from GITHUB

Happy Coding...!!!



How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...