spring boot hystrix 客户端接入
描述:随着微服务的部署开发,为了防止雪崩效应hystrix应运而生,解决了微服务雪崩效应,处理限流等一列应用的问题。
上代码进行了解和测试。
新建一个springboot项目
POM文件
基于springboot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
注意:版本之前可能会出现接口异常
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
spring boot项目的main方法启动类
启动类里面加在hystrix的启动
@EnableHystrix
@RestController
@SpringBootApplication
@EnableEurekaClient
同一个目录下建立hystrix配置bean文件 此处主要是监控流发送的位置
/**
* @ClassName HystrixConfig
* @Description TODO HYSTRIX配置
* @Author tanli
* @Date 2019-11-11
* @Since 1.0
*/
@Configuration
public class HystrixConfig {
/***
* @MethodName
* @Description TODO 用来拦截处理HystrixCommand注解
* @param
* @param null
* @Return
* @Author tanli45
* @Date 2019-11-11
* @Since 1.0
*/
@Bean
public HystrixCommandAspect hystrixAspect(){
return new HystrixCommandAspect();
}
/***
* @MethodName hystrixServletBean
* @Description TODO 用来像监控中心Dashboard发送stream信息
* @param
* @Return org.springframework.boot.web.servlet.ServletRegistrationBean
* @Author tanli45
* @Date 2019-11-11
* @Since 1.0
*/
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
测试类方法代码
@HystrixCommand(fallbackMethod = "onLinkError",
commandProperties ={
@HystrixProperty(name="execution.isolation.strategy",value="THREAD"),
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "1000"),
@HystrixProperty(name="circuitBreaker.enabled",value="true"),
@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "2"),
},
threadPoolProperties = {
@HystrixProperty(name="coreSize",value="15"),
// @HystrixProperty(name="maximumSize",value = "5"),
@HystrixProperty(name="maxQueueSize",value ="10")
}
)
@RequestMapping("/testpropertis")
public String testpropertis(String name){
try{
Thread.sleep(200);
}catch (InterruptedException e){
e.printStackTrace();
}
return "Hello,"+name;
}
@HystrixCommand
@RequestMapping("/testp")
public String testp(String name){
if (StringUtils.isBlank(name)){
throw new RuntimeException("name不能为空");
}
return "Good morninng ,"+name;
}
/***
* @MethodName onLinkError
* @Description TODO 如果fallback方法的参数和原方法参数个数不一致,则会出现FallbackDefinitionException: fallback method wasn't found
* @param
* @param name
* @Return java.lang.String
* @Author tanli45
* @Date 2019-11-11
* @Since 1.0
*/
public String onLinkError(String name){
return "Error !!! "+name;
}
启动项目监控dashboard:
启动好客户端项目后在浏览器中输入访问地址
ip地址+端口号/hystrix.stream (hystirx配置对象配置中的) 如下图
注释:一直出现ping 和 loading 请找一个用了hystrixCommand 服务进行请求访问就可以解决,其他问题可以查看服务的配置的参数是否正确,这个地方在写demo的过程中有碰到过一直访问不了的错误。
如上代码在浏览器中的输入URL 访问得到数据
如图:
服务都已经正常了将服务拉去监控
监控的图形如图:
参数解释如图
代码中各个参数监控
(一)Command Properties
属性控制HystrixCommand行为
Execution
参数 | 描述 | 默认值 |
---|---|---|
execution.isolation.strategy | 隔离策略,有THREAD和SEMAPHORE THREAD - 它在单独的线程上执行,并发请求受线程池中的线程数量的限制 SEMAPHORE - 它在调用线程上执行,并发请求受到信号量计数的限制 | 默认使用THREAD模式,以下几种场景可以使用SEMAPHORE模式:只想控制并发度 外部的方法已经做了线程隔离 调用的是本地方法或者可靠度非常高、耗时特别小的方法(如medis) |
execution.isolation.thread.timeoutInMilliseconds | 超时时间 | 默认值:1000 在THREAD模式下,达到超时时间,可以中断 在SEMAPHORE模式下,会等待执行完成后,再去判断是否超时 设置标准: 有retry,99meantime+avg meantime 没有retry,99.5meantime |
execution.timeout.enabled | HystrixCommand.run()执行是否应该有超时。 | 默认值:true |
execution.isolation.thread.interruptOnTimeout | 在发生超时时是否应中断HystrixCommand.run()执行。 | 默认值:true THREAD模式有效 |
execution.isolation.thread.interruptOnCancel | 当发生取消时,执行是否应该中断 | 默认值为false THREAD模式有效 |
execution.isolation.semaphore.maxConcurrentRequests | 设置在使用时允许到HystrixCommand.run()方法的最大请求数。 | 默认值:10 SEMAPHORE模式有效 |
2、Fallback
以下属性控制HystrixCommand.getFallback()如何执行。这些属性适用于ExecutionIsolationStrategy.THREAD和ExecutionIsolationStrategy.SEMAPHORE。
参数 | 描述 | 默认值 |
---|---|---|
fallback.isolation.semaphore.maxConcurrentRequests | 设置从调用线程允许HystrixCommand.getFallback()方法的最大请求数。 | SEMAPHORE模式有效默认值:10 |
rfallback.enabled | 确定在发生失败或拒绝时是否尝试调用HystrixCommand.getFallback()。 | 默认值为true |
Circuit Breaker
断路器属性控制HystrixCircuitBreaker的行为。
参数 | 描述 | 默认值 |
---|---|---|
circuitBreaker.enabled | 定断路器是否用于跟踪运行状况和短路请求(如果跳闸)。 | 默认值为true |
circuitBreaker.requestVolumeThreshold | row熔断触发的最小个数/10s | 默认值:20 |
circuitBreaker.sleepWindowInMilliseconds | 熔断多少秒后去尝试请求 | 默认值:5000 |
circuitBreaker.errorThresholdPercentage | 失败率达到多少百分比后熔断 | 默认值:50主要根据依赖重要性进行调整 |
circuitBreaker.forceOpen | row属性如果为真,强制断路器进入打开(跳闸)状态,其中它将拒绝所有请求。 | 默认值为false此属性优先于circuitBreaker.forceClosed |
circuitBreaker.forceClosed | 该属性如果为真,则迫使断路器进入闭合状态,其中它将允许请求,而不考虑误差百分比。 | 默认值为false 如果是强依赖,应该设置为truecircuitBreaker.forceOpen属性优先,因此如果forceOpen设置为true,此属性不执行任何操作。 |
4、Metrics
以下属性与从HystrixCommand和HystrixObservableCommand执行捕获指标有关。
参数 | 描述 | 默认值 |
---|---|---|
metrics.rollingStats.timeInMilliseconds | 此属性设置统计滚动窗口的持续时间(以毫秒为单位)。对于断路器的使用和发布Hystrix保持多长时间的指标。 | 默认值:10000 |
metrics.rollingStats.numBuckets | 此属性设置rollingstatistical窗口划分的桶数。以下必须为true - “metrics.rollingStats.timeInMilliseconds%metrics.rollingStats.numBuckets == 0” -否则将抛出异常。 | 默认值:10 |
metrics.rollingPercentile.enabled | 此属性指示是否应以百分位数跟踪和计算执行延迟。 如果禁用它们,则所有摘要统计信息(平均值,百分位数)都将返回-1。 | 默认值为true |
metrics.rollingPercentile.timeInMilliseconds | 此属性设置滚动窗口的持续时间,其中保留执行时间以允许百分位数计算,以毫秒为单位。 | 默认值:60000 |
metrics.rollingPercentile.numBuckets | 此属性设置rollingPercentile窗口将划分的桶的数量。以下内容必须为true - “metrics.rollingPercentile.timeInMilliseconds%metrics.rollingPercentile.numBuckets == 0” -否则将抛出异常。 | 默认值:6 |
metrics.rollingPercentile.bucketSize | 此属性设置每个存储桶保留的最大执行次数。如果在这段时间内发生更多的执行,它们将绕回并开始在桶的开始处重写。 | 默认值:100 |
metrics.healthSnapshot.intervalInMilliseconds | 此属性设置在允许计算成功和错误百分比并影响断路器状态的健康快照之间等待的时间(以毫秒为单位)。 | 默认值:500 |
5、Request Context
这些属性涉及HystrixCommand使用的HystrixRequestContext功能。
参数 | 描述 | 默认值 |
---|---|---|
requestCache.enabled | HystrixCommand.getCacheKey()是否应与HystrixRequestCache一起使用,以通过请求范围的缓存提供重复数据删除功能。 | 默认值为true |
requestLog.enabled | HystrixCommand执行和事件是否应记录到HystrixRequestLog。 | 默认值为true |
(二)Collapser Properties
下列属性控制HystrixCollapser行为。
参数 | 描述 | 默认值 |
---|---|---|
maxRequestsInBatch | 此属性设置在触发批处理执行之前批处理中允许的最大请求数。 | Integer.MAX_VALUE |
timerDelayInMilliseconds | 此属性设置创建批处理后触发其执行的毫秒数。 | 默认值:10 |
requestCache.enabled | 此属性指示是否为HystrixCollapser.execute()和HystrixCollapser.queue()调用启用请求高速缓存。 | 默认值:true |
(三)ThreadPool Properties
以下属性控制Hystrix命令在其上执行的线程池的行为。
大多数时候,默认值为10的线程会很好(通常可以做得更小)。
参数 | 描述 | 默认值 |
---|---|---|
coreSize | 线程池coreSize | 默认值:10 设置标准:qps*99meantime+breathing room |
maximumSize | 此属性设置最大线程池大小。 这是在不开始拒绝HystrixCommands的情况下可以支持的最大并发数。 请注意,此设置仅在您还设置allowMaximumSizeToDivergeFromCoreSize时才会生效。 | 默认值:10 |
maxQueueSize | 请求等待队列 | 默认值:-1如果使用正数,队列将从SynchronizeQueue改为LinkedBlockingQueue |
queueSizeRejectionThreshold | 此属性设置队列大小拒绝阈值 - 即使未达到maxQueueSize也将发生拒绝的人为最大队列大小。 此属性存在,因为BlockingQueue的maxQueueSize不能动态更改,我们希望允许您动态更改影响拒绝的队列大小。 | 默认值:5 |
keepAliveTimeMinutes | 此属性设置保持活动时间,以分钟为单位。 | 默认值:1 |
allowMaximumSizeToDivergeFromCoreSize | 此属性允许maximumSize的配置生效。 那么该值可以等于或高于coreSize。 设置coreSize <maximumSize会创建一个线程池,该线程池可以支持maximumSize并发,但在相对不活动期间将向系统返回线程。 (以keepAliveTimeInMinutes为准) | 默认值: false |
metrics.rollingStats.timeInMilliseconds | 此属性设置statistical rolling窗口的持续时间(以毫秒为单位)。 这是为线程池保留多长时间。 | 默认值:10000 |
metrics.rollingStats.numBuckets | 此属性设置滚动统计窗口划分的桶数。注意:以下必须为true - “metrics.rollingStats.timeInMilliseconds%metrics.rollingStats.numBuckets == 0” -否则将引发异常。 | 默认值:10 |
四)其他
参数 | 描述 | 默认值 |
---|---|---|
groupKey | 表示所属的group,一个group共用线程池 | 默认值:getClass().getSimpleName(); |
commandKey | 默认值:当前执行方法名 |
参数版本与版本之间不一样,请从源码或者对应版本的文章进行输入了解 上面表格只是参考