Search code examples
javaload-balancingnetflix-eurekahystrixspring-cloud-netflix

How to implement Load Balancer in java


I wanted to implement a pattern which automatically or manually to stop all requests to an external service(i.e.port) for a period of time if error percentage passes a threshold. I have two server instances running on same machine with different ports(ex:2401, 2402).

Now the requirement is if port 2401 passes error percentage threshold, then I wanted to stop all the requests to this port(2401) for a period of time and route to another port(2402). Which algorithm will suits for this I'm not sure.

I read some articles, but no where got complete information about Load Balancer implementation in Java code.

Thanks in advance, Sateesh


Solution

  • @Svetlin is exactly right, you can achieve this with hystrix. Here is a sample code. Adapt it to your requirements.

        @HystrixCommand(fallbackMethod = "fallBackForProductDetail", groupKey = "CircuitBreaker", commandKey = "frontend-productdetail", threadPoolKey = "frontend-productdetail",
                commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"),//Time before which this call is supposed to complete. if not throw exception. this is Optional
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"), // Number of requests before which the cicuit is open
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "1200000"),//20 minutes circuit will be open
                },
                threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "30"),
                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "180000")// 3 minutes rolling window. i.e the errors calculated for every 3 minute window.
                })
        public String callProductDetail(.....){
             // call server1 
        }
    
          // Return type and arguments should be exactly same as the method for wich this is fallback. (With an optional Throwable argument to catch exception)
        public String fallBackForProductDetail(...){
            // call server2
        }
    

    Now to explain the behaviour. When request to server1 fails, the counter is incremented and the call goes to fallback method(fallBackForProductDetail) and executes code inside the fallback method. the same behaviour continues till threshold is reached(5 in this case). After threshold is reached, the control doesn't even go into main method(callProductDetail), it directly goes to fallback method. And this happens for sleepWindowInMilliseconds(20 minutes in this case).

    Hope it helps.