Spring Cloud私人笔记整理(五)➡hystrix熔断监控:dashboard和turbine

目录

????什么是 hystrix-dashboard?

????什么是 turbine?

 ???? hystrix-dashboard开始第一个监控小面板

 ????Turbine聚合多个hystrix.stream


 

Spring Cloud私人笔记整理(五)➡hystrix熔断监控:dashboard和turbine

????什么是 hystrix-dashboard?

Hystrix-dashboard是一个对Hystrix进行实时监控的工具,Hystrix Dashboard就像监控室的监控台,以在直观地看到各个Hystrix Command的请求响应时间, 请求成功率等数据。

 

????什么是 turbine?

如果只使用Hystrix Dashboard的话, 你只能看到单个应用内的服务信息, 这明显不够。 我们需要一个工具能让我们汇总系统内多个服务的数据并显示到Hystrix Dashboard上, 这个工具就是Turbine。Turbine就是一个监控板查看多个服务资源数据情况。

 

 ???? hystrix-dashboard开始第一个监控小面板

????示例:

1.创建一个springboot项目:(和上文服务调用spring-cloud-consumer-hystrix项目类似,还是重新建一个方便各个知识模块独立查看)

使用官方快速创建➡:https://start.spring.io/

取名:spring-cloud-consumer-hystrix-dashboard

2.pom加入hystrix-dashboard相关依赖:

        <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<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>

ps:新加的spring-boot-starter-actuator是什么?

一句话,actuator是监控系统健康情况的工具。

pps:spring-cloud-starter-hystrix/spring-cloud-starter-hystrix-dashboard/spring-boot-starter-actuator三个包必须加入。

3.启动类添加启用Hystrix Dashboard和熔断器:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
//启用Hystrix Dashboard功能
@EnableHystrixDashboard
//@EnableCircuitBreaker注解之后,就可以使用断路器功能
@EnableCircuitBreaker
public class ConsumerApplication {

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

4.测试Hystrix Dashboard

启动新建的项目:spring-cloud-consumer-hystrix-dashboard

打开浏览器,输入:http://localhost:9001/hystrix,可以看到一只大熊界面:

Spring Cloud私人笔记整理(五)➡hystrix熔断监控:dashboard和turbine

翻译一下其中的英文:

通过涡轮机的集群(默认集群):http://turbine hostname:port/turbine.stream

通过涡轮机的集群(自定义集群):http://turbine hostname:port/turbine.stream?cluster=[集群名称]

单一Hystrix应用程序:http://hystrix-app:port/hystrix.stream

本次示例是单一程序,启动spring-cloud-consumer服务调用和spring-cloud-producer服务提供测试

启动后在上图示例红框框里输入: http://localhost:9001/hystrix.stream 

Spring Cloud私人笔记整理(五)➡hystrix熔断监控:dashboard和turbine

可以看到监控面板界面。

访问http://localhost:9001/hystrix.stream,也会显示一直ping。

然后请求服务http://localhost:9001/hello/hahaha,就可以看到监控的效果了:

Spring Cloud私人笔记整理(五)➡hystrix熔断监控:dashboard和turbine

图形化显示http://localhost:9001/hystrix.stream,非常便于查看。

简单粗暴翻译一下面板含义:

Spring Cloud私人笔记整理(五)➡hystrix熔断监控:dashboard和turbine

这是单个应用的熔断监控。

多应用熔断监控请看Turbine。

 

 ????Turbine聚合多个hystrix.stream

????示例:

1.创建一个springboot项目:

使用官方快速创建➡:https://start.spring.io/

取名:hystrix-dashboard-turbine

2.maven的pom依赖:

        <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>

3.配置文件

application.properties文件:

#服务名
spring.application.name=hystrix-dashboard-turbine
#服务端口号
server.port=8001
#配置Eureka中的serviceId列表,表明监控哪些服务
turbine.appConfig=node01,node02
#指定聚合哪些集群,多个使用”,”分割,默认为default。
#可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
turbine.aggregator.clusterConfig= default
#eureka注册地址
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

turbine.clusterNameExpression= new String("default")
#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,则需要配置,\
#  同时turbine.aggregator.clusterConfig: ABC

4.修改启动类,添加turbin支持

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class DashboardApplication {

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

}

5.测试

copy示例项目spring-cloud-consumer-hystrix新建两个服务的调用者spring-cloud-consumer-node1和spring-cloud-consumer-node2

spring-cloud-consumer-node1项目改动如下: application.properties文件内容

spring.application.name=node01
server.port=9001
feign.hystrix.enabled=true

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

spring-cloud-consumer-node2项目改动application.properties文件内容

spring.application.name=node02
server.port=9002
feign.hystrix.enabled=true

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

spring-cloud-consumer-node2项目改动HelloRemote类,远程调用服务名以及方法名:

@FeignClient(name= "spring-cloud-producer2", fallback = HelloRemoteHystrix.class)
public interface HelloRemote {

    @RequestMapping(value = "/hello")
    public String hello2(@RequestParam(value = "name") String name);

}

对应的HelloRemoteHystrixConsumerController类跟随修改,具体查看代码

修改完毕后,依次启动spring-cloud-eureka、spring-cloud-consumer-node1、spring-cloud-consumer-node1、hystrix-dashboard-turbine(Turbine)

打开eureka后台可以看到注册了三个服务:

Spring Cloud私人笔记整理(五)➡hystrix熔断监控:dashboard和turbine

访问 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,????发现是loading。。。。因为此时无服务调用呢

访问http://localhost:9001/hello/hahahahttp://localhost:9002/hello/hahaha,再次刷新小熊界面发现两个监控版:

Spring Cloud私人笔记整理(五)➡hystrix熔断监控:dashboard和turbine

????此案例演示turbine实现多个服务hystrix熔断监控~