Hystrix以及Hystrix Dashboard搭建与配置

概述

  • hystrix 断路器工具,提供了系统容错功能。在Spring Cloud 中一般不单独使用,通常使用 feignzuul集成。

配置

1、添加 hystrix 依赖
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2、主程序配置
  • 添加 @EnableCircuitBreaker注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;

@EnableDiscoveryClient    启用断路器
@SpringBootApplication
public class Sp06RibbonApplication {

	public static void main(String[] args) {
		SpringApplication.run(Sp06RibbonApplication.class, args);
	}
}
3、配置超时时间(可不配)
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 1000  

降级功能

  • 方法快速失败,返回指定的降级内容
实现降级方法
  • 在指定的方法上添加@HystrixCommand(fallbackMethod = "降级方法名")注解
	@HystrixCommand(fallbackMethod = "getItemsFB")
	@GetMapping("/item-service/{orderId}")
	public JsonResult<List<Item>> getItems(@PathVariable String orderId) {
		return restTemplate.getForObject("http://item-service/{1}", JsonResult.class, orderId);
	}
	// 降级方法
	public JsonResult<List<Item>> getItemsFB(@PathVariable String orderId) {
		return JsonResult.err("获取订单商品列表失败");
	}

熔断功能

  • 在实现降级后,默认就会有熔断功能
熔断的条件:
  • 1、10秒20次请求(必须首先满足)
  • 2、50% 的请求失败,执行降级代码
半开状态:
  • 在打开断路器后,每隔5秒就会变成 半开状态 ,会尝试发送一次请求,成功,自动关闭断路器,失败,断路器继续打开

Hystrix dashboard

  • Spring Cloud 中的监控工具,利用 actuator暴露监控数据

添加 actuator依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • 暴露的监控端点为:hystrix.stream

yml配置

management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream

使用 dashboard监控 暴露的端点

新建一个项目
  • 1、添加依赖
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
  • 2、在主程序上添加 @EnableHystrixDashboard注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@EnableHystrixDashboard 
@SpringBootApplication
public class Sp08HystrixDashboardApplication {

	public static void main(String[] args) {
		SpringApplication.run(Sp08HystrixDashboardApplication.class, args);
	}

}
1、先测试是否成功暴露端点
  • http://localhost:3001/actuator/hystrix.stream

在没有访问后台服务的时候,只显示ping
在这里插入图片描述

只有访问后台服务后才有监控数据
在这里插入图片描述

访问 Hystrix dashboard

  • http://localhost:4001/hystrix
    在这里插入图片描述

把暴露的端口地址复制到1处,点击 2 开始监控
在这里插入图片描述
从左到右,从上往下代表的次数分别为成功,短路,错误请求,超时,拒绝,失败错误
Circuit 显示断路器的状态,open为打开,closed为关闭

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值