Hystrix has two resource isolation strategies: thread pool and semaphore. Speaking of resource isolation, what do we need to pay attention to in the field?

The background,

For Hystrix fuse isolation strategy are: thread pool and semaphore, the previous article has made a detail the micro service architecture | Hystrix resource isolation strategy how to choice?”

The specific policy needs to be evaluated based on service scenarios. In general, thread pool isolation is recommended.

But what else do you need to know about Hystrix fuses?

Ii. Hystrix combat experience sharing

Under the thread pool isolation policy, the setting of thread pool size and timeout time is very important, which directly affects the responsiveness of system services. For example, if the size of the thread pool is set too large, it will cause resource waste and overhead such as thread switching. If the Settings are too small to support user requests, resulting in request queuing. However, if the timeout time is set too long, some time-consuming requests will block the thread, causing other normal requests to queue up. If set too short, too many normal requests will be fuses.

Recommended to understand below to read the micro service architecture | Hystrix resource isolation strategy how to choice?”

Hystrix’s advice is as follows:

That is, convert to the following calculation formula:

  • Thread pool size = Service TP99 response time (in seconds) * Requests per second + redundant cache value

  • Timeout in milliseconds = 1000 milliseconds/number of requests per second

For example, a certain service TP99 will receive 30 requests every second, and the response time of each request is 200ms, which can be calculated according to the formula above:

Thread pool size = 0.2 * 30 + 4 (redundant cache value) = 10, timeout = 300ms

J. J. 2008

In real development, you might encounter situations where an external calling method has Hystrix annotations used with other annotations, such as query methods with cache annotations. At this point, pay special attention to the execution order between annotations to avoid unexpected results:

  • Cache annotation inactive Hystrix comment at this time the execution of a section in the outermost layer, Hystrix due to the internal implementation is through ProceedingJoinPoint getTarget () to obtain the target object, using reflection to invoke way of execution on the target object method directly, thus cause the loss among other annotation logic. Hystrix annotations can be executed at the innermost layer by specifying the annotation execution Order @order.

  • If Hystrix annotation section execution is at the outermost layer, Hystrix fuse management method logic includes cache call logic as well as remote third-party service calls. An exception in the cache call counts as a whole method exception, causing the whole method to be fused.

Exception handling for The 2008 service

The success or failure rate of a program’s interface requests during execution determines whether a dependent command is opened. If enabled, subsequent requests for this interface will be rejected. As you can see, the control of exceptions makes a big difference to the performance of Hystrix.

Here’s a case study of the problem.

@HystrixCommand(fallbackMethod="executeScriptFallback")public Response<Object> executeScript(FormulaDTO express){ if(! Stringutils.isempty (express.getScript())) {throw new ParamsNotValidException(" invalid parameter "); } try { return sysFormulaLocalApi.executeScript(express); }catch (Exception e){log.error("#executeScript invalid parameter ->{}", jsonUtil.tojsonString (Express),e); return Response.err(JsonUtil.toJsonString(express)); }}Copy the code

A close reading of the code above reveals two exception handling problems.

  • Exception handling when parameter verification fails

Abnormal system call failures such as invalid or invalid parameters should not affect the circuit breaker and should not be calculated within the circuit breaker judgment logic. This can be illegal or invalid parameters such as encapsulation to fuse outer logic anomaly capture processing, or packaging HystrixBadRequestException thrown.

/** * @configuration @enableFeignClients (basePackages = namingConstant.base_package) @enablecircuitbreaker  public class DiscoveryClientConfig { @Autowired private HystrixExtendConfig hystrixExtendConfig; /** * redefinition Hystrix GroupKey */ @bean@scope ("prototype") @conditionAlonProperty (name = "fein.hystrix.enabled ") public Feign.Builder feignHystrixBuilder() { return HystrixFeign.builder() .setterFactory(new HystrixSetterFactory(hystrixExtendConfig)); } /** * hystrix extension Configuration */ @configuration @configurationProperties (prefix = "kmss.hystrix") @data public class HystrixExtendConfig { private Map<String, String> threadpool = new ConcurrentHashMap<>(); private Map<String, String> command = new ConcurrentHashMap<>(); */ @slf4j public class HystrixSetterFactory implements SetterFactory {private HystrixExtendConfig config; public HystrixSetterFactory(HystrixExtendConfig config) { super(); this.config = config; } @Override public Setter create(Target<? > target, Method method) { HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory .asKey(getGroupKey(target, method)); HystrixCommandKey commandKey = HystrixCommandKey.Factory .asKey(Feign.configKey(target.type(), method)); HystrixCommandProperties.Setter properties = getCommandProperties( target); return Setter.withGroupKey(groupKey) .andCommandKey(commandKey) .andCommandPropertiesDefaults(properties); }}Copy the code

Hystrix develops three core key implementations: calculate the groupKey

Private String getGroupKey(Target<? > target, Method method) { String groupKey = getGroupKeyByTarget(target); If (groupKey == null) {// Calculate groupKey based on target groupKey = getDefaultGroupKey(); log.debug("url:{}, hystrix group(default):{}", target.url(), groupKey); } else { log.debug("url:{}, hystrix group:{}", target.url(), groupKey); } return groupKey; }Copy the code

Third, summary

This article mainly summarizes and shares Hystrix’s practical application, including isolation policy, thread pool setting, parameter priority and other knowledge points, as well as specific solutions to problems such as annotation overlay, exception handling, and dynamic parameter configuration. I hope it will be helpful to you.

Focus on high-quality technical articles original sharing and exchange, refuse hydrology, advertorials.

The starting address: micro service architecture | Hystrix fusing relegation which points need to pay attention to?