欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 前端技术 > javascript >内容正文

javascript

服务降级和服务熔断的区别-凯发k8官方网

发布时间:2024/10/12 javascript 22 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 服务降级和服务熔断的区别_spring cloud 熔断 隔离 服务降级 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

一、hystrix

hystrix主要实现熔断和隔离,限流的功能基本没有,通过fallback实现服务降级和快速失败。核心是hystrixcommand。配置主要包含exection、fallback、circuitbreaker、metric、threadpool等。

二、加入依赖包

org.springframework.cloudspring-cloud-starter-hystrix1.4.7.release com.netflix.hystrixhystrix-javanica

三、在application.yml中加入配置

feign: hystrix: enabled: truehystrix:command:default:execution:timeout:enabled: trueisolation:strategy: threadthread:timeoutinmilliseconds: 8000interruptontimeout: trueinterruptoncancel: truefallback:enabled: trueisolation:semaphore:maxconcurrentrequests: 2

四、在访问接口上实现熔断、隔离和服务降级

@hystrixcommand(commandkey = "circuitbreakercommand",threadpoolkey = "circuitbreakerpool",fallbackmethod = "circuitbreakerfallbackmethod",commandproperties = {@hystrixproperty(name = "circuitbreaker.enabled", value = "true"),@hystrixproperty(name = "circuitbreaker.requestvolumethreshold", value = "2"),@hystrixproperty(name = "circuitbreaker.errorthresholdpercentage", value = "10"),@hystrixproperty(name = "circuitbreaker.sleepwindowinmilliseconds", value = "5000")}, threadpoolproperties = {@hystrixproperty(name = "coresize", value = "5") })@getmapping("/circuitbreakerhello/{id}")public string circuitbreakerhello(@pathvariable("id") int id) {if(id==2){throw new runtimeexception("exception");}return "sucessful execution" ;}public string circuitbreakerfallbackmethod(int id) {return "param " id " is wrong. circuitbreaker has been valid"; }

五、启动类上添加@enablehystrix

@springbootapplication(exclude={datasourceautoconfiguration.class}) @enablediscoveryclient @enablefeignclients @enablehystrix @mapperscan("com.sboot.dao") public class consulconsumerapplication {public static void main(string[] args) {springapplication.run(consulconsumerapplication.class, args);} }

六、测试

1、在浏览器中多次刷新如下地址,导致熔断打开

2、当访问正常得路径http://localhost:8082/circuitbreakerhello/1,也走服务降级:

3、过一会,过了休眠期,再访问正常路径,可以正常访问

总结

以上是凯发k8官方网为你收集整理的服务降级和服务熔断的区别_spring cloud 熔断 隔离 服务降级的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图