SpringCloud (六) --------- Hystrix Dashboard 与 Hystrix Turbine


一、 Hystrix Dashboard

Hystrix 仪表盘 (Hystrix Dashboard),就像汽车的仪表盘实时显示汽车的各项数据一样,Hystrix 仪表盘主要用来监控Hystrix 的实时运行状态,通过它我们可以看到 Hystrix 的各项指标信息,从而快速发现系统中存在的问题进而解决它。

要使用 Hystrix 仪表盘功能,我们首先需要有一个 Hystrix Dashboard 项目,这个功能我们可以在原来的消费者应用上添加,让原来的消费者应用具备 Hystrix 仪表盘功能,但一般地,微服务架构思想是推崇服务的拆分,Hystrix Dashboard 也是一个服务,所以通常会单独创建一个新的工程专门用做 Hystrix Dashboard 服务。

搭建 Hystrix Dashboard 服务步骤

A、创建一个普通的 Spring Boot 工程

创建一个名为 springcloud-hystrix-dashboard 的 Spring Boot 工程,建立好基本的结构和配置。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

B、添加相关依赖

在创建好的 Spring Boot 项目的 pom.xml 文件中添加相关依赖,如下:

<!-- spring-cloud-starter-netflix-hystrix-dashboard -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>

C、入口类上添加注解

//添加好依赖之后,在入口类上添加 @EnableHystrixDashboard 注解开启仪表盘功能,如下: 

package com.fancy.springcloudhystrixdashboard;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableHystrixDashboard
public class SpringcloudHystrixDashboardApplication {

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

}

D、属性配置

最后,我配置一下 application.properties 文件,如下:

server.port=3721
#防止 Eureka 自己注册自己
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

在这里插入图片描述
Hystrix 仪表盘工程已经创建好了,现在我们需要有一个服务,让这个服务提供
一个路径为 /actuator/hystrix.stream 接口,然后就可以使用 Hystrix 仪表盘来对该服务进行监控了。

改造消费者项目

我们改造消费者服务,让其能提供 /actuator/hystrix.stream 接口,步骤如下:

A、消费者项目需要有 hystrix 的依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-netflix-hystrix</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>

B、添加一个 spring boot 的服务监控依赖

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

C、配置文件需要配置 spring boot 监控端点的访问权限

management.endpoints.web.exposure.include=*

这个是用来暴露 endpoints 的,由于 endpoints 中会包含很多敏感信息,除了 health 和 info 两个支持直接访问外,其他的默认不能直接访问,所以我们让它都能访问,或者指定:

management.endpoints.web.exposure.include=hystrix.stream 

D、访问入 http://localhost:8081/actuator/hystrix.stream

注意:这里有一个细节需要注意,要访问 /hystrix.stream 接口,首先得访问 consumer 工程中的任意一个其他接口,否则直接访问 /hystrix.stream 接口时会输出出一连串的 ping: ping: …,先访问 consumer 中的任意一个其他接口, 然后再访问 /hystrix.stream 接口即可。

在这里插入图片描述

在这里插入图片描述

二、Spring Cloud Hystrix Turbine

Hystrix Dashboard 前面已经知道了,它的主要功能是可以对某一项微服务进行监控,但真实情况下,不可能只对一个微服务进行监控,我们有很多微服务,所以我们需要对很多微服务进行监控,这个时候就需要使用到turbine [ˈtɜːbaɪn] 来完成。

单个 hystrix 服务的监控(如下图):

在这里插入图片描述

多个 hystrix 服务的监控(如下图):
在这里插入图片描述

搭建步骤

准备一个 turbine 模块
A、添加依赖

<!-- spring-cloud-starter-netflix-turbine -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>

B、配置文件

server.port=3722

#不向注册中心注册自己
eureka.client.register-with-eureka=false
#eureka注册中心的连接地址
eureka.client.service-url.defaultZone=http://192.168.10.128:8761/eureka,http://192.168.10.128:8762/eureka,http://192.168.10.128:8763/eureka

#配置turbine
turbine.app-config=34-SPRINGCLOUD-SERVICE-PORTAL,34-SPRINGCLOUD-SERVICE-PORTAL-2
#需要有这个,没有的话聚合不了多个项目
turbine.cluster-name-expression="default"

C、在 main 方法的入口类上添加注解

@EnableTurbine //开启turbine
@SpringBootApplication
public class TurbineApplication {
    public static void main(String[] args) {
        SpringApplication.run(TurbineApplication.class, args);
    }
}

接下来为了能对多个使用了hystrix的项目进行监控,我们再准备一份项目,并且里面使用了hystrix:springcloud-service-portal

注意点就是要先访问一下各个使用了 hystrix 的优熔断功能的接口,然后再测试turbine,否则你不访问的话,测试是不会有数据的。

http://localhost:8080/cloud/goodsFeignHystrix 
http://localhost:8081/cloud/goodsLimit 
http://localhost:8080/actuator/hystrix.stream 
http://localhost:8081/actuator/hystrix.stream 
http://localhost:3722/turbine.stream
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在森林中麋了鹿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值