用Hystrix Dashboard和Turbine实现数据的可视化监控

Hystrix提供了实时监控,可以记录执行信息,QPS,请求中成功和失败的数量等。

Hystrix Dashboard的可视化监控

单个应用的熔断监控

1. 添加依赖和配置

添加Eureka、Hystrix、Hystrix-dashboard、Actuator、Feign的依赖

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

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-web
        

2. 添加配置

spring.application.name=spring-cloud-consumer-hystrix
server.port=9001
feign.hystrix.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3. 添加配置启动类和Servlet

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class SpringCloudConsumerHystrixApplication {

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

    @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;
    }
}

注意: springboot2.0需要配置Servlet才会显示监控数据。

4. 查看监控数据

  • 启动项目spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三个项目
  • 访问http://localhost:9001/hystrix
    然后输入http://localhost:9001/actuator/hystrix.stream 即可进入监控页面。

Hystrix Dashboard + Turbine的可视化监控

在分布式系统中,相同服务实例经常需要部署上百甚至上千个,很多时候需要把服务实例的状态以集群方式展现出来,这样就可以更好的查看系统状态。,为此Netflix提供了一个开源项目(Turbine)来提供把多个hystrix.stream的内容聚合为一个数据源供Dashboard展示。

1. 添加依赖并启动支持

需要添加Turbine、Hystrix-dashboard、Actuator、Hystrix依赖

 
        
            org.springframework.cloud
            spring-cloud-starter-netflix-turbine
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix-dashboard
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-web
        

2. 添加配置

spring.application.name=hystrix-dashboard-turbine
server.port=8001
turbine.appConfig=node01,node02
turbine.aggregator.clusterConfig= default
turbine.clusterNameExpression= new String("default")
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
  • turbine.appConfig :配置Eureka中的serviceId列表,表明监控哪些服务
  • turbine.aggregator.clusterConfig :指定聚合哪些集群,多个使用”,”分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
  • turbine.clusterNameExpression : 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称;2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default;3. 当clusterNameExpression: metadata[‘cluster’]时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,

3. 配置启动支持

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class HystrixDashboardTurbineApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboardTurbineApplication.class, args);
    }
}

4. 创建多个服务消费者

分别创建子项目spring-cloud-consumer-node1,spring-cloud-consumer-node2
node01添加配置

spring.application.name=node01
server.port=9001
feign.hystrix.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

node02添加配置

spring.application.name=node02
server.port=9002
feign.hystrix.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

分别配置feign
node01

@FeignClient(name = "spring-cloud-provider", fallback = HelloHystrix.class)
public interface MyFeignClient {
    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);
}

node02

@FeignClient(name = "spring-cloud-provider2", fallback = HelloHystrix.class)
public interface MyFeignClient {
    @RequestMapping(value = "/hello")
    public String hello2(@RequestParam(value = "name") String name);
}

5. 测试

依次启动spring-cloud-eureka、spring-cloud-consumer-node1、spring-cloud-consumer-node1、hystrix-dashboard-turbine(Turbine)
(1)访问 http://localhost:8001/turbine.stream
返回:

ping
data: {"reportingHostsLast10Seconds":1,"name":"meta","type":"meta","timestamp":1494921985839}

进行图形化监控查看,输入:http://localhost:8001/hystrix,返回酷酷的小熊界面,输入: http://localhost:8001/turbine.stream,然后点击 Monitor Stream ,可以看到出现了俩个监控列表

参考:
http://www.ityouknow.com/springcloud/2017/05/18/hystrix-dashboard-turbine.html

你可能感兴趣的:(用Hystrix Dashboard和Turbine实现数据的可视化监控)