Hystrix


Hystrix翻译成中文是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与Hystrix本身的功能不谋而合,因此Netflix团队将该框架命名为Hystrix。所以,Hystrix的功能便是自我保护机制,我们将其称之为断路器。

在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是Hystrix需要做的事情。Hystrix提供了熔断、隔离、Fallback、cache、监控等功能,能够在一个、或多个依赖同时出现问题时保证系统依然可用。


在Ribbon项目中开启Hystrix

在Ribbon项目中,我们需要在pom.xml文件中导入spring-cloud-starter-netflix-hystrix依赖。

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

再在Ribbon项目的启动类中,增加@EnableHystrix注解开启Hystrix。

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class RibbonApplication {public static void main(String[] args) {SpringApplication.run(RibbonApplication.class, args);}@Bean@LoadBalancedRestTemplate restTemplate() {return new RestTemplate();}
}

再修改Service中的welcomeService方法,增加@HystrixCommand(fallbackMethod = “error”)注解,指定好断路时调用的方法。并新增error方法。

@HystrixCommand(fallbackMethod = "error")public String welcomeService(String name) {return restTemplate.getForObject("http://client/welcome?name="+name,String.class);}public String error(String name) {return "哎呀呀,"+name+",出错了呀!";}

最后,我们依次启动Service,Client,Ribbon项目,访问http://localhost:9003/welcome?name=Cheng,出现:

Cheng 欢迎您,这里有一些话想对你说: Hey! This is Spring Cloud

再关闭Client项目,刷新页面,出现:

哎呀呀,Cheng,出错了呀!

由此我们可以知道,当访问Client(程序调用API接口)出错时,将会快速执行失败,返回出错时,希望用户看到的内容。而不是要用户等待到访问超时,这很好的控制了容器的线程阻塞。


在Feign项目中开启Hystrix

在Feign中,已经集成了Hystrix断路器,在Spring Cloud Dalston及以上的版本中,默认是不启动的,我们需要在properties中,打开它。

Dalston及以上的版本中,打开断路器
feign.hystrix.enabled=true

而后,我们在WelcomeInterface中,新增fallback,并指定其出错时,调用的方法。

@FeignClient(value = "client",fallback = WelcomeError.class)
public interface WelcomeInterface {@RequestMapping(value = "/welcome",method = RequestMethod.GET)String welcomeClientOne(@RequestParam(value = "name") String name);
}

新增WelcomeError类,指定出错时调用的内容:

@Component
public class WelcomeError implements WelcomeInterface {@Overridepublic String welcomeClientOne(String name) {return "哎呀呀,不好意思"+name+",出错了呀!";}
}

启动Server、Client、Feign项目,访问http://localhost:9004/welcome?name=Cheng,出现:

Cheng 欢迎您,这里有一些话想对你说: Hey! This is Spring Cloud

关闭Client项目,刷新页面(有时候可能因为缓存问题,没有出现,清除缓存就好了),出现:

哎呀呀,不好意思Cheng,出错了呀!

同样证明了,我们的断路器启用了。


Hystrix Dashboard——Hystrix仪表盘

Spring Cloud的断路器,还有个仪表盘功能,可以直观的看到程序当前的状态。
Ribbon和Feign项目都一样,所以我就在Feign项目中,进行演示了。
在pom.xml文件中。引入spring-cloud-starter-netflix-hystrix-dashboard依赖。

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

而后,在项目的启动类中,加入@EnableHystrixDashboard注解,打开断路器仪表盘功能。并添加ServletRegistrationBean,因为在SpringBoot 2.0及以上的版本中, Springboot的默认路径不是 “/hystrix.stream”,所以我们手动加上即可。

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
public class FeignApplication {public static void main(String[] args) {SpringApplication.run(FeignApplication.class, args);}@Beanpublic ServletRegistrationBean getServlet(){HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);registrationBean.setLoadOnStartup(1);registrationBean.addUrlMappings("/hystrix.stream");registrationBean.setName("HystrixMetricsStreamServlet");registrationBean.setName("HystrixMetricsStreamServlet");return registrationBean;}
}

再启动项目,访问http://localhost:9004/hystrix,出现下图界面,红色圈的地方为位置路径,需指定,蓝色圈的地方为名称,可以随意填写。

我们再访问http://localhost:9004/welcome?name=Cheng,此时监控界面会出现

Spring Boot+Spring Cloud基础入门(五)断路器——Hystrix相关推荐

  1. Spring Boot+Spring Cloud基础入门(一)简单介绍

    转自:https://blog.csdn.net/mingwei_cheng/article/details/80939833 在经历了毕业的摧残后,终于又有时间来更新博客了,毕业设计项目是写了一个基 ...

  2. 19年8月 字母哥 第一章 spring boot 2.x基础及概念入门 这里全部看完了 热部署没出来 第二章在前面2页 用热点公司网不行

    http://springboot.zimug.com/1233100   文档 http://www.zimug.com/page/5     字母哥个人博客 11111 第一章 spring bo ...

  3. Spring Boot 2.x基础教程:Swagger静态API文档的生成

    点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 作者 | 翟永超 来源 | didispace.com/spring-boot-learni ...

  4. Spring Boot学习笔记-基础(2)

    Spring Boot学习笔记-基础(2) Spring Boot 优点: – 快速创建独立运行的Spring项目以及与主流框架集成 – 使用嵌入式的Servlet容器,应用无需打成WAR包 – st ...

  5. Spring Boot 2.x基础教程:Swagger静态文档的生成

    前言 通过之前的两篇关于Swagger入门以及具体使用细节的介绍之后,我们已经能够轻松地为Spring MVC的Web项目自动构建出API文档了.如果您还不熟悉这块,可以先阅读: Spring Boo ...

  6. Spring Boot 2.x基础教程:使用Elastic Job实现定时任务

    上一篇,我们介绍了如何使用Spring Boot自带的@Scheduled注解实现定时任务(https://blog.didispace.com/spring-boot-learning-2-7-1/ ...

  7. Spring Boot 2.x基础教程:使用集中式缓存Redis

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 之前我们介绍了两种进程内缓存的用法,包括Spring B ...

  8. Spring Boot 2.x基础教程:MyBatis的多数据源配置

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 前两天,我们已经介绍了关于JdbcTemplate的多数 ...

  9. Spring Boot 2.x基础教程:使用国产数据库连接池Druid

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 翟永超 来源 | http://blog.di ...

  10. Spring Boot 2.x基础教程:使用JdbcTemplate访问MySQL数据库

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 翟永超 来源 | didispace.com/ ...

最新文章

  1. AI做不了“真”3D图像?试试Google的新生成模型
  2. 如何在网站上同步查看BCH数据
  3. 交叉编译VIM并移植到ARM嵌入式Linux系统
  4. android 47 service绑定
  5. Java-Runoob:Java Stream、File、IO
  6. zabbix-02-CentOS7.4安装zabbix4.0
  7. java 1.8stream_java jdk1.8 使用stream流进行list 分组归类操作
  8. 当使用easyui时,表单的onchange事件失效
  9. 蓝桥杯第八届省赛JAVA真题----最大公共子串
  10. PCL Examples
  11. Jsp链接传值中文乱码问题解决
  12. 固态硬盘与普通硬盘有哪些区别?
  13. 技巧8——linux假死现象要知道
  14. Python学习笔记-2017.5.4thon学习笔记-2017.8.16
  15. 如何将企业微信好友设置为外部联系人
  16. C++的O2、O3到底是个什么鬼
  17. java 1st 2nd 3rd 4th_1st(3rd)
  18. unity3d发布webgl手机测试流程
  19. bzoj3998/洛谷3975 [TJOI2015]弦论 (后缀自动机)
  20. 第一章: 微型计算机组成结构

热门文章

  1. 经典北京话语录~~~~
  2. Mermermaid
  3. 成员函数指针与高性能的C++委托 (Member Function Pointers and the Fastest Possible C++ Delegates)...
  4. 最新,2023年ABC中国大学排名发布
  5. 想兼职php讲师怎么找,如何成为一名兼职讲师?
  6. c语言实现莫尔斯编码,c语言编写莫尔斯码,帮帮忙啊,速回
  7. 基于零空间投影(NSB)行为法的多智能体控制
  8. git global
  9. js复制链接到剪贴板
  10. 循环矩阵的傅里叶对角化