Spring Cloud系列(八)熔断监控Hystrix Dashboard和Turbine

熔断监控Hystrix Dashboard和Turbine

    • Hystrix Dashboard和Turbine是什么?
    • 使用Hystrix Dashboard
    • 使用Turbine

Hystrix Dashboard和Turbine是什么?

      Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数据。但是只使用Hystrix Dashboard的话, 你只能看到单个应用内的服务信息, 这明显不够. 我们需要一个工具能让我们汇总系统内多个服务的数据并显示到Hystrix Dashboard上, 这个工具就是Turbine。

使用Hystrix Dashboard

     以下演示项目我都会上传到Github上,大家可以在上面拉取项目进行学习测试;
一、准备工作:
     启动以前的项目 erurekaserver,config-server,eurekaclient1;
二、修改项目
     复制前面的项目service-ribbon,修改RibbonApplication.java:

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
    
    @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;
    }
}

     修改pom.xml:


  4.0.0

  wg
  sgccplatform-business-ribbon
  0.0.1-SNAPSHOT
  jar

  sgccplatform-business-ribbon
  http://maven.apache.org

  
    UTF-8
    UTF-8
    Greenwich.RELEASE
    1.8
  

  
    org.springframework.boot
    spring-boot-starter-parent
    2.1.1.RELEASE
     
  

  
  	
		org.springframework.cloud
		spring-cloud-starter-zipkin
	
	
        org.springframework.boot
        spring-boot-starter-actuator
    
  	
        org.springframework.cloud
        spring-cloud-starter-netflix-hystrix
	
	
        org.springframework.cloud
        spring-cloud-starter-netflix-hystrix-dashboard
    
	
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.springframework.cloud
        spring-cloud-starter-netflix-ribbon
    
    
      junit
      junit
      test
    
  
  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        ${spring-cloud.version}
        pom
        import
      
    
  

     启动项目,访问:http://localhost:6005/login,可以访问相应的数据;
     访问:http://localhost:6005/hystrix,可以看到页面:
Spring Cloud系列(八)熔断监控Hystrix Dashboard和Turbine_第1张图片
     确定,留着这个页面打开状态。
Spring Cloud系列(八)熔断监控Hystrix Dashboard和Turbine_第2张图片
     再次访问:http://localhost:6005/login,回来查看监控页:
Spring Cloud系列(八)熔断监控Hystrix Dashboard和Turbine_第3张图片
      关闭 eurekaclient1 ,访问 :http://localhost:6005/login,会看到错误信息;此时再次查看监控页面;
Spring Cloud系列(八)熔断监控Hystrix Dashboard和Turbine_第4张图片

使用Turbine

     参照上面的项目和以前的项目,把以前的项目 service-feign 进行同样的改造。
1、新建maven项目service_turbine,新建pom.xml:


  4.0.0

  wg
  service_turbine
  0.0.1-SNAPSHOT
  jar

  service_turbine
  http://maven.apache.org

  
    UTF-8
    UTF-8
    Greenwich.RELEASE
    1.8
  

  
    org.springframework.boot
    spring-boot-starter-parent
    2.1.1.RELEASE
     
  

  
  	
        org.springframework.boot
        spring-boot-starter-actuator
    
  	
        org.springframework.cloud
        spring-cloud-starter-netflix-hystrix
	
	
        org.springframework.cloud
        spring-cloud-starter-netflix-hystrix-dashboard
    
  	
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.springframework.cloud
        spring-cloud-starter-openfeign
    
     
         org.springframework.cloud
         spring-cloud-starter-netflix-turbine
     
    
      junit
      junit
      test
    
  
  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        ${spring-cloud.version}
        pom
        import
      
    
  

2、新建启动类ServiceTurbineApplication.java

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

3、新建application.yml

server:
  port: 8767

spring:
  application:
    name: service-turbine

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost1:6001/eureka/,http://localhost2:6001/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

turbine:
  app-config: service-feign,service-ribbon
  aggregator:
    clusterConfig: default
  clusterNameExpression: new String("default")
  combine-host: true
  instanceUrlSuffix:
    default: actuator/hystrix.stream

4、启动项目,访问:http://localhost:8767/turbine.stream
Spring Cloud系列(八)熔断监控Hystrix Dashboard和Turbine_第5张图片
会看到一个这样的页面,具体信息不多解释;
5、访问:http://localhost:7605/hystrix ,填写内容;
Spring Cloud系列(八)熔断监控Hystrix Dashboard和Turbine_第6张图片
6、点击监控,然后访问:http://localhost:8765/hello,http://localhost:8764/hello

Spring Cloud系列(八)熔断监控Hystrix Dashboard和Turbine_第7张图片

你可能感兴趣的:(吃透Spring,Cloud系列)