一、hystrix

hystrix主要实现熔断和隔离,限流的功能基本没有,通过fallback实现服务降级和快速失败。核心是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

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

@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、过一会,过了休眠期,再访问正常路径,可以正常访问

服务降级和服务熔断的区别_Spring Cloud 熔断 隔离 服务降级相关推荐

  1. java微服务项目简历_Spring Cloud及微服务简介

    最近在看微服务编排的东西,看到一篇入门博客,私以为不错,再次分享下:https://blog.csdn.net/w05980598/article/details/79007194 什么是微服务 微服 ...

  2. feign调用多个服务_spring cloud各个微服务之间如何相互调用(Feign、Feign带token访问服务接口)...

    1.首先先看什么是Feign. 2.若其他服务的接口未做权限处理,参照上文第1点的博文即可. 3.若其他服务的接口做了权限的处理(例如OAuth 2)时该如何访问? a.有做权限处理的服务接口直接调用 ...

  3. eureka自我保护时间_spring cloud中微服务之间的调用以及eureka的自我保护机制详解...

    上篇讲了spring cloud注册中心及客户端的注册,所以这篇主要讲一下服务和服务之间是怎样调用的 基于上一篇的搭建我又自己搭建了一个客户端微服务: 所以现在有两个微服务,我们所实现的就是微服务1和 ...

  4. springcloud上传文件_Spring Cloud实战:服务链路追踪Spring Cloud Sleuth

    推荐阅读: Spring全家桶笔记:Spring+Spring Boot+Spring Cloud+Spring MVC 一个SpringBoot问题就干趴下了?我却凭着这份PDF文档吊打面试官. 前 ...

  5. feign一个接口多个方法_spring cloud 建一个服务消费者client-feign(最好用这种方式)...

    Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解.Feign默认集 ...

  6. Spring Cloud构建微服务架构:服务容错保护(Hystrix服务降级)【Dalston版】

    前言 在微服务架构中,我们将系统拆分成了一个个的服务单元,各单元应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服 ...

  7. Spring Cloud构建微服务架构:服务容错保护(Hystrix断路器)【Dalston版】

    前言 在前两篇<Spring Cloud构建微服务架构:服务容错保护(Hystrix服务降级)>和<Spring Cloud构建微服务架构:服务容错保护(Hystrix依赖隔离)&g ...

  8. Spring Cloud构建微服务架构:Hystrix监控数据聚合【Dalston版】

    上一篇我们介绍了使用Hystrix Dashboard来展示Hystrix用于熔断的各项度量指标.通过Hystrix Dashboard,我们可以方便的查看服务实例的综合情况,比如:服务调用次数.服务 ...

  9. 《深入理解 Spring Cloud 与微服务构建》第十一章 服务网关

    <深入理解 Spring Cloud 与微服务构建>第十一章 服务网关 文章目录 <深入理解 Spring Cloud 与微服务构建>第十一章 服务网关 一.服务网关简介 二. ...

最新文章

  1. 前端编程提高之旅(五)----写给大家看的css书
  2. 请教做个图标扇动提示》在线等
  3. 北邮dsp matlab实验,北京邮电大学《数字信号处理》门爱东-DSP实验.pdf
  4. 【转】VS TFS源码分析软件PATFS使用方法一:配置团队项目
  5. 一个 js 中值传递和引用传递的坑。
  6. c语言~991|4等于多少,复习C语言9-helloworld3000-ChinaUnix博客
  7. Sublime和LaTeX支持
  8. Spring:pom.xml中引入依赖发红解决方案
  9. openGL细分着色器详解
  10. Spring之refresh的12个步骤
  11. cilium系列之四:使用阿里云vpc作为ipam
  12. 深入解析SpringBoot启动原理
  13. Dijkstra迪杰斯特算法(C++版本和JAVA版本)
  14. python:在指定范围内按学号随机生成座位顺序,并分行输出
  15. 图像修复 : ICCV 2021 基于条件纹理和结构并行生成的图像修复【翻译】
  16. 二进制补码是如何把减法转变为加法的
  17. ArcGIS教程:创建饼图
  18. 多WAN路由器配置 实现简述
  19. 正则表达式 (js)
  20. python支持复数类型以下什么说法是错误的_python测试开发面试题1

热门文章

  1. telnet 以及 win10 开放端口
  2. #再一次用construct2做游戏
  3. 一篇文章教你弄懂SpringMvc中的HttpMessageConverter
  4. npm run serve后台运行的命令写法
  5. k8s实现jenkins master-slave分布式构建方案
  6. k8s pod应用升级回滚(set image/rollout)和弹性伸缩(scale)示例
  7. win10使用虚拟光驱安装vcenter6.7
  8. linux通过tar包安装docker
  9. Flink快速入门wordcount示例(scala版)
  10. python3.6安装tesserocr