首页 > 编程知识 正文

hystrix线程池,kafka工作流程原理

时间:2023-05-06 17:46:38 阅读:135698 作者:1439

hystrix详情大家好,我是酷韩~

一.什么是hystrix?

hystrix是Netlifx的开源容错框架,是雪崩防范的利器,具有服务降级、服务熔断、隔离依赖、监控(Hystrix Dashboard )等功能。

二.功能点详解

1.服务降级

使用场景:

例如,双十一出现在购物中,啊,被压扁了。 APP秒杀了某个商品,在网上开了个小差,请稍后再试。

使用方法:

(1)添加pom文件

ependencygroupidorg.spring framework.cloud/groupidartifactidspring-cloud-starter-Netflix-hy strix/artifact id/aartid

(3) RestTemplate在请求数据中添加@ hystrixcommand (fallback method=“fallback”) ) ) ) ) ) ) ) ) ) ) ) ) ) )

(4) fallback是指方法在方法中保持参数和返回值相同。

(5)对)3)4)的例子

@getmapping(/user/test ) ) hystrixcommand ) fallbackmethod='fallback2) ) publicstringtest ) @requestparam ) ) nnest stringstr=rest template.getforobject (http://127.0.0.133608081/rw/user/test ),String.class ); return str; }私有故障2 (integer number ) { return太拥挤了。 稍后2 ); 在RestTemplate中调用的其他服务。 其他服务是为普通接口编写的。 如果任何number输入不为1,则请求服务http://127.0.0.133608081/rw/user/test,正常调用返回正常结果,如果服务太短或请求时间太长,则返回fallback2

(6)修改超时时间

默认超时时间为1000ms,以下可设定的单位为ms

@ hystrixcommand (fallback method=' fallback2',command properties={ @ hystrixproperty (name=' execution.isolation.)

hy strix : command : default : execution : isolation : thread : timeoutinmilliseconds 33603000 #请求命令的执行超时时间

hy strix : command 3360 default : execution : isolation : thread : timeoutinmilliseconds 33603000 # 命令执行请求超时时间test : execution : isolation : thread : timeoutinmilliseconds :1000 #命令执行请求超时时间由某个方法单独设置

(1)向各hystrixCommand提供线程池,自动实现依存分离,以便即使线程池已满也不影响其他进程。

2.依赖隔离:

(1)服务高并发时,会发生进程阻塞,当前线程无法使用,逐渐所有线程被阻塞,服务器会发生雪崩。 此时,直接熔断整个服务。 不是等到服务超时。

)2)断路器全开时)在一定时间内达到一定次数不能呼叫,多次监测无恢复迹象的断路器完全开启,下一次请求不计入该服务

半开:短时间内有恢复迹象的断路器将部分请求发送到该服务器,正常呼叫时断路器关闭

关闭:如果服务始终在正常状态下正常调用

)3)服务熔断使用:

@ hystrixcommand (fallback method=' fallback2',command properties={/* @ hystrixproperty ) name=' execution.isolation */@ hystrixproperty (name=' circuit breaker.enabled ',value='true ' ) hystrixproperty ) (naaabled ),value=' tred @ hystrixproperty (name=' circuit breaker.sleepwindowinmilliseconds ',value='10000 ',@ hystrixproperty (name=' coce ) value='60 ',}@getmapping(/user/test ) (publicstringtest ) @requestparam ) ) number ) ) Integer number ) if ) NUE ) if ) } resttemplateresttemplate=newresttemplate (; stringstr=rest template.getforobject (http://127.0.0.133608081/rw/user/test ),String.class ); return str; } circuit breaker.enabled : true开启熔断预设开启

circuit breaker.requestvolumethreshold :在配置时间窗口中达到此数量的失败后发生短路。 默认20个

circuit breaker.sleepwindowinmilliseconds 3360短路后多久尝试恢复,默认5s

circuit breaker.errorthresholdpercentage :错误率阈值在达到该阈值后开始短路。 默认为50%

3.服务熔断:

1 .添加POM文件

ependencygroupidorg.spring framework.cloud/groupidartifactidspring-cloud-starter-Netflix-hy strix-dashboard/adaboard ependencydependencygroupidorg.spring framework.boot/groupidartifactidspring-boot-starter-actuator/artifact

例如我使用的当地

localhost:8082/hystrix访问

点击Monitor Stream进入

Circuit的保险丝开启状态默认为Closed关闭,发生多个故障时发生Open,打开保险丝再次访问时直接变为fallback,在服务逐渐恢复后再变为Closed。

其中有一个博客可以解决访问问题。 https://blog.csdn.net/hjq _ ku/article/details/89516530

4.服务熔断监控

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。