spring cloud using Feign Hystrix fuse

 

Avalanche

In the micro-services architecture usually have multiple service layer calls, failure of basic services could lead to cascading failures, and cause the entire system is unavailable, a phenomenon called avalanche effect service. Services avalanche effect is a result of "service provider" of unavailability "service consumer" is not available and unavailable gradually enlarge the process.

If it is shown below: as a service provider A, B is the service consumer A, C and D are B Service Consumers. A Not available due to unusable B, and is not available to an enlarged snowball C and D, the avalanche effect is formed.

 

 

 

Fuse (CircuitBreaker)

Fuse principle is very simple, as the power overload protection. It can be implemented quickly fail if it detects many similar errors over time, will force more quickly after calling it a failure, no longer access a remote server in order to prevent the application constantly try to execute the operation may fail so that the application continues to execute without waiting for error correction, or a waste of CPU time to produce a long wait for a timeout. Fuses can also enable an application to diagnose whether an error has been corrected, if already amended application attempts to call the operation again.

Acting like a fuse mode operation of those error-prone. This agent is able to record the number of errors recent call occurs, then decided to use allows the operator to continue, or returns an error immediately. Fuse switch logic conversion as shown below:

 

 

 

Fuse is the availability of protection services last line of defense.

Hystrix properties

1. The circuit breaker mechanism

Circuit breakers is well understood that, when a backend service request failed Hystrix Command quantity exceeds a certain percentage (default 50%), the circuit breaker will switch to an open state (Open). In this case all requests are not sent directly to the failure of the backend service the circuit breaker remains in an open state after a period of time (5 seconds by default) automatically switches to the half-open state (hALF-oPEN). in this case a request to return the situation will determine if the request is successful, the closed state of the circuit breaker switch back ( CLOSED), or switch back to the open state (oPEN). Hystrix circuit breaker is like our family in the fuse, once the back-end service is not available, the circuit breaker will cut off the direct chain of requests, to avoid sending a large number of invalid requests affect system throughput and circuit breakers have the ability to self-testing and recovery.

2.Fallback

Fallback equivalent downgrade. For query operation, we can achieve a fallback method, when the back-end service request an exception, you can use the value fallback method returns. The return value is usually set default values ​​or from the cache fallback method.

3. Resource isolation

In Hystrix mainly by the thread pool resources to achieve isolation, usually in use when we will be divided into multiple threads pool based remote service call. For example call into goods and services Command A thread pool, Command call account services put B into the thread pool. the main advantage of this is that the operating environment was set apart. such services exist, even if the calling code bug or due to other causes their own time thread pool is exhausted, will not affect other services system. but the cost of maintaining multiple threads is to bring the pool system will bring additional performance overhead. If there are strict performance requirements and believe that your client code to invoke the service will not be a problem, you can use Hystrix signal pattern ( Semaphores) to isolate resources.

achieve

In the last article, using Feign, implementation calls in services, more convenient way Feign call, which will help to understand.

Next spring cloud do give a fuse, it means to do a service exception handling, there is a problem when the provider node goes down, inaccessible

Make a deal to prevent the avalanche effect, leading to paralysis of the cluster. We only need to service the caller Consumer transform it.

Because support Feign in and have Hystrix, so do not rely on additional import new

application.yml open fuse

feign:
  hystrix:
    enabled: true

MessageRemoteHyx.java fuse inherit the call interface Feign

@Component
public class MessageRemoteHyx implements MessageRemote{

    @Override
    public Map hello() {
        Map map=new HashMap();
        map.put("message","hello world is error!!");
        return map;
    }
}

MessageRemote interface added

fallback = MessageRemoteHyx.class
@FeignClient(name = "PROVIDER",fallback = MessageRemoteHyx.class)
public interface MessageRemote {

    @RequestMapping(value = "/provider/hello")
    Map hello();
}

First, through Feign normal call HelloWorld service

 

 Now manually shut off Provider, call again

 

After starting Provider again, take some time (default after 5 seconds, turn semi-open state, attempts to access the service, if successful, turn off the fuse)

 

Guess you like

Origin www.cnblogs.com/yhood/p/11572427.html