@HystrixCommand(fallbackMethod="getStudentCourseRecommonedFallback",
groupKey="recommend",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "20"),
@HystrixProperty(name = "execution.timeout.enabled", value = "true"),
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "3")}
)
fallbackMethod是指方法出现异常或者超时调用的方法,在当前类中的方法
groupKey是指具有相同的groupKey使用同一个线程池,hystrix-recommend-*
commandProperties是指配置属性,
timeoutInMilliseconds是指超时时间,单位为毫秒
execution.timeout.enabled是指是否开户超时开关,如为false,则timeoutInMilliseconds无效
circuitBreaker.requestVolumeThreshold是指滚动时间内出问题次数则进入全熔断,
进入后原方法不会调用,会使用fallbackMethod指定方法
circuitBreaker.sleepWindowInMilliseconds默认值为5000毫秒,自动进入半开状态,
尝试调用原方法,如可以使用则关闭熔断。
使用注解前提打开spring的aspectj-autoproxy
引入的包为
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.11</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-metrics-event-stream</artifactId>
<version>1.5.11</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.11</version>
</dependency>
引入bean
<bean id="hystrixAspect" class="com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect"></bean>