Spring Cloud Hystrix Dashboard是一个可以监控HystrixCommand的可视化图形界面,由于某种原因,如网络延迟、服务故障等,这时候可以借助dashboard提供的可视化界面监控各个Hystrix执行的成功率、调用成功数、失败数量、最近十分钟的流量图等等,根据这些数据我们就可以进行错误排查以及进行服务的优化等。Hystrix Dashboard只能对单个服务进行监控,实际项目中,服务通常集群部署,这时候可以借助Turbine进行多个服务的监控。
hystrix-server:eureka服务注册中心,端口1111,本文不做介绍
hystrix-dashboard1:进行hystrix监控的服务1 端口3333
hystrix-dashboard2:进行hystrix监控的服务2 端口5555
hystrix-turbine:turbine集群监控项目 端口4444
接下来先讲讲单个服务的监控:
pom.xml
4.0.0
com.springcloud.wsh
springcloud_hystrix_dashboard
0.0.1-SNAPSHOT
jar
springcloud_hystrix_dashboard
Spring Cloud Hystrx Dashboard仪表盘
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
Camden.SR6
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-ribbon
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.cloud
spring-cloud-starter-hystrix-dashboard
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
/**
* @Description: 单个服务的Hystrix监控
* @Author: WeiShiHuai
* @Date: 2018/9/13 15:30
*/
//@SpringBootApplication
//@EnableCircuitBreaker
//@EnableDiscoveryClient
@SpringCloudApplication
@EnableHystrixDashboard
public class SpringcloudHystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudHystrixDashboardApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
提供一个/hello的接口,并且使用@HystrixCommand注解修饰,指定Hystrix服务降级处理方法
/**
* @Title: HystrixController
* @ProjectName springcloud_hystrix_dashboard
* @Description: 测试
* @Author WeiShiHuai
* @Date 2018/9/13 15:48
*/
@RestController
public class HystrixController {
private static Logger logger = LoggerFactory.getLogger(HystrixController.class);
@RequestMapping("/hello")
@HystrixCommand(fallbackMethod = "hiError")
public String hello(@RequestParam String name) {
return "hello, " + name;
}
public String helloError(String name) {
return "hello, " + name + ",sorry,error!";
}
}
server:
port: 3333
spring:
application:
name: hystrix-dashboard1
eureka:
client:
service-url:
defaultZone: http://localhost:1111/eureka/
首先我们先访问http://localhost:3333/hello?name=weixiaohuai,如下图:
可以看到,接口已经调用成功,接着我们访问:http://localhost:3333/hystrix,进入hystrix监控界面:如下图
此时我们在监控地址栏输入:http://localhost:3333/hystrix.stream,如下图
点击Monitor Stream,进入到Dashboard仪表盘实时监控页面,这个时候我们需要请求一下接口http://localhost:3333/hello?name=weixiaohuai,这样仪表盘才能监控到请求的一些数据信息。
监控界面实时展示了两分钟内接口请求的流量、请求成功百分比、成功数、失败数、超时数等等。至此,单服务的Hystrix监控已经实现。接下来看一下集群监控Turbine的实现方法:
端口5555,只是端口与hystrix-dashboard1工程不一致,其他与hystrix-dashboard1都一样,下面直接贴代码:
pom.xml:
4.0.0
com.springcloud.wsh
springcloud_hystrix_dashboard2
0.0.1-SNAPSHOT
jar
springcloud_hystrix_dashboard2
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
Camden.SR6
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-ribbon
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.cloud
spring-cloud-starter-hystrix-dashboard
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
配置文件:
server:
port: 5555
spring:
application:
name: hystrix-dashboard2
eureka:
client:
service-url:
defaultZone: http://localhost:1111/eureka/
启动类:
@SpringCloudApplication
@EnableHystrixDashboard
public class SpringcloudHystrixDashboard2Application {
public static void main(String[] args) {
SpringApplication.run(SpringcloudHystrixDashboard2Application.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
HystrixController2:
/**
* @Title: HystrixController
* @ProjectName springcloud_hystrix_dashboard
* @Description: 测试
* @Author WeiShiHuai
* @Date 2018/9/13 15:48
*/
@RestController
public class HystrixController2 {
private static Logger logger = LoggerFactory.getLogger(HystrixController2.class);
@RequestMapping("/hello")
@HystrixCommand(fallbackMethod = "helloError")
public String hello(@RequestParam String name) {
return "hello, " + name;
}
public String helloError(String name) {
return "hello, " + name + ",sorry,error!";
}
}
端口4444,注意要引入turbine的依赖:
4.0.0
com.springcloud.wsh
springcloud_turbine
0.0.1-SNAPSHOT
jar
springcloud_turbine
Spring Cloud Turbine集群监控
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
Camden.SR6
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-turbine
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
@SpringBootApplication
@EnableTurbine
public class SpringcloudTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudTurbineApplication.class, args);
}
}
server:
port: 4444
spring:
application:
name: hystrix-turbine
eureka:
client:
service-url:
defaultZone: http://localhost:1111/eureka/
turbine:
# 配置注册到Eureka中的serviceId列表,表明监控哪些服务
app-config: hystrix-dashboard1,hystrix-dashboard2
cluster-name-expression: new String("default")
# 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
#aggregator:
#clusterConfig: default # 指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
注意:通过turbine.appConfig指定我们需要监控哪些服务,这里对应注册到Eureka中的名称,即application-name(serviceId)
浏览器访问:http://localhost:3333/hello?name=weixiaohuai、http://localhost:5555/hello?name=helloworld
我们随机访问一下这两个服务的接口,接着我们浏览器输入http://localhost:3333/hystrix进入到hystrix监控页面(http://localhost:5555/hystrix也可以),在监控地址栏输入:http://localhost:4444/turbine.stream
可以看到,turbine已经实现了对HystrixController、HystrixController2的集群监控。
其实,在实际项目中,这种实时监控有点耗性能,通常采用消息中间件如RabbitMQ等,我们接口调用把Hystrix的一些信息收集到RabbitMQ中,然后Turbine从RabbitMQ中获取监控的数据。本文主要讲解dashboard和turbine的基本使用方法,对于一些高级用法,还有待研究。。。