SpringCloud实战九:Spring Cloud Hystrix Dashboard 与 Turbine

Hystrix Dashboard断路器监控,是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。通过配置Hystrix Dashboard,我们可以通过浏览页面看运行情况
上一张介绍了Hystrix,Hystrix Dashboard断路器监控,本章直接上代码,看看监控页面,基于上一篇的代码,在pom.xml中引入:

  • 1.引入actuator与dashboard依赖
<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>
  • 2.在application.properties中添加配置,现在的actuator需要手动配置,暴露端点
# 暴露监控端点
management.endpoints.web.exposure.include=*
  • 3.在启动主类上添加 @EnableHystrixDashboard 与 @EnableHystrix 注解
@RestController
@EnableHystrixDashboard
@EnableHystrix
@SpringBootApplication
public class HystrixDemoApplication {

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

    // hystrix默认1秒超时,下面的随机方法可能让线程睡3秒,从而引发hystrix熔断降级,
    // 观察dashboard界面,有百分比与圆点为红色则说明有熔断
    @HystrixCommand(fallbackMethod = "getNameFallback" ,
     threadPoolKey = "hystrixDemoThreadPool",
     threadPoolProperties = {
            @HystrixProperty(name = "coreSize", value = "30"),
            @HystrixProperty(name = "maxQueueSize", value = "10")
     })
    @RequestMapping("/test")
    public String test(String username){
        randomlyRunLong();
        return "randomlyRunLong"+username;
    }

    // 1/3的机率会让线程睡3秒
    private void randomlyRunLong() {
        Random rand = new Random();
        int randomNum = rand.nextInt(3) + 1;
        if (randomNum == 3) sleep();
    }

    //线程睡3秒
    private void sleep() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 出错后会调用该降级方法,返回指定的信息
     *
     * @param username
     * @return
     */
    public String getNameFallback(String username) {
        return " this username is not exist ";
    }
}
  • 4.启动项目,访问 http://localhost:8080/hystrix ,注意红色框框中的hystrix.stream流地址需要加actuator:
    在上面的文本框中输入:http://localhost:8080/actuator/hystrix.stream ,再点击下面的 Monitor Stream按钮,进入dashboard监控页面
    在这里插入图片描述
  • 5.访问 http://localhost:8080/test ,多刷新几次,再观察 dashboard 监控页面
    在这里插入图片描述
    对监控图的一些数据进行简要解释:
  • test:为请求的名称,HystrixCommand注解上的方法
  • 12为成功请求数
  • 6为超时请求数
  • 33%为10秒内错误百分比
  • Host为单机请求率
  • Cluster为集群请求率
  • Cirecuit为断路器状态,Closed为闭合,表示没有进行熔断
  • Pool Size为20,设置线程池是30,因为请求量小,没有达到最大值
  • Queue Size为5,设置的10,因为请求量小,没有达到最大值

好了,完成了Hystrix Dashboard 单机监控页面,在微服务环境中,Dashboard往往依赖 Turbine,它是一个聚合服务,因为我们的应用程序一般是以集群的方式部署的,一个服务有很多应用实例,我们可以把hystrix单个的流放到dashboard中查看,但是更需要看集群流量的情况,请求的错误数和成功数需要聚合在一起相加后对外显示的,那么这个时候流需要做聚合

代码已上传至码云,源码,项目使用的版本信息如下:

- SpringBoot 2.0.6.RELEASE
- SpringCloud Finchley.SR2
上面是hystrix dashboard单机监控,下面继续集成 Turbine 聚合流,再输出到 dashboard 中,显示集群信息, Turbine需要依赖 Eureka 注册中心,通过eureka注册中心获取要监控服务的所有实例地址,然后聚合流传到 dashboard 中显示出来,下面是聚合架构图:

在这里插入图片描述

项目环境,只演示turbine代码,其余项目代码都有展示:

  • 1.eureka 注册中心(前面篇幅中有源码)
  • 2.hystrix 项目(上面的单机hystrix项目)
  • 3.turbine 项目
1.改造上面 hystrix-demo 项目
  • 引入注册中心:
<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	</dependency>
  • 修改配置文件,将hystrix-demo注册到注册中心
# 暴露监控端点
management.endpoints.web.exposure.include=*
server.port=9700
spring.application.name=hystrix-service
eureka.instance.prefer-ip-address=true
#配置eureka-server security的账户信息
eureka.client.serviceUrl.defaultZone=http://zy:zy123@localhost:10025/eureka/
  • 在启动主类上添加 @EnableDiscoveryClient 注解
2.新建 turbine-server 项目
  • pom.xml依赖如下:
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</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-netflix-hystrix-dashboard</artifactId>
</dependency>
  • 添加配置,指定turbine聚合eureka中的那些服务实例
server.port=9800
spring.application.name=turbine-service

# 暴露监控端点
management.endpoints.web.exposure.include=*

#appConfig 配置Eureka中的serviceId列表,表明监控哪些服务,多个服务id用 , 逗号隔开
turbine.appConfig=hystrix-service
turbine.aggregator.clusterConfig= default
turbine.clusterNameExpression= new String("default")
        
eureka.instance.prefer-ip-address=true
#配置eureka-server security的账户信息
eureka.client.serviceUrl.defaultZone=http://zy:zy123@localhost:10025/eureka/
  • 在启动主类上添加注解:
@EnableHystrixDashboard
@EnableTurbine
@EnableDiscoveryClient
@SpringBootApplication
public class TurbineServerApplication {

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

}
3.分别启动 eureka、hystrix-service、turbine-server
  • 1.访问 http://localhost:10025/ ,可看到 hystrix-service、turbine-service已注册到eureka,同时 hystrix-service启动了2个节点,分别在9700、9701端口
    在这里插入图片描述
  • 2.访问 http://localhost:9800/hystrix ,输入 http://localhost:9800/turbine.stream ,点击Monitor Stream按钮,进入监控页面
    在这里插入图片描述
  • 3.因hystrix-service开启了2个节点,因此在浏览器中开个tab,不停访问 http://localhost:9700/testhttp://localhost:9701/test
    在这里插入图片描述
  • 4.随着不停访问 hystrix-service,可以在监控页面看到请求信息在不停变化,和上面 单机监控最大的区别是 Hosts是2,因为开启了2个实例,通过turbine,把2个实例的访问请求聚合后显示出来
    在这里插入图片描述
  • 5.还可以访问 http://localhost:9800/turbine.stream ,可看到 turbine 在源源不断的把流吐出来,如果有请求,就是一大堆数据,没有请求就是 ping data: {“reportingHostsLast10Seconds”:2,“name”:“meta”,“type”:“meta”,“timestamp”:1546411776596}
    在这里插入图片描述

好了,Turbine的集成已经完成了,turbine 通过 eureka 把 hystrix 应用的 stream 信息聚合起来,到 dashboard 中进行显示

代码已上传至码云,源码,项目使用的版本信息如下:

- SpringBoot 2.0.6.RELEASE
- SpringCloud Finchley.SR2
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值