项目源码github地址

  • 什么是Hystrix
  • 快速入门
    • Hystrix项目基本配置
  • Hystrix仪表盘
    • 基本配置
    • 仪表盘的使用

什么是Hystrix

还以商城为例:

  • 单点服务

在单点部署的商场服务项目中,如果库存模块发生错误,则会使整个商城陷入长时间的等待,或者不可用状态。

  • 分布式服务

在分布式服务中,如果库存模块不可用时,将启动熔断机制,在指定时间内,将返回错误信息,并且保证整体服务的可以用性。

快速入门

spring-cloud创建spring-cloud-Hystrix模块项目,如下图:

Hystrix项目基本配置

  • pom.xml中引入Hystrixjar包

<dependencies><!-- spring boot web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 生产环境时监视和管理应用程序 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--hystrix--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency>
</dependencies>
  • application.properties配置

server.port= 8563
spring.application.name=hystrix#配置日志级别
logging.level.org.springframework.cloud.netflix.hystrix = debug
  • HystrixApplication.java中加入@EnableCircuitBreaker注解

/*** @author : R&M www.rmworking.com/blog*         2018/9/26 10:38*         spring-cloud*         org.qnloft.hystrix*/
@SpringBootApplication
@EnableCircuitBreaker
public class HystrixApplication{/*** 注入发起rest请求的bean* @return*/@Bean@LoadBalancedRestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(HystrixApplication.class, args);}
}
  • Hystrix在项目中的使用示例

我们使用注入的restTemplatebean来请求 spring-web 项目的index接口

service层代码如下:


/*** @author : R&M www.rmworking.com/blog*         2018/9/26 15:30*         spring-cloud*         org.qnloft.hystrix.service*/
@Service
public class RestWebService {@Autowiredprivate RestTemplate restTemplate;@HystrixCommand(fallbackMethod = "helloFallback" ,commandKey = "helloKey")public String restHelloWorldService() {return restTemplate.getForEntity("http://127.0.0.1:8661/index", String.class).getBody();}public String helloFallback() {return "访问web服务出错";}
}

小伙伴们应该都注意到了@HystrixCommand这个注解,这个就是在方法中增加熔断机制,fallbackMethod参数中的值是触发熔断后指定调用的方法,commandKey是为这个熔断接口的别名,在仪表盘界面会显示这个名称。

controller层代码如下:


/*** @author : R&M www.rmworking.com/blog*         2018/9/26 15:30*         spring-cloud*         org.qnloft.hystrix.controller*/
@RestController
public class RestWebController {@Autowiredprivate RestWebService restWebService;@RequestMapping(value = "hello" , method = RequestMethod.GET)public String getHelloWorld(){return restWebService.restHelloWorldService();}
}

如果不出意外,启动spring-web项目和spring-cloud-Hystrix项目,然后访问:http://127.0.0.1:8563/hello

那如何才能模拟熔断呢?有两种方式:最简单暴力的方式就是停止spring-web项目;另外一种温柔的方式是设置一个线程休眠3秒,因为Hystrix请求超时时间默认是2秒,这样就可以触发熔断机制了。

修改RestWebServicerestHelloWorldService方法,代码如下:


@HystrixCommand(fallbackMethod = "helloFallback")
public String restHelloWorldService() {// 如果让线程等待3s会发生什么呢??try {Thread.sleep(3000);} catch (InterruptedException e) {System.out.println(e.getMessage());}return restTemplate.getForEntity("http://127.0.0.1:8661/index", String.class).getBody();
}

接下来我们再次访问:http://127.0.0.1:8563/hello

这个结果就是helloFallback方法中返回值。这样就成功触发了熔断机制。

Hystrix仪表盘

基本配置

  • pom.xml中加入dashboard的jar包:

<!-- Hystrix 仪表盘 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
  • HystrixApplication.java中加入@EnableHystrixDashboard注解,并且加入如下代码:
/*** 将HystrixMetricsStreamServlet注册到Servlet,否则会出现访问hystrix.stream 404问题* @return*/
@Bean
public ServletRegistrationBean<HystrixMetricsStreamServlet> getHystrixStreamServlet(){HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<>(streamServlet);registrationBean.setLoadOnStartup(1);registrationBean.addUrlMappings("/hystrix.stream");registrationBean.setName("HystrixMetricsStreamServlet");return registrationBean;
}

将HystrixMetricsStreamServlet注册到Servlet,否则会出现访问hystrix.stream 404问题

  • 访问:http://127.0.0.1:8563/hystrix

仪表盘的使用

当出现如下页面时,则证明配置成功。

首先Hystrix Dashboard提供三种不同的监控方式:

  • 默认集群监控:http://turbine-hostname:port/turbine.stream
  • 指定集群监控:http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
  • 单体应用监控:http://hystrix-app:port/hystrix.stream

因为我们没有集群环境,暂时先使用单体应用监控。在图中红框中输入http://127.0.0.1:8563/hystrix.stream ,点击页面最下面的Monitor Stream按钮,之后进入监控界面,如下图:

此时Hystrix Dashboard已经处于监控状态,小伙伴可以看一下CPU占用率,idea控制台也会有显示:

现在我们访问一下:http://127.0.0.1:8563/hello 这个地址,再看一下监控台会出现变化

这是监控到接口helloKey被访问。更多功能小伙伴们根据工作需求,自行探索。

转载于:https://www.cnblogs.com/qnloft/p/qing-ning-kai-cheSpring-Cloud-qi--duan-lu-qi-Hystr.html

青柠开车Spring Cloud(七) —— 断路器 Hystrix相关推荐

  1. 青柠开车Spring Cloud(六) —— Spring Cloud Gateway与zuul使用对比

    青柠开车Spring cloud(一) -- 生态系统以及在企业项目中的基础架构图     (1-7),有时间可以看看 项目源码github地址 补充 Gateway简介 快速入门 Gateway 项 ...

  2. 青柠开车Spring Cloud(三) —— Spring cloud Eureka

    项目源码github地址 什么是Eureka 快速入门 创建spring boot项目 引入spring cloud配置 引入Eureka依赖jar包 application.properties的配 ...

  3. 青柠开车Spring Cloud(五) —— spring cloud的窗口zuul(路由和过滤器)

    项目源码github地址 什么是zuul 快速入门 zuul项目基本配置 将zuul加入eureka-server中,实现高可用 什么是zuul zuul在之前我举得例子中,担任的是警察的角色.准确的 ...

  4. spring cloud 熔断_Spring Cloud 熔断器/断路器 Hystrix

    在微服务架构中,业务会被拆分成一个个服务,服务间可以彼此调用.为了保证服务的高可用性,单个服务通常会被集群部署,但是由于网络等原因,服务并不能保证100%可用.如果某个服务出现了问题,那么调用这个服务 ...

  5. Spring Cloud入门教程-Hystrix断路器实现容错和降级

    简介 Spring cloud提供了Hystrix容错库用以在服务不可用时,对配置了断路器的方法实行降级策略,临时调用备用方法.这篇文章将创建一个产品微服务,注册到eureka服务注册中心,然后我们使 ...

  6. 【夯实Spring Cloud】Spring Cloud中使用Hystrix实现断路器原理详解(上)

    本文属于[夯实Spring Cloud]系列文章,该系列旨在用通俗易懂的语言,带大家了解和学习Spring Cloud技术,希望能给读者带来一些干货.系列目录如下: [夯实Spring Cloud]D ...

  7. 10 在Spring Cloud中使用Hystrix

    Hystrix主要用于保护调用服务的一方,如果被调用的服务发生故障,符合一定条件,就会开启断路器对调用的程序进行隔离. 1.准备测试程序 在进行Spring Cloud整合Hystrix之前,我们先准 ...

  8. Spring Cloud (Eureka,Feign,Hystrix整合)

    Spring Cloud(Eureka,Feign,Hystrix整合) Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代 ...

  9. Spring Cloud(七) GateWay 服务化和过滤器

    前文回顾: Spring Cloud(一)Eureka Server-单体及集群搭建 Spring Cloud(二) 配置Eureka Client Spring Cloud(三) 熔断器Hystri ...

最新文章

  1. 正则表达式了解和使用
  2. 二叉树的层次遍历 II[LeetCode-107]
  3. canvas arcTo()用法详解 – CodePlayer
  4. Secure CRT 自动记录日志和时间戳功能配置
  5. Linux环境中Qt程序的手工发布
  6. 【JAVA基础篇】Socket编程
  7. 大学学计算机专业好吗,现在上大学学计算机专业好吗?好找工作吗?听他人说计算机学不了什么?学的人也多,...
  8. RecyclerView 数据预取
  9. lua编译器和ide
  10. VB 泛型 T 应用
  11. 统计学习(三):假设检验与 p-values
  12. python list的+=操作
  13. (练打字微信小程序)把手机当成小型计算机练习打字
  14. 武汉科技大学计算机实验预约系统,实验室与设备管理处
  15. c语言字母排列组合的实现,c语言中一种典型的排列组合算法
  16. 奇迹,我拿什么征服你?
  17. 基于MES系统的追溯管理功能,没你想的那么复杂
  18. 关于TLC2543不常见问题
  19. H5C3动画实例,通过基于jQuery的fullpage插件完成
  20. AI 图片截取、ffmpeg使用及安装, anaconda环境,图片标注(labelme),模型训练(yolov5),CUDA+Pytorch安装及版本相关问题

热门文章

  1. htc+m7+android系统+电源,htc one m7 4.4系统获取root权限教程(安卓4.4专用)
  2. 赶紧学会!开发者愚人节怎么写代码。。。
  3. 【华为OD机试真题 JS】出错的或电路
  4. 网络工程的工作任务课堂作业
  5. 微小宝公众号排行榜_微小说免费阅读网公众号大全推荐资源网
  6. 第十一课 区块链常用共识算法介绍
  7. 英语专业,不想做老师,还能做什么工作?
  8. 能净化空气的PC电源,配置也很不错,艾湃电竞AP-550Ti体验
  9. thinkphp ajax 跨域请求 Access-Control-Allow-Origin 完美解决
  10. 服务器怎么和本地共享文件夹同步,云服务器如何共享文件夹同步