Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring cloud - how to get benefits of retry,load balancing and circuit breaker for distributed spring application

I want the following features in spring-cloud-Eureka backed microservices application.

1) Load balancing - if I have 3 nodes for one service, load balancing should happen between them

2)Retry logic - if one of the nodes did not respond, retry should happen for certain number ( eg 3. should be configurable) before falling back to another node.

3)circuit breaker - if for some reasons, all the 3 nodes of service is having some issue accessing db and throwing exceptions or not responding, the circuit should get open, fall back method called and circuit automatically closes after the services recovers.

Looking at many examples of Spring-cloud, I figured out

1) RestTemplate will help with option 1. but when RestTemplate access one instance of service and if the node fails, will it try with other two nodes?

2) Hystix will help with circuit breaker option (3 above). but if just one node is not responding, will it try other nodes, before opening up circuit and call fallback method. and will it automatically close circuit once the service recovers?

3) how to get retryLogic with spring-cloud? I do know about @Retryable annotation. But will it help in the following situation? Retry with one node for 3 times and after it fails, try the next node 3 times and the last node 3 times before circuit breaker kicks in.

I see that all these configurations are available in spring cloud. but having a hard-time understanding how to configure for all these for efficient solution.

Here is one proposed:

@HystrixCommand
@Retryable
public Object doSomething() {
   // use your RestTemplate here
}

But I don't totally know if it is going to help me with all the subtleties I mentioned above.

I do see there is a @FeignClient. But from this blog, I understand that it provides a high level feature for HTTP client requests. Does it help with retry and circuit breaker and load balancing all-in-one?

Thanks

like image 311
brain storm Avatar asked Aug 09 '15 05:08

brain storm


People also ask

Is Spring retry a circuit breaker?

Spring Retry provides a circuit breaker implementation via a combination of it's CircuitBreakerRetryPolicy and a stateful retry. All circuit breakers created using Spring Retry will be created using the CircuitBreakerRetryPolicy and a DefaultRetryState .

How is circuit breaker implemented in spring cloud?

Spring Cloud's Circuit Breaker library provides an implementation of the Circuit Breaker pattern: when we wrap a method call in a circuit breaker, Spring Cloud Circuit Breaker watches for failing calls to that method, and if failures build up to a threshold, Spring Cloud Circuit Breaker opens the circuit so that ...

How load balancing is implemented in spring cloud?

Load balancing is the process of distributing traffic among different instances of the same application. To create a fault-tolerant system, it's common to run multiple instances of each application. Thus, whenever one service needs to communicate with another, it needs to pick a particular instance to send its request.


1 Answers

I do see there is a @FeignClient. Does it help with retry and circuit breaker and load balancing all-in-one?

If you are using the full spring-cloud stack, it actually solves everything you mentioned.

The netflix components in this scenario are the following in spring-cloud:

Eureka - Service Registry

Let's you dyanmically register your services so you only need to fix one host in your app (eureka).

Ribbon - Load balancer

Out of the box it's providing you with round robin loadbalancing, but you can implement your own @RibbonClient (even for a specific service) and design your custom loadbalancing for example based on eureka metadata. The loadbalancing happens on the client side.

Feign - Http client

With @FeignClient you can rapidly develop clients for you other services (or services outside of your infrastructure). It is integrated with ribbon and eureka so you can refer to your services @FeignClient(yourServiceNameInEureka) and what you end up with is a client which loadbalances between the registered instances with your preferred logic. If you are using spring you can use the familiar @RequestMapping annotation to describe the endpoint you are using.

Hystrix - Circuit breaker

By default your feign clients will use hystrix, every request will be wrapped in a hystrix command. You can of course create hytrix commands by hand and configure them for your needs.

You have to configure a little to get thees working (actually just a few @Enable annotation on your configuration).

I highly recommend reading the provided spring documentation because it wraps up almost all of your aspects in a fairly quick read.

http://cloud.spring.io/spring-cloud-netflix/spring-cloud-netflix.html

like image 158
Ákos Ratku Avatar answered Nov 13 '22 19:11

Ákos Ratku