【微服务笔记11】微服务组件之Hystrix服务监控的三种方式

这篇文章,主要介绍微服务组件之Hystrix服务监控的三种方式。

目录

一、Hystrix服务监控

1.1、Actuator实现服务监控

(1)引入依赖

(2)开放hystrix.stream端点

(3)运行测试

1.2、dashboard实现服务监控

(1)引入依赖

(2)启动类启用dashboard

(3)添加dashboard配置

(4)运行测试

1.3、turbine实现服务监控

(1)创建turbine工程

(2)添加turbine配置

(3)启动类启用turbine

(4)运行测试


一、Hystrix服务监控

Hystrix组件提供了服务监控的功能,它可以监控每一次接口的调用情况,并且提供了一些可视化的查看界面,有三种方式可以查看监控情况,分别是:

  • 1、使用 Actuator 方式查看监控。
  • 2、使用Hystrix提供的dashboard查看监控。
  • 3、使用Hystrix提供的turbine查看监控。

1.1、Actuator实现服务监控

(1)引入依赖

  • 首先需要引入 hystrix 的依赖。
  • 其次还需要引入 actuator 的依赖,actuator 是SpringBoot提供的一个专门用于监控应用程序的starter启动器。
<!--hystrix 依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- 引入 actuator 依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

(2)开放hystrix.stream端点

  • 在application.yml配置文件中,添加如下内容:
# 监控相关属性
management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream # 开放 hystrix 监控访问的端口
  endpoint:
    hystrix:
      stream:
        enabled: true # 再次声明,开放 hystrix 监控访问的端口

(3)运行测试

启动工程,浏览器访问【http://IP地址:端口/actuator/hystrix.stream】地址,如下所示:

上图中一直显示【ping:】内容,这是hystrix处于监控状态,当有接口调用的时候,就会显示出具体的调用信息。

 到此,actuator实现服务监控就完成啦。

1.2、dashboard实现服务监控

actuator虽然可以实现hystrix的信息监控,但是这种方式不利于查看,满屏幕的字符,看着不太舒服,所以hystrix提供了一个可视化的界面,叫做:dashboard。

(1)引入依赖

<!-- 引入 hystrix 依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- 引入 actuator 依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 引入 dashboard 依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

(2)启动类启用dashboard

引入dashboard依赖之后,还需要主动的告诉SpringBoot要启用dashboard功能,在启动类上面使用【@EnableHystrixDashboard】注解即可。

// 启用 hystrix 的 dashboard 功能
@EnableHystrixDashboard

(3)添加dashboard配置

  • 在application.yml配置文件中,添加如下内容:
# 监控相关属性
management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream # 开放 hystrix 监控访问的端口
  endpoint:
    hystrix:
      stream:
        enabled: true
# 设置 dashboard 配置
hystrix:
  dashboard:
    proxy-stream-allow-list: 127.0.0.1,192.168.0.101 # 设置允许代理的地址

(4)运行测试

启动工程,浏览器访问【http://IP地址:端口/hystrix】地址,会看见如下页面:

在页面中,输入服务监控地址【http://IP地址:端口/actuator/hystrix.stream】,进入之后,就可以看到如下界面:

上图就是hystrix给我们提供的可视化监控界面。

1.3、turbine实现服务监控

dashboard已经很完美可以实现服务监控了,但是在微服务环境下,存在很多个应用程序,某一个应用程序都可以有一个dashboard用于服务监控,那么在这种情况下,就需要访问不同的微服务才可以查看不同的服务监控,这就不太方便了,所以可以使用turbine进行聚合监控(集群监控)。

所谓的聚合监控(集群监控),就是指:将多个微服务中的dashboard整合到一个监控页面里面,这样就只需要访问turbine就可以查看不同微服务的监控信息啦。

(1)创建turbine工程

这里我们新创建一个Springboot工程,专门用于turbine查看监控信息,并且引入turbine的依赖。

<!-- 引入 eureka 服务端依赖 -->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入 dashboard 依赖 -->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!-- 引入 turbine 依赖 -->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>

(2)添加turbine配置

# 配置 turbine
turbine:
  combine-host-port: true
  cluster-name-expression: "'default'"
  aggregator:
    cluster-config: default
  app-config: hystrix-consumer,hystrix-provider # 需要使用turbine进行监控的微服务名称,多个采用逗号隔开

# 设置 dashboard 配置
hystrix:
  dashboard:
    proxy-stream-allow-list: 127.0.0.1,192.168.0.101 # 设置允许代理的地址

(3)启动类启用turbine

package com.gitcode.hystrix;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

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

(4)运行测试

启动turbine工程,两个hystrix的工程,然后浏览器访问turbine工程的【http://IP地址:端口/hystrix】,此时如下图所示:

在上图中的输入框填写【http://IP地址:端口/turbine.stream】地址,进入监控页面,第一次进入时候由于还没有访问接口,所以这个时候还没有监控信息,可以先访问一些接口,在查看监控信息。

到此,Hystrix服务监控就介绍完啦。

综上,这篇文章结束了,主要介绍微服务组件之Hystrix服务监控的三种方式。

猜你喜欢

转载自blog.csdn.net/qq_39826207/article/details/130116691