當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
服务降级和服务熔断的区别_Spring Cloud 熔断 隔离 服务降级
生活随笔
收集整理的這篇文章主要介紹了
服务降级和服务熔断的区别_Spring Cloud 熔断 隔离 服务降级
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、hystrix
hystrix主要實現(xiàn)熔斷和隔離,限流的功能基本沒有,通過fallback實現(xiàn)服務(wù)降級和快速失敗。核心是HystrixCommand。配置主要包含exection、fallback、circuitBreaker、metric、threadPool等。
二、加入依賴包
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId><version>1.4.7.RELEASE</version> </dependency> <dependency><groupId>com.netflix.hystrix</groupId><artifactId>hystrix-javanica</artifactId> </dependency>三、在application.yml中加入配置
feign: hystrix: enabled: truehystrix:command:default:execution:timeout:enabled: trueisolation:strategy: THREADthread:timeoutInMilliseconds: 8000interruptOnTimeout: trueinterruptOnCancel: truefallback:enabled: trueisolation:semaphore:maxConcurrentRequests: 2四、在訪問接口上實現(xiàn)熔斷、隔離和服務(wù)降級
@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、在瀏覽器中多次刷新如下地址,導(dǎo)致熔斷打開
2、當訪問正常得路徑http://localhost:8082/circuitBreakerHello/1,也走服務(wù)降級:
3、過一會,過了休眠期,再訪問正常路徑,可以正常訪問
總結(jié)
以上是生活随笔為你收集整理的服务降级和服务熔断的区别_Spring Cloud 熔断 隔离 服务降级的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 客厅长6米宽4米如何摆放6米沙发
- 下一篇: 订书机钉子卡住了怎么办?