What is Hystrix? The Chinese name for Hystrix is “porcupine”. The porcupine is covered with spines to protect itself from predators, representing a defense mechanism. This is exactly the same as hystrix itself, so the Netflix team named the framework Hystrix and used the corresponding cartoon character as the logo. In a distributed system, many dependencies will inevitably fail, such as timeouts, exceptions, etc. How to ensure that the failure of a dependency does not lead to the failure of the entire service is Hystrix’s task. Hystrix provides fuses, isolation, Fallback, cache, monitoring, and more to keep the system usable in the event of a simultaneous failure of one or more dependencies. Why do we need Hystrix? In large and medium-sized distributed systems, the system usually has many dependencies (HTTP,hession,Netty,Dubbo, etc.), as shown below:

Dependency I with QPS of 50 becomes unavailable, but other dependencies are still available.











The Netflix API processes 10+ billion HystrixCommand executions per day using thread isolation.

Each API instance has 40+ thread-pools with 5-20 threads in each (most are set to 10).

How does Hystrix address dependency isolation?

Hystrix uses the Command pattern HystrixCommand(Command) to wrap the dependent call logic, with each Command executed in a separate thread/under signal authorization. You can configure the dependent call timeout period. The timeout period is generally set to slightly higher than 99.5% average time. When the call times out, the fallback logic is returned or executed directly. Provide a small thread pool (or signal) for each dependency, and the call will be rejected immediately if the thread pool is full, with no queuing by default. Speed up the failure determination time. Dependent call results: success, failure (throw exception), timeout, thread rejection, short circuit. Fallback logic is executed when the request fails (exception, rejection, timeout, short circuit). Provides fuse components that can be run automatically or manually called to stop the current dependence for a period of time (10 seconds). The fuse default error rate threshold is 50%, beyond which it will run automatically. Provides statistics and monitoring for near real-time dependency.

Hystrix relies on the isolation architecture as shown below:

Too much source code, not one post up, here only show the main test source code.

public static void main(String[] args) { System.out.println(test(“javastack”)); }

private static String test(String name) { HystrixUtil.HystrixReqConfig hc = HystrixUtil.HystrixReqConfig.withGroupKey(“TestGroup”).withTimeout(3) .withUnit(TimeUnit.SECONDS).withPassNum(64);

String result = HystrixUtil.getExcuteResult(new HystrixCallableService<String>() {

    @Override
    public String execute() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "success " + name;
    }

    @Override
    public String fallback() {
        return "fallback " + name;
    }

}, hc);

return result;
}
Copy the code

This set a 3-second timeout to enter the circuit breaker. Sleep for 5 seconds in the test program, enter the fuse and output:

fallback javastack

Sleep for 2 seconds in test program, enter normal flow and output:

success javastack

If the fuse test is successful, even if one service fails, the normal operation of the whole system will not be affected.

You can add Java Architect group to get Java engineering, high performance and distributed, high performance, easy to understand. High architecture. Performance tuning, Spring, MyBatis, Netty source analysis and big data and other knowledge points of advanced advanced dry live free learning rights are danniu belt fly let you walk a lot less detour group.. Number is: 855801563 to the small white do not enter had better be to have development experience

Note: Add group requirement

1. Those with work experience, who do not know where to start in the face of the current popular technology and need to break the technical bottleneck can be added.

2. I have been in the company for a long time and have been comfortable, but I hit a wall in the interview when I changed my job. Need to study in a short period of time, job-hopping can be added.

3. If you have no working experience, but have a solid foundation, and have a good command of Java working mechanism, common design ideas and common Java development framework, you can add.

4, feel very good B, general needs can be done. But the knowledge points learned are not systematic, it is difficult to continue to break through in the field of technology can be added.

5. Ali Java senior bull live explain knowledge points, share knowledge, sorting out and summarizing years of work experience, with everyone to establish their own technical system and technical knowledge in a comprehensive and scientific way!