Introduction to Soul Chapter 15 Principles of hystrix Fuse plugs

introduce


A distributed system is usually made up of many interdependent services, due to the service, a service failure probability increases relatively, the service call each other, when some are dependent on service failure or response delays, call the service the service may appear problems such as resource depletion, in the worst case, the application will crash.

The Hystrix framework controls interdependencies between services by providing circuit breakers and downgrades, and avoids failure escalation by isolating failed services and stopping the cascade of failures.

The function of Hystrix


  • Fail fast, recover fast.

  • demotion

  • Stop cascading faults of complex distributed systems

How does Soul introduce the Hystrix plugin


1. Admin starts the Hystrix plug-in on the background

2. Add selectors and rules to Hystrix

Configure which requests are processed by Hystrix. So how do you deal with that? Tell Hystrix what to do with it by rules.

Take a look at the key code

@Override
protected Mono<Void> doExecute(final ServerWebExchange exchange, final SoulPluginChain chain, final SelectorData selector, final RuleData rule) {
    final SoulContext soulContext = exchange.getAttribute(Constants.CONTEXT);
    assertsoulContext ! =null;
  // Encapsulates the breaker rules
    final HystrixHandle hystrixHandle = GsonUtils.getInstance().fromJson(rule.getHandle(), HystrixHandle.class);
   / / generated command
    Command command = fetchCommand(hystrixHandle, exchange, chain);
    return Mono.create(s -> {
        Subscription sub = command.fetchObservable().subscribe(s::success,
                s::error, s::success);
        s.onCancel(sub::unsubscribe);
        if (command.isCircuitBreakerOpen()) {
            log.error("hystrix execute have circuitBreaker is Open! groupKey:{},commandKey:{}", hystrixHandle.getGroupKey(), hystrixHandle.getCommandKey());
        }
    }).doOnError(throwable -> {
        log.error("hystrix execute exception:", throwable);
        exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE, ResultEnum.ERROR.getName());
        chain.execute(exchange);
    }).then();
}
Copy the code

4. Fail tests quickly

Add thread.sleep (1000*5) to the method being accessed; Sleep for 5 seconds

Go to http://localhost:9195/springcloud/order/findById? id=95

The first few failed after waiting about five seconds, and the subsequent visits failed quickly

{

​ “code”: 500,

​ “message”: “Internal Server Error”,

​ “data”: “/order/findById short-circuited and fallback failed.”

}

summary

To understand the ins and outs of hystrix, first 🉐️ learn about hystrix functions and usage scenarios, and how to configure parameters.