1、常用参数说明
下面是一些常用的配置:
配置项
默认值
默认属性
实例属性
隔离策略,hystrixcommandkey,如果不配置,默认为方法名
thread
hystrix.command.default.execution.isolation.strategy
hystrix.command.hystrixcommandkey.execution.isolation.strategy
超时时间,hystrixcommand命令执行超时时间,单位:毫秒
1000
hystrix.command.default.execution.isolation.thread.timeoutinmilliseconds
hystrix.command.hystrixcommandkey.execution.isolation.thread.timeoutinmilliseconds
hystrixcommand命令执行是否开启超时
true
hystrix.command.default.execution.timeout.enabled
hystrix.command.hystrixcommandkey.execution.timeout.enabled
超时的时候,是否中断执行操作
true
hystrix.command.default.execution.isolation.thread.interruptontimeout
hystrix.command.hystrixcommandkey.execution.isolation.thread.interruptontimeout
信号量请求数,当设置为信号量隔离策略时,设置最大允许的请求数
10
hystrix.command.default.execution.isolation.semaphore.maxconcurrentrequests
hystrix.command.hystrixcommandkey.execution.isolation.semaphore.maxconcurrentrequests
circuitbreaker设置打开fallback并启动fallback逻辑的错误比例
50
hystrix.command.default.circuitbreaker.errorthresholdpercentage
hystrix.command.hystrixcommandkey.circuitbreaker.errorthresholdpercentage
强制打开断路器,拒绝所有请求
false
hystrix.command.default.circuitbreaker.forceopen
hystrix.command.hystrixcommandkey.circuitbreaker.forceopen
当为线程隔离时,核心线程池大小
10
hystrix.threadpool.default.coresize
hystrix.threadpool.hystrixthreadpoolkey.coresize
当隔离策略为线程池隔离模式时,最大线程池大小配置。1.5.9版本中还需要allowmaximumsizetodivergefromcore为true
10
hystrix.threadpool.default.maximumsize
hystrix.threadpool.hystrixthreadpoolkey.maximumsize
allowmaximumsizetodivergefromcore,该属性允许配置maximumsize生效
false
hystrix.threadpool.default.allowmaximumsizetodivergefromcore
hystrix.threadpool.hystrixthreadpoolkey.default.allowmaximumsizetodivergefromcore
在真实的项目中,一般会对超时时间、线程池大小、信号量等进行修改,具体需要根据业务,hystrix默认超时1秒,实际项目中,这个时间是肯定不够的,一般会设置5-10秒,如果有同步上传的业务,时间需要更长,如果配置了ribbon的时间,其超过时间也需要和ribbon的时间配合使用,一般情况下,ribbon的时间应短于hystrix的超时时间。
2、hystrix缓存使用
常用的注解说明:
注解
说明
@cacheresult
使用该注解后,结果会被缓存,同时它需要和 @hystrixcommand(commandkey = "xxx") 一起使用,注解参数为cachekeymethod
@cacheremove(commandkey="xxx")
清除缓存,需要指定commandkey,注解参数为cachekeymethod
@cachekey
指定请求命令参数,默认使用方法所有参数作为key,注解属性为value
一般在查询接口上使用@cacheresult,在更新接口上使用@cacheremove删除缓存。
在使用hystrix缓存,注意事项:
需要开启 @enablehystrix
需要初始化 hystrixrequestcontext
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.springframework.web.servlet.handlerinterceptor;
import org.springframework.web.servlet.modelandview;
import com.netflix.hystrix.strategy.concurrency.hystrixrequestcontext;
/**
* 初始化hystrix上下文
*/
public class hystrixcontextinterceptor implements handlerinterceptor {
private hystrixrequestcontext context;
@override
public boolean prehandle(httpservletrequest request, httpservletresponse response, object arg2) {
context = hystrixrequestcontext.initializecontext();
return true;
}
@override
public void aftercompletion(httpservletrequest request, httpservletresponse response, object arg2, exception arg3) {
context.shutdown();
}
}
在指定了 hystrixcommand 的commandkey以后,在@cacheremove也要指定commandkey
希望与广大网友互动??
点此进行留言吧!