天天看点

spring cloud 基于2.X实现熔断监控Hystrix Dashboard和Turbine

1. 熔断监控

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

2. Hystrix Dashboard

2.1 引入pom文件

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<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>
           

2.2 启动类

# ---------添加@EnableHystrixDashboard  注解

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard 
public class CustomerApplication {

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

    /**
     * @author pengjw
     * @date 2019/3/19 20:32
     * @return org.springframework.boot.web.servlet.ServletRegistrationBean
     * spring boot 版本大于2.0就需要配置这个,否则就会报404
     */
    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}
           

2.3 测试

这个是在customer上改造的,所以启动项目访问 地址:http://localhost:8003/hystrix,输入http://localhost:8003/hystrix.stream,就会有图形见面展示,但是这个是但服务监控的,所以我们需要Turbine群监控。

spring cloud 基于2.X实现熔断监控Hystrix Dashboard和Turbine
spring cloud 基于2.X实现熔断监控Hystrix Dashboard和Turbine

3. Turbine

创建turbine项目

3.1 引入依赖

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-turbine</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-netflix-turbine</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
	</dependency>
</dependencies>
           

3.2 配置文件

spring:
  application:
    name: hystrix-dashboard-turbine
server:
  port: 8004
turbine:
  app-config: spring-cloud-node1,spring-cloud-node2   #需要监控的服务
  aggregator:
    cluster-config: default
  cluster-name-expression: new String("default")
  instanceUrlSuffix: /hystrix.stream          #由于默认是/actuator/hystrix.stream,所以在此需要重新声明一下
eureka:
  client:
    service-url:
      defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/  #注册中心
           

3.3 启动类

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class TurbineApplication {

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

}
           

3.4 创建两个实例

由于和customer差不多,所以就不贴代码了,如果需要可以去github。

spring:
  application:
    name: spring-cloud-node1
server:
  port: 8005
feign:
  hystrix:
    enabled: true
eureka:
  client:
    service-url:
      defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/
           
spring:
  application:
    name: spring-cloud-node2
server:
  port: 8006
feign:
  hystrix:
    enabled: true
eureka:
  client:
    service-url:
      defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/在这里插入代码片
           

3.5 启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
public class Node1Application {

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


	/**
	 * @author pengjw
	 * @date 2019/3/23 16:45
	 * @return org.springframework.boot.web.servlet.ServletRegistrationBean
	 * 由于出现访问http://localhost:8004/turbine.stream 时出现404,配置完了就ok了
	 */
	@Bean
	public ServletRegistrationBean getServlet() {
		HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
		ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
		registrationBean.setLoadOnStartup(1);
		registrationBean.addUrlMappings("/hystrix.stream");
		registrationBean.setName("HystrixMetricsStreamServlet");
		return registrationBean;
	}
}
           

3.6 测试

修改完毕后,依次启动eureka、node1、node2、turbine

spring cloud 基于2.X实现熔断监控Hystrix Dashboard和Turbine

这说明已经启动完成。

输入地址:http://localhost:8004/hystrix,又会出现熟悉的小熊熊,访问下两个服务 http://localhost:8005/hello/pipi、http://localhost:8006/hello/pipi 就会出现如下图所示:

spring cloud 基于2.X实现熔断监控Hystrix Dashboard和Turbine

spring-cloud系列之Turbine示例代码-github

继续阅读