SpringCloud Hystrix-Dashboard仪表盘的实现

 更新时间:2019年08月06日 14:34:02   作者:寻找风口的猪  
这篇文章主要介绍了SpringCloud Hystrix-Dashboard仪表盘的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

Hystrix Dashboard,它主要用来实时监控Hystrix的各项指标信息。通过Hystrix Dashboard反馈的实时信息,可以帮助我们快速发现系统中存在的问题。下面通过一个例子来学习。

一、新建一个Spring Cloud 项目,命名为hystrix-dashboard

1.1在pom.xml引入相关的依赖

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

1.2在spring boot 的启动类上面引入注解@EnableHystrixDashboard,启用Hystrix Dashboard功能。

package org.hope.hystrix.dashboard;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@EnableHystrixDashboard
@SpringCloudApplication
public class HystrixDashboardApplication {
 public static void main(String[] args) {
  SpringApplication.run(HystrixDashboardApplication.class, args);
 }

}

1.3修改配置文件application.properties

spring.application.name=hystrix-dashboard
server.port=2001

1.4启动应用,然后再浏览器中输入http://localhost:2001/hystrix可以看到如下界面

通过Hystrix Dashboard主页面的文字介绍,我们可以知道,Hystrix Dashboard共支持三种不同的监控方式

☞默认的集群监控:通过URL:http://turbine-hostname:port/turbine.stream开启,实现对默认集群的监控。

☞指定的集群监控:通过URL:http://turbine-hostname:port/turbine.stream?cluster=[clusterName]开启,实现对clusterName集群的监控。

☞单体应用的监控:通过URL:http://hystrix-app:port/hystrix.stream开启,实现对具体某个服务实例的监控。

☞Delay:控制服务器上轮询监控信息的延迟时间,默认为2000毫秒,可以通过配置该属性来降低客户端的网络和CPU消耗。

☞Title:该参数可以展示合适的标题。

二、要有一个eureka-server用来提供eureka的服务注册中心,在码云上有,可以作为参考。此处不再粘代码。

三、要有一个eureka-service来提供服务,工程名为hello-service,项目地址同上。

四、新建一个服务被监控的工程,工程名为ribbon-customer。

4.1pom.xml引入相关依赖

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

4.2在启动类上添加@EnableCircuitBreaker 开启断路器功能

package com.didispace;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableCircuitBreaker //开启断路器功能
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {

 @Bean
 @LoadBalanced
 RestTemplate restTemplate() {
  return new RestTemplate();
 }
 public static void main(String[] args) {
  SpringApplication.run(ConsumerApplication.class, args);
 }

}

4.3 RestController

package com.didispace.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {

 @Autowired
 HelloService helloService;

 @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
 public String helloConsumer() {
  return helloService.hello();
 }

}

4.4 application.properties配置文件

spring.application.name=ribbon-consumer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000

通过上面的步骤,已经基本完成了准备工作,下面我们进行测试。

1.启动eureka-server

2.启动hello-service

3.启动ribbon-customer

4.启动hystrix-dashboard

5.在浏览器输入http://localhost:2001/hystrix

6.在浏览器的新窗口输入http://localhost:9000/ribbon-consumer

7.在Hystrix-Dashboard的主界面上输入: http://localhost:9000/hystrix.stream然后点击 Monitor Stream按钮

在监控的界面有两个重要的图形信息:一个实心圆和一条曲线。

  • 实心圆:1、通过颜色的变化代表了实例的健康程度,健康程度从绿色、黄色、橙色、红色递减。2、通过大小表示请求流量发生变化,流量越大该实心圆就越大。所以可以在大量的实例中快速发现故障实例和高压实例。
  • 曲线:用来记录2分钟内流浪的相对变化,可以通过它来观察流量的上升和下降趋势。

注意:当使用Hystrix Board来监控Spring Cloud Zuul构建的API网关时,Thread Pool信息会一直处于Loading状态。这是由于Zuul默认会使用信号量来实现隔离,只有通过Hystrix配置把隔离机制改成为线程池的方式才能够得以展示。

参考:

[1]《Spring Cloud微服务实战》,翟永超

[2]博客,纯洁的微笑,https://www.jb51.net/article/167053.htm

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Spring MVC常用客户端参数接收方式详解

    Spring MVC常用客户端参数接收方式详解

    这篇文章主要介绍了Spring MVC常用客户端参数接收方式详解,文章主要介绍了几种方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • Jenkins的安装配置详解

    Jenkins的安装配置详解

    这篇文章主要介绍了Jenkins的安装配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • 关于logBack配置日志文件及编码配置的问题

    关于logBack配置日志文件及编码配置的问题

    这篇文章主要介绍了logBack配置日志文件及编码配置的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • java 实现判断回文数字的实例代码

    java 实现判断回文数字的实例代码

    这篇文章主要介绍了java 实现判断回文数字的实例代码的相关资料,需要的朋友可以参考下
    2017-03-03
  • 使用JAVA命令运行JAR包以及日志输出详解

    使用JAVA命令运行JAR包以及日志输出详解

    这篇文章主要给大家介绍了关于使用JAVA命令运行JAR包以及日志输出的相关资料,文中通过代码示例介绍的非常详细,对大家学习或者使用java具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-09-09
  • 源码解读Spring-Integration执行过程

    源码解读Spring-Integration执行过程

    Spring-Integration基于Spring,在应用程序中启用了轻量级消息传递,并支持通过声明式适配器与外部系统集成,今天主要是看个简单的hello word进来分析下整个执行过程,感兴趣的朋友一起看看吧
    2021-06-06
  • 在安卓系统中插入表情到光标位置的代码详解

    在安卓系统中插入表情到光标位置的代码详解

    这篇文章主要介绍了在安卓系统中插入表情到光标位置的代码详解,利用Java代码在EditText控件中实现,需要的朋友可以参考下
    2015-07-07
  • JAVA后端学习精华之网络通信项目进阶

    JAVA后端学习精华之网络通信项目进阶

    不同项目之间的通信方式分为,http、socket、webservice;其中socket通信的效率最高,youtube就采用的是原始的socket通信,他们信奉的原则是简单有效
    2021-09-09
  • Java IO流和文件操作实现过程解析

    Java IO流和文件操作实现过程解析

    这篇文章主要介绍了Java IO流和文件操作实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • java简单实现数组的增删改查方法

    java简单实现数组的增删改查方法

    这篇文章主要介绍了Java数组的增删改查的示例,帮助大家更好的利用Java处理数据,感兴趣的朋友可以了解下,希望能给你带来帮助
    2021-07-07

最新评论