Hystrix workflow

Hystrix is how to deal with the request, the official website has details: https://github.com/Netflix/Hystrix/wiki/How-it-Works , The article focuses on the flowchart below explain the main processes;

Hystrix is ​​among our packaged into a system call to perform a Comman, give a simple example:

the extends HystrixCommand TestCommand class public <Integer> { 

    Private TestServiceA serviceA; 

    Private int index; 

    Private static HystrixCommandProperties.Setter HystrixCommandProperties.Setter the setter = () 
                                                                                    // request at least 10 fuse was calculated error rate 
                                                                                    .withCircuitBreakerRequestVolumeThreshold (3) 
                                                                                    // fuse 5 seconds after an interrupt request to enter the half-opened state, the discharge flow rate of the last part of retry 
                                                                                    .withCircuitBreakerSleepWindowInMilliseconds (5000)
                                                                                    // open error rate fuse protection 50 
// .withCircuitBreakerErrorThresholdPercentage (30) 
                                                                                    .withExecutionIsolationSemaphoreMaxConcurrentRequests (2) 
                                                                                    .withExecutionTimeoutEnabled (to true) 
                                                                                    .withExecutionTimeoutInMilliseconds (1000); 

    protected TestCommand (TestServiceA serviceA, int index) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("testGroupKey"))
                    .andCommandKey(HystrixCommandKey.Factory.asKey("testCommandKey"))
                    .andCommandPropertiesDefaults(setter)
                    .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(10)));
        this.serviceA = serviceA;
        this.index = index;
    }

    @Override
    protected Integer run() throws Exception {
        return serviceA.service(index);
    }

    @Override
    protected Integer getFallback() {
        // if service execute fail, do sth
        return -1;
    }

}

// execute
TestCommand testCommand = new TestCommand(new TestServiceA(), i);
testCommand.execute();

 We only need to use a Command to wrap our RPC / RMI calls on the use of the above TestCommand wraps a TestService method;

 DETAILED logic flowchart will be explained below;

1, packing requirements:

 Inheritance can be used to wrap HystrixCommand or HystrixObservableCommand business methods;

2, initiated the request:

 To perform a business method calls with a call Command's execute;

 In addition to providing a method Hystrix execute, and also provides for three kinds of parties, all requests inlet:

  

K             value   = command.execute();
Future<K>     fValue  = command.queue();
Observable<K> ohValue = command.observe();         //hot observable
Observable<K> ocValue = command.toObservable();    //cold observable

  As shown in FIG:

  Synchronize call the execute method calls the queue () get () method, queue () will call toObservable () toBlocking () toFuture ()...;

  So, all method calls rely Observable method call, just depends on is the need to call synchronous or asynchronous;

3, cache handling:

  When the request comes, it will determine whether the request is enabled cache (enabled by default), and then determine whether the current request carries the cache Key;

  If the cache hit directly returned; otherwise enter the rest of the logic;

4, it is determined whether the circuit breaker to open (blown):

  Circuit breakers are designed core Hystrix, the circuit breaker is an important means to achieve rapid failure (circuit breaker failure to open direct return);

  After a predetermined time to open the circuit breaker may be provided, the service request may be attempted (default 5000 ms);

5, it is determined whether the service request (whether isolated or downgrade):

  According to the current quality of service will be processed to determine whether you need to ask for business services prior to whether a service request;

  If the current low quality of service (thread pool / queue / semaphore full), it will simply fail;

  Thread pool or semaphore choice (the default is the thread pool):

    The main advantage is the thread pool client isolation and timeouts, but if the request is massive low latency, frequent thread switching loss caused is also very impressive, in which case we can use semaphore strategy;

    The main disadvantage is that the semaphore can not handle the timeout, the request is sent to the client, if the client live pending, then the need to have to wait;

6, the implementation of the service request:

  The current quality of service is better, it will submit a request to the server to service;

  HystrixObservableCommand.construct() or HystrixCommand.run()

7, health monitoring:

  Execution of business method based on historical results, to count the current service health indicators, whether the fuse for the circuit breaker as the basis for such action;  

8/9, in response to the result of failure or success of treatment

Guess you like

Origin www.cnblogs.com/souyoulang/p/11370401.html