spring cloud微服务快速教程之(四)熔断器(Hystrix)及其工具(Dashboard、Turbine)
0-为什么需要熔断器
在分布式系统中,各个服务相互调用相互依赖,如果某个服务挂了,很可能导致其他调用它的一连串服务也挂掉或者在不断等待中耗尽服务器资源,这种现象称之为雪崩效应;
未来防止系统雪崩,熔断机制必不可少,就是当一个服务挂掉后,调用它的服务能快速熔断,不再耗费资源,快速失败并提供回退方案;
【Hystrix】:是spring cloud的熔断器组件,提供了熔断器功能,能够阻止联动故障,并提供故障的解决方案,提供系统弹性;
【Hystrix Dashboard】:是熔断器的监控面板,通过它,能直观的了解熔断器的状况;
【Turbine】: 在Dashboard中,我们要输入一个一个单独的服务地址进行监控和了解;那么多服务,一个一个输入那不是累死人,有没有一个工具能把众多分散的微服务熔断器监控状况聚合到一起,使得我们在一个Dashboard就能了解众多服务的熔断器监控状况呢,有,这个工具就是Turbine;它的作用就是聚合众多微服务的hystrix监控数据到一起,使得我们只需要在一个Dashboard就能了解所有;
一、使用hystrix
1.1、添加依赖
- <!-- 断路器 hystrix-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
- </dependency>
1.2、配置文件中开启熔断器开关
- #开启熔断器开关
- feign:
- hystrix:
- enabled: true
1.3、启动类中增加 @EnableHystrix 注解和bean
- @SpringBootApplication
- @EnableEurekaClient
- @EnableHystrix
- @EnableFeignClients
- public class application
- {
- public static void main(String[] args)
- {
- SpringApplication.run(application.class);
- @Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
- } }
1.4、增加熔断器类,实现Feign的接口
- package com.anson.service.feign;
- import org.springframework.stereotype.Component;
- import org.springframework.web.bind.annotation.RequestMapping;
- /**
- * @description: 熔断器类
- * @author: anson
- * @Date: 2020/1/7 11:24
- */
- @Component
- public class FUserServiceHystrix implements FUserService
- {
- @RequestMapping("/user/hello")
- @Override
- public String hello()
- {
- return "对不起,user服务不可达,请稍后再试!";
- }
- }
1.5、增加熔断器配置类FeignConfig
- package com.anson.config;
- import feign.Retryer;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import java.util.concurrent.TimeUnit;
- @Configuration
- public class FeignConfig {
- @Bean
- public Retryer feignRetryer() {
- /*
- * 参数说明:
- * 第一个> 重试间隔为100毫秒
- * 第二个> 最大重试时间为1秒
- * 第三个> 最大重试次数为5次
- */
- return new Retryer.Default(100, TimeUnit.SECONDS.toMillis(1), 5);
- }
- }
1.6、在Feign接口注解中增加fallback,指向熔断器类
- package com.anson.service.feign;
- import org.springframework.cloud.openfeign.FeignClient;
- import org.springframework.web.bind.annotation.RequestMapping;
- @FeignClient(name = "user",configuration = FeignConfig.class,fallback = FUserServiceHystrix.class)
public interface FUserService
{
@RequestMapping("/user/hello")
public String hello();
}
1.7、运行测试
user服务正常时:
user服务关闭时:
---------------------------华丽丽的分割线-------------------------------------------------------
二、Hystrix Dashboard的使用
2.1、熔断器加了,那么我们要监控和查看hystrix的运行状态,这时候Dashboard上场;
2.2、添加依赖:
- <!-- dashboard -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
2.3、启动类添加注解 @EnableHystrixDashboard
2.4、完成,运行测试,打开,http://localhost:8767/hystrix
在里面填入要监控的微服务地址 +/actuator/histrix.stream,如:http://localhost:8768/actuator/histrix.stream,title也填入,点击按钮就可以看到结果了:如图:
可以看到我们加入的熔断器的运行状态
----------------------------------------------华丽丽的分割线-----------------------------------------------------------------
三、Turbine 聚合监控数据
3.1、上面Dashboard中,我们输入http://localhost:8767/actuator/histrix.stream,它只是现实端口8767这个微服务实例的监控数据而已,要看其他的,还得重新输入,那能不能有个工具将所有微服务的监控数据都聚合到一起,显示到Dashboard中呢,这个工具就是Turbine
3.2、新建一个模块,添加相关依赖:
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
3,.3、修改配置文件,其中app-config是要监控的服务名,多个用逗号分隔;
- server:
- port: 8768
- spring:
- application:
- name: turbine
- eureka:
- client:
- serviceUrl:
- defaultZone: http://localhost:8761/eureka/
- instance:
- prefer-ip-address: true
- turbine:
- combine-host-port: true
- app-config: order
- cluster-name-expression: new String("default")
- instanceUrlSuffix: actuator/hystrix.stream
- aggregator:
- cluster-config: default
3.4、在启动类中添加注解@EnableTurbine;
- package com.anson;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
- import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
- import org.springframework.cloud.netflix.turbine.EnableTurbine;
- /**
- * @description: TODO
- * @author: anson
- * @Date: 2020/1/6 14:27
- */
- @SpringBootApplication
- @EnableEurekaClient
- @EnableTurbine
- @EnableHystrixDashboard
- public class application
- {
- public static void main(String[] args)
- {
- SpringApplication.run(application.class);
- }
- }
完成,启动运行后,他就会将所有配置文件中添加进来的服务全部的熔断器监控数据聚合到一起了,
在任意项目的Dashboard面板中填入http://turbine-hostname:port/turbine.stream 的地址样式,就能看到所有的监控数据了
,如 : http://localhost:8768/turbine.stream
我们只加了一个熔断器,你可以多加几个看看效果;
好了,熔断器hystrix简单讲解到着,,更深入的后续再详聊,GIT源码后续再放出
spring cloud微服务快速教程之(四)熔断器(Hystrix)及其工具(Dashboard、Turbine)的更多相关文章
- spring cloud微服务快速教程之(七) Spring Cloud Alibaba--nacos(一)、服务注册发现
0.前言 什么是Spring Cloud Alibaba? Spring Cloud Alibaba 是阿里开源的,致力于提供微服务开发的一站式解决方案.此项目包含开发分布式应用微服务的必需组件,方便 ...
- spring cloud微服务快速教程之(十四)spring cloud feign使用okhttp3--以及feign调用参数丢失的说明
0-前言 spring cloud feign 默认使用httpclient,需要okhttp3的可以进行切换 当然,其实两者性能目前差别不大,差别较大的是很早之前的版本,所以,喜欢哪个自己选择: 1 ...
- spring cloud微服务快速教程之(十一) Sleuth(zipkin) 服务链路追踪
0.前言 微服务架构上众多微服务通过REST调用,可能需要很多个服务协同才能完成一个接口功能,如果链路上任何一个服务出现问题或者网络超时,都会形成导致接口调用失败.随着业务的不断扩张,服务之间互相调用 ...
- spring cloud微服务快速教程之(五) ZUUL API网关中心
0-前言 我们一个个微服务构建好了,外部的应用如何来访问内部各种各样的微服务呢?在微服务架构中,后端服务往往不直接开放给调用端,而是通过一个API网关根据请求的url,路由到相应的服务.当添加API网 ...
- spring cloud微服务快速教程之(十) gateway 服务网关
0.前言 gateway是spring的二代网关, 作为Netflix Zuul的替代者,是异步非阻塞网关 ,ZUUL2也是异步非阻塞的,但未纳入spring cloud整合计划 基于WebFlux ...
- spring cloud微服务快速教程之(九) Spring Cloud Alibaba--sentinel-限流、熔断降级
0.前言 sentinel的限流.降级功能强大,可以在控制面板中任意制定规则,然后推送到微服务中: 可以根据URL单独制定规则,也可以根据资源名批量制定规则: 需要注意的地方是:1.GITHUB文件在 ...
- spring cloud微服务快速教程之(八) Spring Cloud Alibaba--nacos(二)、配置中心
0-前言 上一篇我们介绍了nacos作为服务注册发现组件的功能,nacos还具有配置中心的功能,而且支持热加载: 在此之前,配置中心有Spring Cloud Config,实际上,用这个有很多风险和 ...
- spring cloud微服务快速教程之(三)声明式访问Feign、负载均衡Ribbon
0-前言 eureka实际上已经集成了负载均衡调度框架Ribbon: 我们有了各个微服务了,那怎么来调用他们呢,一种方法是可以使用 RestTemplate(如:String str= restTem ...
- spring cloud微服务快速教程之(六) 应用监控 spring boot admin
0-前言 当我们发布了微服务后,我们希望对各个应用的各个运行状况进行一个监控:这个时候spring boot admin,就出场了: spring boot admin:是一个监控和管理spring ...
随机推荐
- apply、call、bind方法调用
---恢复内容开始--- 首先这三个方法的作用都是用来改变this的值,而this的值一般有几种情况. 1.函数作为一个对象的一个方法来调用,此时this的值指向对象. var a={ v:0; f: ...
- poj 1514 Metal Cutting (dfs+多边形切割)
1514 -- Metal Cutting 一道类似于半平面交的题. 题意相当简单,给出一块矩形以及最后被切出来的的多边形各个顶点的位置.每次切割必须从一端切到另一端,问切出多边形最少要切多长的距离. ...
- redux【react】
首先介绍一下redux就是Flux的一种进阶实现.它是一个应用数据流框架,主要作用应用状态的管理 一.设计思想: (1).web应用就是一个状态机,视图和状态一一对应 (2).所有的状态保存在一个对象 ...
- CentOS 7 安装 LNMP 环境(PHP7 + MySQL5.7 + Nginx1.10)
记录下在CentOS 7 安装 LNMP 环境(PHP7 + MySQL5.7 + Nginx1.10)过程笔记. 工具 VMware版本号 : 12.0.0 CentOS版本 : 7.0 一.修改 ...
- 关于 FormData 和 URLSearchParams
一.FormData FormData 接口提供了一种表示表单数据的键值对的构造方式,经过它的数据可以使用 XMLHttpRequest.send() 方法送出,本接口和此方法都相当简单直接.如果送出 ...
- 2018-9-20-断点调试-Windows-源代码
title author date CreateTime categories 断点调试 Windows 源代码 lindexi 2018-09-20 17:37:55 +0800 2018-05-1 ...
- multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
情景再现: 上传文件到.net(wcf)后台时,总是上传不成功,后台要求Content-Type是application/octet-stream,我使用multipart/form-data方式,c ...
- JS(JavaScript)的深入了解1(更新中···)
面向对象 1.单列模式 2.工厂模式 3.构造函数 (1) 类Js天生自带的类Object 基类Function Array Number Math Boolean Date Regexp Strin ...
- javax.el.PropertyNotFoundException: Property 'XXX' not found on type java.lang.String
遇到的问题: 在使用idea开发Java Web时,调用SSM框架出现了如下错误: 但是我的类中已经定义了geter和seter方法,如下: 而Jsp中的调用代码是通过EL实现,也导入了相应的包.如下 ...
- SPOJ - REPEATS Repeats (后缀数组)
A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed strin ...