微服务设计指导-hystrix的监控

我们在生产环境或者是在压测时经常会需要了解:熔断了多少次?目前哪些接口断着哪些接口开着?

你总不能看那些刷的可以飞起来的日志滚屏吧?那你真的成Matrix了。

因此,Hytrix是先天就带有熔断监控功能的。如下这样的截图:

这是由以下这个组件组成的(我们是基于spring boot2以及spring cloud2来做的hystrix监控,版本是有严格讲究的,甚至连artifactId的名字在不同版本都有本质上的区别,你千万记得不要盲目的复制粘贴。

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

 光有这一个组件,你是看不到dashboard的。因此要看到hystrix-dashboard你必须同时要加入如下依赖进入maven的pom.xml文件。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!-- hystrix dashboard必备 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- hystrix dashboard必备 -->

这是因为,hystrix-dashboard是通过actuator的端点控制功能来收集/ping相应的hystrix客户端的数据流,并以hystrix-dashboard来作展示的。

说好了pom.xml文件的注意项后我们为了启用这个仪表盘还需要做到以下几点:

在spring boot启动类里加入以下annotation

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
@EnableHystrixDashboard
@EnableCircuitBreaker
public class AdvancedIndexApp {
 
    public static void main(String[] args) {
        SpringApplication.run(AdvancedIndexApp.class, args);
 
    }
}

其中以下几荐紧密和hystrix-dashboard相关:

  • @EnableFeignClient
  • @EnableHystrixDashboard
  • @EnableCircuitBreaker

看,网上是不会告诉你这些的。

需要新建一个hystrix-dashboard的自动装配注解类-HystrixDashBoardConfig

package org.sky.demo.timeout.advancedindex.config;
 
/**
 * hystrix必备
 */
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
 
public class HystrixDashBoardConfig {
    @Bean
    public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet() {
        HystrixMetricsStreamServlet hystrixMetricsStreamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean<HystrixMetricsStreamServlet> servletRegistrationBean = new ServletRegistrationBean();
        servletRegistrationBean.setServlet(hystrixMetricsStreamServlet);
        servletRegistrationBean.setLoadOnStartup(1);
        servletRegistrationBean.addUrlMappings("/hystrix.stream");
        servletRegistrationBean.setName("HystrixMetricsStreamServlet");
        return servletRegistrationBean;
    }
}
  • 这里面的/hystrix.stream严格会和.yml里的hystrix用actuator收集到的各hystrix内的一个“数据文件”是一一对应名字子的;
  • 这里面的setLoadOnStartup(1)也是很有讲究的,网上99%的例子里没有这一句,导致你copy了网上的例子,在本机启动的好好的,跑到其它环境后,你会发觉死活这个dashboard不显示;

在applicaton.yml文件里需要加入actuator和hystrix-dashboard的配置

ribbon:
   eager-load:
      enabled: true
      clients: TimeoutUser
hystrix:
   dashboard:
      proxy-stream-allow-list: "localhost" #hystrix dashboard用,如果不加这条hystrix启动后也会出现hystrix.dashboard.proxyStreamAllowList
   threadpool:
      default:
         coreSize: 1000
         maximumSize: 2000
         maxQueueSize: -1
   command:
      default:
         circuitBreaker:
            sleepWindowInMilliseconds: 30000 #断路器等待窗口时间 默认5000, 该时间段内服务不可访问
            errorThresholdPercentage: 10 # 断路器错误百分比阈值 默认50  建议保持默认值    #当请求超过阈值20并且 50%以上的错率 就会打开断路器
         execution:
            isolation:
               thread:
                  timeoutInMilliseconds: 30000
management: #hystrix dashboard用,actuator暴露端口,在http://localhost:端口名/hystrix界面添加的是http://localhost:端口名/actuator/hystrix.stream
  endpoints:
    web:
      exposure:
        include: "hystrix.stream,turbine.stream"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

这边高度需要各位注意了,太多和网上不一样的地方。

  • hystrix.dashboard.proxy-stream-allow-list,这个值,如果不配,你在用hystrix的dashboard加载“actuator“收集到的hystrix.stream文件会因该JVM的沙盒机制不允许加载网页组件而显示:404 no page的错误;
  • management那边一陀就是actuator的申请:
    • exposure指的是“哪些收集到的接口可用”,你不要来一个*,这就危险了,你会把api都暴露在内网上,这是一个安全的问题;
    • cors指的是可允许跨域名,因为当你有多个hystrix,很可能会在不同域名下,这边我建议在QAS或者是生产环境,一定要指定“允许跨域访问hystrix.stream和turbine.stream我那些域名“,由于是本地环境,因此这边我就放了一个*

启动hystrix-dashboard

以上全配好了后,把这个带有hystrix-dashboard的spring cloud consumer启动起来。

启动后,打开一个ie输入:http://localhost:你的spring boot的server端口/hystrix,你就可以看到这样的一个界面

 然后在上部红色箭头所指的输入框即以下截图指示输入:http://localhost:你的spring boot的server端口/actuator/hystrix.stream

然后点[Monitor Stream]这个按钮,你就可以得到以下dashboard

 如果一开始,你进入这个页面,上下一直显示“loading data“,这个没有关系的,这是因为,/actuator/hystrix.stream还没有收集到数据。此时你多访问几次你的微服务,这个仪表盘过一会当开始吸到的第一条数据后就会出现了。

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TGITCIC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值