Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hystrix configuration for circuit breaker in Java

I am writing an application and I want to implement circuit breaker pattern. This is the Hystrix Command class I have written:

public class HystrixCommandNextGen extends HystrixCommand<ScriptContext> {

    private ScriptContext scriptctx;
    private ScriptFactory scriptFactory;
    private ScriptContext responseContext = null;

    private Logger logger = LoggerFactory.getLogger(HystrixCommandNextGen.class);

    public HystrixCommandNextGen(ScriptContext scriptctx, ScriptFactory scriptFactory) {
        super(
            Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Thread_Pool"))
            .andCommandKey(HystrixCommandKey.Factory.asKey(scriptctx.getExecutionData(ExecutionParam.SCRIPTNAME)))
        );
        this.scriptctx = scriptctx;
        this.scriptFactory = scriptFactory;

        HystrixCommandProperties.Setter().withCircuitBreakerEnabled(true);
        HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(150);
    }

    @Override
    protected ScriptContext run() throws Exception {
        scriptFactory.execute(scriptctx);
        return scriptctx;
    }

    @Override
    protected ScriptContext getFallback() {
        logger.error("FI is not responding: Error occurred in the execution of " + getClass().getSimpleName());
        return scriptctx;
    }
}

I am not able to understand how to configure the number of threads, threshold time for circuit breaker and number of requests to handle.

like image 318
Sumit Kumar Avatar asked Apr 14 '15 09:04

Sumit Kumar


People also ask

How do I configure hystrix properties?

First, we need to add the Spring Cloud Starter Hystrix dependency in our build configuration file. Now, add the @EnableHystrix annotation into your main Spring Boot application class file. The @EnableHystrix annotation is used to enable the Hystrix functionalities into your Spring Boot application.

Is Hystrix a circuit breaker?

It makes more sense for the service to back off and give calls to the callee service after some time or share default response. Netflix Hystrix, Resilince4j are two well-known circuit breakers which are used to handle such situations.

How do I configure hystrix dashboard?

To enable Hystrix dashboard, we only have to annotate our spring boot main class with @EnableHystrixDashboard . Following is the dependency information of Hystrix project. The Hystrix dashboard is avialable at http://localhost:9090/hystrix for client-service instance in our case.


1 Answers

Hystrix uses Archaius for configuration management. The Archaius library allows for dynamic reloading of property values at runtime. Documentation on how to configure Archaius is here: https://github.com/Netflix/archaius/wiki/Users-Guide

If you want to configure Hystrix in code, you can use the Archaius ConfigurationManager class like this:

ConfigurationManager.getConfigInstance().setProperty(
  "hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds", 
  500);

Note that the HystrixCommandKey part of the property name string is actually the name of the circuit breaker you set with the .andCommandKey() method of the Setter. So if you set the command key to be "MyCommand", the property key for timeout would actually be "hystrix.command.MyCommand.execution.isolation.thread.timeoutInMilliseconds"

like image 131
harperska Avatar answered Nov 14 '22 15:11

harperska