美文网首页
Hystrix 使用说明

Hystrix 使用说明

作者: 赫灵 | 来源:发表于2018-12-12 16:26 被阅读0次

一、简介:

Hystrix是用于分布式场景下服务熔断、降级的开源Java库,它的主要作用有:依赖隔离,熔断,降级和监控报警。

二、支持功能说明:

1、支持服务隔离,熔断,降级;

2、支持某一个时间窗口内的错误次数、错误百分比等熔断条件配置;

3、熔断支持自动恢复(熔断半开Half-Open);

4、支持自定义降级方法;

三、依赖组件

<properties>

    <hystrix.version>1.5.12hystrix.version>

</properties>

<dependency>

    <groupId>com.netflix.hystrix</groupId>

    <artifactId>hystrix-core</artifactId>

    <version>${hystrix.version}</version>

</dependency>

<dependency>

    <groupId>com.netflix.hystrix</groupId>

    <artifactId>hystrix-javanica</artifactId>

    <version>${hystrix.version}</version>

</dependency>

<dependency>

    <groupId>com.netflix.hystrix</groupId>

    <artifactId>hystrix-metrics-event-stream</artifactId>

    <version>${hystrix.version}</version>

</dependency>

四、Spring配置:

<!-- hystrix原生配置, 配置此bean可提供hystrix原生功能 -->

<bean class="com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect" />

五、代码示例:

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

import org.springframework.stereotype.Service;

@Service

public class HystrixDemoService {

    @HystrixCommand(groupKey = "exampleGroupKey", commandKey = "exampleCommandKey", threadPoolKey = "exampleThreadPoolKey", fallbackMethod = "fallback",

            commandProperties = {

                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "${execution.isolation.thread.timeoutInMilliseconds}"),

                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "${circuitBreaker.requestVolumeThreshold}"),

                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "${circuitBreaker.errorThresholdPercentage}")

            },

            threadPoolProperties = {

                    @HystrixProperty(name = "coreSize", value = "${coreSize}"),

                    @HystrixProperty(name = "maximumSize", value = "${maximumSize}"),

                    @HystrixProperty(name = "allowMaximumSizeToDivergeFromCoreSize", value = "${allowMaximumSizeToDivergeFromCoreSize}")

            }

    )

    public String example() {

        return "Hello World!";

    }

    /**

    * 降级方法和主方法保持一致的入参、出参

    */

    public String fallback() {

        return "Fallback";

    }

}

相关文章

网友评论

      本文标题:Hystrix 使用说明

      本文链接:https://www.haomeiwen.com/subject/hwxphqtx.html