Home > Java > javaTutorial > body text

What are the functions of java's downgraded component Hystrix?

WBOY
Release: 2023-04-19 21:16:11
forward
1084 people have browsed it

    #1. Interviewer: Can you briefly introduce the functions of Hystrix?

    Problem analysis: Understand the functions of Hystrix, and at the same time get inspiration from the excellent design concepts of Hystrix in terms of architectural design.

    Answer: I used it in the project. Under the protection of Hystrix, the system can be in a high-availability state for a long time. The commonly used functions are as follows:

    1.1, fail-fast ( Fail-fast)

    Hystrix design provides fail-fast (fail-fast) and fast recovery mechanisms.

    Tip: I don’t know if you have understood the fail-fast mechanism before, or when you were interviewing for Java basics, the Iterator in HashMap is designed to be fail-fast, **fail quickly (fail— fast)** is a mechanism in Java collections. When using an iterator to traverse a collection object, if the contents of the collection object are modified (added, deleted, modified) during the traversal process, a Concurrent Modification Exception will be thrown. .

    The first time I learned HashMap, I didn’t know much about fail-fast. I thought fast failure was only used in Java collection classes to prevent concurrent operations of Java non-thread-safe collections. After learning to use Hystrix, it turned out that fast failure mechanism It can also be applied in system architecture design to quickly fail (fail-fast) requests that cannot be processed in time to reduce system load instead of queuing.

    1.2. Fallback graceful degradation mechanism

    Fallback literally means to start back when encountering a Fall. After learning about the mechanism of Fallback, I immediately used it in the project.

    Look at a real example:

     @Override
        @Degrade(key = "getOrderByParamFromES", fallBackMethod = "getOrderByParamFromMysql")
        public OrderResult getOrderByParamFromES(OrderSearchParam param) {
            //走ES查询
            ......
            return OrderResult;
        }
     		//fallBack后调用getOrderByParamFromMysql方法
     		public OrderResult getOrderByParamFromMysql(OrderSearchParam param) {
            //走mysql查询
            ......
            return OrderResult;
        }
    Copy after login

    Code explanation:

    fallBackMethod = "getOrderByParamFromMysql"

    is after the ES query failure , the system automatically downgrades the getOrderByParamFromMysql method and uses mysql query. Under normal circumstances, getOrderByParamFromMysql will not be called unless it falls.

    1.3. Thread/Semaphore Isolation Mechanism

    Thread Isolation:

    The request will obtain the thread execution in the corresponding thread pool according to its own key, and dynamically set the thread pool parameters. This naturally isolates different requests and supports asynchronousness to improve interface performance. Different requests have no direct impact. For example, service1 requests are slow, but service2 and service3 can still work normally. The disadvantage is that thread switching affects performance.

    Semaphore isolation:

    Service1, service2, and service3 are accessed in one request. If the service1 request times out, the entire semaphore will not be released, and other requests will not be accepted.

    For requests with small latency (such as access to cache or local access to the database), the overhead caused by the thread pool is very high. You can consider using other methods, such as non-blocking semaphores (timeouts are not supported) ) to achieve isolation of dependent services. But in most cases, Netflix prefers to use thread pools to isolate dependent services because the extra overhead it brings is acceptable and it can support all functions including timeouts.

    2. Interviewer: I just mentioned thread isolation. In actual use, do you turn on the timeout thread interruption switch?

    Problem analysis: Based on the actual usage experience, according to the characteristics of the thread itself, the thread times out. If it is not interrupted in time, thread resources will be wasted.

    Answer: Under normal circumstances, we will turn on the timeout interrupt switch in order to release thread resources in time.

    Set by hystrix.command.default.execution.isolation.thread.interruptOnTimeout = true.

    But if you are writing database commands or recording key log commands, you need to turn off the timeout interrupt if you need to complete the command execution.

    (The interviewer nodded with satisfaction, believing that I do have Hystrix maintenance experience)

    3. Interviewer: How did you estimate the thread pool size?

    Answer: To correctly set the size of the thread pool, you need to analyze the number of CPUs, memory size, and task types of the deployed system (computing-intensive, IO-intensive, etc.). For computing-intensive tasks, the thread pool size and A similar number of CPUs can usually achieve optimal utilization. For IO-intensive tasks, the calculation formula for the optimal size of the thread pool is: thread pool size = number of CPUs * (1 task waiting time / task processing time).

    In-depth analysis

    Hystrix history

    Hystrix originated from a project started by the Netflix API team in 2011. In 2012, Hystrix continued to evolve and mature, and many teams within Netflix adopted it. Today, tens of billions of thread-isolated and hundreds of billions of semaphore-isolated calls are performed every day on Netflix via Hystrix. This greatly improves uptime and resiliency.

    Under high concurrent access, the stability of the services that the system relies on has a great impact on the system. There are many uncontrollable factors in dependence, such as slow network connections, suddenly busy resources, and temporary unavailability. Wait offline. If we want to build a stable and reliable distributed system, we must have such a fault-tolerant method.

    Hystrix的主要功能特性

    熔断器机制:熔断器可以理解成保险丝,项目里使用Hystrix Command,当 Hystrix Command请求后,如果服务失败数量超过一定比例(比如默认50%),断路器自动熔断,该服务将进入熔断状态,后续请求都会进入fallback。

    降级机制:通过fallbackMethod注解,当请求后端服务出现异常的时候, 为了避免影响到其他业务逻辑,可以使用fallback方法指定的方法快速返回,或启用“备胎方案”。

    环境隔离:包括线程隔离和信号量隔离。

    cache:Hystrix支持将一个请求结果缓存起来,下一个具有相同key的请求将直接从缓存中取出结果,减少请求开销。

    Hystrix Demo

    通过一个demo快速理解Hystrix fallback 的使用

    @Service
    public class OrderQueryService {
         /**
         * 订单查询接口
         */
        @HystrixCommand(fallbackMethod = "queryOrderBack")
        public List<Order> queryOrderFromRedis(String userId) {
          // todo  reids查询逻辑
          return orderlist;
        }
         /**
         * 订单查询接口失败降级方案
         */
        @SuppressWarnings("unused")
        private String queryOrderBack(String userId) {
          // todo  如,走ES查询逻辑  或者 直接提示用户“请稍后再试”
          // todo 通知维护人员处理故障
          return "";
        }
    }
    Copy after login

    代码解释:

    程序正常时,查询订单服务是走queryOrderFromRedis方法的逻辑,当queryOrderFromRedis方法抛出异常,根据设定的异常比例,或者指定哪个异常,达到阈值触法fallback开关,程序切换到queryOrderBack,设置程序走ES查询逻辑 或者 直接提示用户“请稍后再试”,根据业务自行设置。

    哪些情况下会触发fallback?

    Failure TypeException classException.cause触发fallback
    FAILUREHystrixRuntimeExceptionunderlying exception (user-controlled)YES
    SEMAPHORE_REJECTEDHystrixRuntimeExceptionj.l.RuntimeExceptionYES
    SHORT_CIRCUITEDHystrixRuntimeExceptionj.l.RuntimeExceptionYES
    THREAD_POOL_REJECTEDHystrixRuntimeExceptionj.u.c.RejectedExecutionExceptionYES
    TIMEOUTHystrixRuntimeExceptionj.u.c.TimeoutExceptionYES

    FAILURE:任意RuntimeException异常都可以激活fallback。

    THREAD_POOL_REJECTED:并发执行的任务数超过线程池和队列之和时,也就是Hystrix的线程隔离机制。

    SEMAPHORE_REJECTED:类似 THREAD_POOL_REJECTED ,当服务的并发数大于信号量阈值时将进入fallback。比如配置程序执行并发数不能大于3,由于信号量隔离下无论调用哪种命令执行方法,Hystrix都不会创建新线程执行run()/construct(),所以调用程序需要自己创建多个线程来模拟并发调用execute(),最后看到一旦并发线程>3,后续请求都进入fallback。

    SHORT_CIRCUITED:在一定时间内,用户请求超过一定的比例失败时,如超时,异常,线程并发达到限定最大值等,断路器都会打开;短路器打开后所有请求直接走fallback,可以通过。circuitBreakerErrorThresholdPercentage方法设置百分比,默认是50。

    TIMEOUT:即超时请求。

    附录:Hystrix策略配置

    /* --------------统计相关------------------*/ 
    // 统计滚动的时间窗口,默认:5000毫秒(取自circuitBreakerSleepWindowInMilliseconds)   
    private final HystrixProperty metricsRollingStatisticalWindowInMilliseconds;   
    // 统计窗口的Buckets的数量,默认:10个,每秒一个Buckets统计   
    private final HystrixProperty metricsRollingStatisticalWindowBuckets; // number of buckets in the statisticalWindow   
    // 是否开启监控统计功能,默认:true   
    private final HystrixProperty metricsRollingPercentileEnabled;   
    /* --------------熔断器相关------------------*/ 
    // 熔断器在整个统计时间内是否开启的阀值,默认20。也就是在metricsRollingStatisticalWindowInMilliseconds(默认10s)内至少请求20次,熔断器才发挥起作用   
    private final HystrixProperty circuitBreakerRequestVolumeThreshold;   
    // 熔断时间窗口,默认:5秒.熔断器中断请求5秒后会进入半打开状态,放下一个请求进来重试,如果该请求成功就关闭熔断器,否则继续等待一个熔断时间窗口
    private final HystrixProperty circuitBreakerSleepWindowInMilliseconds;   
    //是否启用熔断器,默认true. 启动   
    private final HystrixProperty circuitBreakerEnabled;   
    //默认:50%。当出错率超过50%后熔断器启动
    private final HystrixProperty circuitBreakerErrorThresholdPercentage;  
    //是否强制开启熔断器阻断所有请求,默认:false,不开启。置为true时,所有请求都将被拒绝,直接到fallback 
    private final HystrixProperty circuitBreakerForceOpen;   
    //是否允许熔断器忽略错误,默认false, 不开启   
    private final HystrixProperty circuitBreakerForceClosed; 
    /* --------------信号量相关------------------*/ 
    //使用信号量隔离时,命令调用最大的并发数,默认:10   
    private final HystrixProperty executionIsolationSemaphoreMaxConcurrentRequests;   
    //使用信号量隔离时,命令fallback(降级)调用最大的并发数,默认:10   
    private final HystrixProperty fallbackIsolationSemaphoreMaxConcurrentRequests; 
    /* --------------其他------------------*/ 
    //使用命令调用隔离方式,默认:采用线程隔离,ExecutionIsolationStrategy.THREAD   
    private final HystrixProperty executionIsolationStrategy;   
    //使用线程隔离时,调用超时时间,默认:1秒   
    private final HystrixProperty executionIsolationThreadTimeoutInMilliseconds;   
    //线程池的key,用于决定命令在哪个线程池执行   
    private final HystrixProperty executionIsolationThreadPoolKeyOverride;   
    //是否开启fallback降级策略 默认:true   
    private final HystrixProperty fallbackEnabled;   
    // 使用线程隔离时,是否对命令执行超时的线程调用中断(Thread.interrupt())操作.默认:true   
    private final HystrixProperty executionIsolationThreadInterruptOnTimeout; 
    // 是否开启请求日志,默认:true   
    private final HystrixProperty requestLogEnabled;   
    //是否开启请求缓存,默认:true   
    private final HystrixProperty requestCacheEnabled; // Whether request caching is enabled
    //请求合并是允许的最大请求数,默认: Integer.MAX_VALUE   
    private final HystrixProperty maxRequestsInBatch;   
    //批处理过程中每个命令延迟的时间,默认:10毫秒   
    private final HystrixProperty timerDelayInMilliseconds;   
    //批处理过程中是否开启请求缓存,默认:开启   
    private final HystrixProperty requestCacheEnabled; 
    /* 配置线程池大小,默认值10个 */ 
    private final HystrixProperty corePoolSize; 
    /* 配置线程值等待队列长度,默认值:-1 建议值:-1表示不等待直接拒绝,测试表明线程池使用直接决绝策略+ 合适大小的非回缩线程池效率最高.所以不建议修改此值。 当使用非回缩线程池时,queueSizeRejectionThreshold,keepAliveTimeMinutes 参数无效 */
    private final HystrixProperty maxQueueSize;
    Copy after login

    其他常用限流降级组件

    Sentinel:阿里巴巴集团内部基础技术模块,覆盖了所有的核心场景。Sentinel 也因此积累了大量的流量归整场景以及生产实践。2018 年,Sentinel 开源,并持续演进。

    Resilience4j:也是一个轻量级的容错组件,其灵感来自于 Hystrix,但主要为 Java 8 和函数式编程所设计。轻量级体现在其只用 Vavr库(前身是 Javaslang),没有任何外部依赖。而 Hystrix 依赖了 Archaius ,Archaius 本身又依赖很多第三方包,例如 Guava、Apache Commons Configuration 等。

    Sentinel 与 Hystrix resilience4j 对比

      Sentinel Hystrix resilience4j
    隔离策略 信号量隔离(并发线程数限流) 线程池隔离/信号量隔离 信号量隔离
    熔断降级策略 基于响应时间、异常比率、异常数等 异常比率模式、超时熔断 基于异常比率、响应时间
    实时统计实现 滑动窗口(LeapArray) 滑动窗口(基于 RxJava) Ring Bit Buffer
    动态规则配置 支持多种配置源 支持多种数据源 有限支持
    扩展性 丰富的 SPI 扩展接口 插件的形式 接口的形式
    基于注解的支持 支持 支持 支持
    限流 基于 QPS,支持基于调用关系的限流 有限的支持 Rate Limiter
    集群流量控制 支持 不支持 不支持
    流量整形 支持预热模式、匀速排队模式等多种复杂场景 不支持 简单的 Rate Limiter 模式
    系统自适应保护 支持 不支持 不支持
    控制台 提供开箱即用的控制台,可配置规则、查看秒级监控、机器发现等 简单的监控查看 不提供控制台,可对接其它监控系统
    多语言支持 Java / C++ Java Java
    开源社区状态 活跃 停止维护 较活跃

    The above is the detailed content of What are the functions of java's downgraded component Hystrix?. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!