Hystrix Dashboard是Hystrix提供的实时监控系统。Hystyix会持续记录所有通过Hystrix发起的请求执行信息,并通过统计报表和图形的形式展示给用户。
Hystrix Dashboard是一个单独的微服务。
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>启动类:
@SpringBootApplication @EnableHystrixDashboard //开启HystrixDashboard支持 public class HystrixDashboard { public static void main(String[] args) { SpringApplication.run(HystrixDashboard.class,args); } }配置文件:
server: port: 9002至此Hystrix Dashboard服务创建完成。在浏览器中输入:http://localhost:9002/hystrix可以访问Dashboard主页:
如果出现上面的界面,说明Dashboard创建成功。下面开始监测Eureka中的微服务。
由于Hystrix Dashboard是通过Hystrix监控微服务的,所以被监控的微服务需要开启Hystrix断路保护器。这里以之前创建的cloudProviderHystrix微服务为例进行监测:
POM:
<!-- hystrix --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <!-- 将微服务provider侧注册进eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- actuator监控信息完善 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>注意:这里必须有actuator包,支持监控。
启动类:
@SpringBootApplication @EnableEurekaClient @EnableCircuitBreaker public class CloudProviderHystrix { public static void main(String[] args) { SpringApplication.run(CloudProviderHystrix.class,args); } }application.yml:
server: port: 8089 spring: application: name: cloud-provider-hystrix eureka: client: service-url: defaultZone: http://eurekaServer7003:7003/eureka/ instance: instance-id: cloud-provider-hystrix-${server.port}dept prefer-ip-address: truecontroller:
@RestController public class CloudProviderController { @GetMapping("/provider/get/{message}") @HystrixCommand(fallbackMethod = "hystrix") public String get(@PathVariable("message") String message){ return "进入服务熔断保护服务"; // throw new RuntimeException("熔断服务,进入服务熔断方法hystrix"); } public String hystrix(@PathVariable("message") String message){ return "进入服务熔断"; } }如果get方法上没有 @HystrixCommand(fallbackMethod = "hystrix")注解,服务监测页面将一直处于loading状态。
启动Eureka注册中心、服务提供者、HystrixDashboard。在服务监测的主界面查看被检测的服务,如图:
点击Monitor Stream按钮,进入服务监测界面:
监测图中主要的信息可分为:七色、一圈、一线
七色是指微服务被访问时的各种状态和状态数一圈表示微服务的访问频率,频率越高,表示访问量越大一线会随着微服务被访问数量编程曲线如果我们频繁访问http://localhost:8089/provider/get/1234服务,看一下界面的变化:
随着我们访问频率的增高,一圈变大,一线也开始上扬,当访问次数下降后,圈会变小,线也开始下行。这样我们就能很形象的观察到当前微服务的访问频率和服务状态。如下图,访问停止后:
注意:初次处理服务监控时,可能出现监控见面一直处于loading状态,对此可以参考文章:https://blog.csdn.net/zhoushimiao1990/article/details/90487176
