Customizing the default configuration

The Hystrix library allows you to customize the default configuration by using some properties for command and fallback. The command properties can be set using commandProperties of the @HystrixCommand annotation:

@HystrixCommand(commandProperties = { 
   @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "300") 
}) 
public Account findAccountById(String id) { 
   return accountService.findAccountById(id); 
}

We have customized the default timeout to 300 milliseconds. Similar to commandProperties, we can customize the thread pool properties by using the threadPoolProperties of @HystrixCommand:

@HystrixCommand(commandProperties = { 
         @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "300") 
   }, 
   threadPoolProperties = { 
         @HystrixProperty(name = "coreSize", value = "30"), 
         @HystrixProperty(name = "maxQueueSize", value = "101"), 
         @HystrixProperty(name = "keepAliveTimeMinutes", value = "2"), 
         @HystrixProperty(name = "queueSizeRejectionThreshold", value = 
"15"), @HystrixProperty(name = "metrics.rollingStats.numBuckets",
value = "12"), @HystrixProperty(name =
"metrics.rollingStats.timeInMilliseconds", value = "1200") }) public Account findAccountById(String id) { return accountService.findAccountById(id); }

We have set threadPoolProperties, such as coreSize, maxQueueSize, keepAliveTimeMinutes, and queueSizeRejectionThreshold. Sometimes we are required to set some common properties to all Hystrix commands. The Hystrix library also allows us to set default properties at the class level so that these can be applicable for all Hystrix commands.

Netflix's Hystrix provides the @DefaultProperties annotation. It is a class-level annotation that allows us to set default command properties, such as groupKey, threadPoolKey, commandProperties, threadPoolProperties, ignoreExceptions, and raiseHystrixExceptions.

By default, specified properties will be used for each command within an annotated class by using the @DefaultProperties annotation unless a command specifies those properties explicitly using the corresponding @HystrixCommand parameters. Let's see the following:

@DefaultProperties(groupKey = "DefaultGroupKey") 
class AccountService { 
    @HystrixCommand // hystrix command group key is 'DefaultGroupKey' 
    public Object commandInheritsDefaultProperties() { 
        return null; 
    } 
    @HystrixCommand(groupKey = "SpecificGroupKey") // command overrides 
default group key public Object commandOverridesGroupKey() { return null; } }

Let's enable the Hystrix Metrics Stream in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.60.158