dashboard界面化管理hystrix

217 阅读2分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第27天,点击查看活动详情

HystrixDashboard

  • hystrix 除了服务熔断、降级、限流以外,还有一个重要的特性是实时监控。并形成报表统计接口请求信息。
  • 关于hystrix的安装也很简单,只需要在项目中配置actutor和hystrix-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>
  • 启动类上添加EnableHystrixDashboard 就引入了dashboard了。 我们不需要进行任何开发。这个和eureka一样主需要简单的引包就可以了。
  • 就这样dashboard搭建完成了。dashboard主要是用来监控hystrix的请求处理的。所以我们还需要在hystrix请求出将端点暴露出来。
  • 在使用了hystrix命令的模块加入如下配置即可,我就在order模块加入
 @Component
 public class HystrixConfig {
     @Bean
     public ServletRegistrationBean getServlet(){
         HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
         ServletRegistrationBean registrationBean = newServletRegistrationBean(streamServlet);
         registrationBean.setLoadOnStartup(1);
         //注意这里配置的/hystrix.stream 最终访问地址就是 localhost:port/hystrix.stream ; 如果在配置文件中配置在新版本中是需要
         //加上actuator 即 localhost:port/actuator
         registrationBean.addUrlMappings("/hystrix.stream");
         registrationBean.setName("HystrixMetricsStreamServlet");
         return registrationBean;
    }
 }
  • 然后我们访问order模块localhost/hystrix.stream 就会出现ping的界面。表示我们order模块安装监控成功。当然order也需要actuator模块
  • 下面我们通过jmeter来压测我们的熔断、降级、限流接口在通过dashboard来看看各个状态吧。
  • 上面的动画看起来我们的服务还是很忙的。想想如果是电商当你看着每个接口的折线图像不像就是你的心跳。太高的你就担心的。太低了就没有成就高。下面我们看看dashboard的指标详情
  • 我们看看我们服务运行期间各个接口的现状。

聚合监控

  • 上面我们通过新建的模块hystrix-dashboard 来对我们的order模块进行监控。但是实际应用中我们不可能只在order中配置hystrix的。
  • 我们只是在上面为了演示所以在order配置的。现在我们在payment中也对hystrix中配置。那么我们就需要在dashboard中来回切换order、payment的监控数据了。
  • 所以我们的聚合监控就来了。在进行聚合监控之前我们先将payment也引入hystrix。注意上面我们是通过bean方式注入hystrix.stream 的 。 访问前缀不需要actuator

新建hystrix-turbine

pom

 <!--新增hystrix dashboard-->
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
         </dependency>
  • 主要就是新增turbine坐标,其他的就是hystrix , dashboard等模块,具体查看结尾处源码

yml

 spring:
  application:
    name: cloud-hystrix-turbine
 
 eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka
  instance:
    prefer-ip-address: true
 
 # 聚合监控
 
 turbine:
  app-config: cloud-order-service,cloud-payment-service
  cluster-name-expression: "'default'"
  # 该处配置和url一样。如果/actuator/hystrix.stream 的则需要配置actuator
  instanceUrlSuffix: hystrix.stream

启动类


\