Hystrix监控中心

1. Spring cloud监控

spring cloud可以借用hystrix监控功能对请求进行实时的监控,比如每秒执行的请求数,成功数,查看熔断器状态等等。

搭建一个监控的功能会涉及到下面三个模块

  1. hystrix stream
  2. turbine stream
  3. hystrix dashboard

1.1 hystrix stream

hystrix stream是一个hystrix暴露的api,用于查看服务的请求状态。

** 引入依赖**
要引入hystrix stream监控需要依赖hystrix-metrics-event-stream模块,该模块已经包含在spring-cloud-stater-netflix-hystrix依赖里面了。
另外,还需要springboot的监控模块, spring-boot-starter-actuator。所以应该引入下面两个依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

**在启动类中添加


然后可以通过http://localhost:port/actuator/hystrix.stream查看了。它会在浏览器中直接返回json数据。

### 1.2 turbine stream
每个微服务都可以通过hystrix暴露hystrix.stream接口,当微服务很多的情况下,需要去访问各个微服务的hystrix.stream接口,效率很低。
而turbine stream可以从各个微服务收集hystrix.stream的数据,然后用户只需要访问一个接口即可。

**turbin stream的原理**
turbin stream会先去到eureka服务器,拿到所有微服务的列表,继而可以知道各个微服务的hystrix.stream.

**引入依赖**

org.springframework.cloud spring-cloud-starter-netflix-turbine

**修改配置文件**

turbine:

假设有两个微服务,它们的application name分别是order-service, product-service

app-config: order-service,product-service

配置文件中需要配置eureka server的地址信息。

**在启动类中使用```@EnableTurbine```注解**

然后启动成功之后可以通过
http://localhost:port/turbine.stream 查看了


### 1.3 hystrix dashboard
不论是hystrix stream还是turbine stream,它们返回的都是json的数据格式,分析起来很不妨表。

而hystrix dashboard提供了一个图形话界面,用于转换hystrix stream或者turbine stream的数据,以图形的格式呈现。

**引入依赖**
org.springframework.cloud spring-cloud-starter-netflix-hystrix-dashboard ```

使用@EnableHystrixDashboard注解

启动成功之后就可以访问
http://localhost:port/hystrix 看到dashboard的界面了

然后在界面的输入框中填入要查看的地址就行了,比如
http://localhost:8083/turbine.stream

一般在使用中,可以把turbine stream和hystrix dashboard部署在一个单独的服务器上

总的来说,它们三者的关系如图所示

avatar

原文:大专栏  Hystrix监控中心


猜你喜欢

转载自www.cnblogs.com/petewell/p/11615222.html