上一篇文章hystrix使用中讲解了使用Hystrix的基本方法,能够看出须要手写大量代码,作为一个不爱动手的程序猿固然以为麻烦,因此Javanica诞生了。
Javanica项目经过引入支持注解,使Hystrix更容易使用:
1.添加依赖java
<dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-core</artifactId> <version>1.5.11</version> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> <version>1.5.11</version> </dependency>
2.添加AOPsegmentfault
<aop:aspectj-autoproxy/> <bean id="hystrixAspect" class="com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect"></bean>
3.添加HystrixCommand注解ui
public class StudentService { ... @HystrixCommand(groupKey= "group", commandKey = "commandkey" commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "100")}) public Student getNameById(String id) { return studentResource.getNameById(id); } } ...
更多配置:
execution.isolation.thread.timeoutInMilliseconds|执行超时时间|default:1000
circuitBreaker.requestVolumeThreshold|触发断路最低请求数|default:20
circuitBreaker.sleepWindowInMilliseconds|断路器恢复时间|default:5000
circuitBreaker.errorThresholdPercentage|触发短路错误率,单位%|default:50
coreSize|线程池核心数|default:10
maxQueueSize|队列长度|default:-1(SynchronousQueue)
queueSizeRejectionThreshold|队满拒绝服务阈值|default:5|此值生效优先于队满
metrics.rollingStats.timeInMilliseconds|窗口维持时间|默认10000
metrics.rollingPercentile.numBuckets|窗口拆分数|默认10.net