栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

一分钟快速配置hystrix断路器、及hystrix dashboard 断路器仪表盘

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一分钟快速配置hystrix断路器、及hystrix dashboard 断路器仪表盘

(1/2) Hystrix断路器

维基百科说明:https://github.com/Netflix/Hystrix/wiki
维基配置说明:https://github.com/Netflix/Hystrix/wiki/Configuration
源码:https://github.com/benwang6/spring-cloud-repo

依赖——>配置——>注解——>启动


:依赖 netflix-hystrix


	org.springframework.cloud
	spring-cloud-starter-netflix-hystrix

:配置

spring:
  application:
    name: hystrix
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 500 #超时后,执行降级代码,此设置一般应大于 ribbon 的重试超时时长,例如 10 秒。
      circuitBreaker:
        requestVolumeThreshold: 20 #第一条件:10秒内请求数量,默认20,如果没有达到该数量,即使请求全部失败,也不会触发断路器打开。
        errorThresholdPercentage:  #第二条件:请求失败比例
        sleepWindowInMilliseconds: 5000 #多少毫秒进入半开,尝试访问。默认5000

:注解

@EnableCircuitBreaker
//启动类

:核心功能
降级——是熔断后的措施
熔断——是降级前的判断

if(10秒内产生超过20次请求){
     if(错误超50%){
          开启断路器【降级】
          5秒后进入半开状态,尝试访问。
     }
}

RibbonController中添加降级方法(控制层)
	@GetMapping("/item-service/{orderId}")
	@HystrixCommand(fallbackMethod = "getItemsFB") //指定降级方法的方法名
	public JsonResult> getItems(@PathVariable String orderId) {
		return rt.getForObject("http://item-service/{1}", JsonResult.class, orderId);
	}
	//降级方法的参数和返回值,需要和原始方法一致,方法名任意
	public JsonResult> getItemsFB(String orderId) {
		return JsonResult.err("获取订单商品列表失败");
	}
(2/2) hystrix dashboard 断路器仪表盘

hystrix dashboard 通过SpringActuator暴露监控信息

暴露端点

这里我们先了解一下SpringActuator
springboot提供的项目监控指标工具,提供了多种监控数据

  • 健康状态
  • 环境变量,配置参数
  • springmvc映射路径
  • JVM虚拟机堆内存镜像
  • spring容器中的所有对象
依赖——>配置——>查看端点

:依赖 actuator


	org.springframework.boot
	spring-boot-starter-actuator

:配置

#首字母:mewei
management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream #暴露hystrix的流控
        		 "*" #暴露所有监控数据
        		 health, env, beans, mappings #健康状况,环境变量,bean们,mvc映射们

:查看端点
http://localhost:3001/actuator

新建dashboard服务 依赖——>配置——>注解——>启动

:依赖 netflix-hystrix-dashboard

		
			org.springframework.cloud
			spring-cloud-starter-netflix-hystrix-dashboard
		

:配置

hystrix:
  dashboard:
    proxy-stream-allow-list: localhost

:注解

@EnableHystrixDashboard
@EnableDiscoveryClient
//启动类

:启动
访问rul:http://localhost:4001/hystrix
在bashboard界面中填入其他服务的流控端点,开启监控。
填入:http://localhost:3001/actuator/hystrix.stream

注意:这里只能填入一个流控端点
思考,如果想监控集群。应该怎么办?

hystrix dashboard + turbine

使用 turbine 可以汇集监控信息,将聚合后的信息提供给 hystrix dashboard 来集中展示和监控。

依赖——>配置——>注解——>启动

:依赖

		
			org.springframework.cloud
			spring-cloud-starter-netflix-turbine
		

:配置

turbine:
  app-config: order-service
  cluster-name-expression: new String("default")

:注解
@EnableTurbine
@EnableDiscoveryClient
//启动类

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/677408.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号