一、官方文档阅读

服务启动后,可以通过/health和hystrix.stream查看效果,实际上,访问上述两个地址,会出现404,这是因为spring boot版本的问题,

我在这里使用的springboot的版本是:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>

使用的spring cloud的版本是

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>

这里,需要在movie服务的配置文件中,增加:

management:
endpoints:
web:
exposure:
include: ["health","info"]

并且在启动类中,增加:

    @Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}

这样设置后,可以开放对地址地址的访问,具体开放参数,可以查看springboot的文档:

https://docs.spring.io/spring-boot/docs/2.1.4.RELEASE/reference/htmlsingle/#boot-features-security-actuator

当中有很具体的说明。

二、实践

启动user,eureka,movie服务,

访问movie服务:http://localhost:8010/actuator/hystrix.stream

在未发生实际请求时,会一直ping:ping:

当我们访问服务,请求user服务后,

就会一直刷日志,这个日志很难截图,因此,hystrix提供了一个图形化界面,供我们进行查看,分析。

首先,在pom文件中,增加相应的依赖:

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

在启动类上 增加注解:@EnableHystrixDashboard ,启动项目,并访问如下地址:http://localhost:8010/hystrix

在上面的文本框中,填入hystrix.stream,然后设置一个标题,如下:

点击monittor stream ,

然后访问服务,查看数据变化,

实际上,一个真实的项目,有很多个微服务,这种监控单点的实例,其实是没有作用的。

查看单个实例的Hystrix数据在系统整体运行状况方面不是很有用。 Turbine是一个应用程序,它将所有相关的/hystrix.stream端点聚合到一个组合的/turbine.stream中,以便在Hystrix仪表板中使用。 个别实例位于尤里卡。 运行Turbine需要使用@EnableTurbine注释来注释主类(例如,使用spring-cloud-starter-netflix-turbine来设置类路径)。 Turbine 1 wiki中所有记录的配置属性均适用。 唯一的区别是turbine.instanceUrlSuffix不需要前置端口,因为除非turbine.instanceInsertPort = false,否则会自动处理。

默认情况下,Turbine通过在Eureka中查找其hostName和端口条目,然后将/hystrix.stream附加到其上来查找已注册实例上的/hystrix.stream端点。 如果实例的元数据包含management.port,则使用它来代替/hystrix.stream端点的端口值。 默认情况下,名为management.port的元数据条目等于management.port配置属性。 可以通过以下配置覆盖它:

eureka:
instance:
metadata-map:
management.port: ${management.port:8081}

通过这个配置呢,可以修改hystrix.stream的端口,

spring cloud 提供了turbine,下面我们先搭建一个turbine server

turbine是从eureka上获取节点数据的,因此,turbine也需要注册到eureka上,首先,引入相应的依赖:

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

在启动类上,增加如下的注解:

@EnableTurbine

如果名称是默认值,则可以省略cluster参数。 cluster参数必须与turbine.aggregator.clusterConfig中的条目匹配。 从尤里卡返回的值是大写的。 因此,如果有一个名为Customers的应用程序在Eureka注册,则以下示例有效:

我们在配置文件中,增加类似的配置:

 spring:
application:
name: microservice-hystrix-turbine
server:
port: 8031 eureka:
client:
serviceUrl:
defaultZone: http://user:password123@192.168.1.114:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}} turbine:
aggregator:
clusterConfig: MICROSERVICE-CONSUMER-MOVIE-FEGIN-WITH-HYSTRIX
appConfig: microservice-consumer-movie-fegin-with-hystrix

启动eureka,user,dasdboard,movie服务

访问:http://localhost:8031/turbine.stream?cluster=MICROSERVICE-CONSUMER-MOVIE-FEGIN-WITH-HYSTRIX

就能看到打印的结果,

下载把地址,放入仪表盘,dasdboard

下面访问movie服务,查看访问结果

这个监控,有一定的延时,我们再次启动一个movie服务,相同的服务名称,

然后再次查看仪表盘,可以查看到两个节点的监控都有了,但是后起的那个监控,会延迟一段时间,才会被仪表盘监控到

下面看一下turbine是如何监控多个不同的服务的

修改turbine的配置文件:

turbine:
aggregator:
clusterConfig: default
appConfig: microservice-consumer-movie-fegin-with-hystrix,microservice-consumer-movie
clusterNameExpression: "'default'"

这样,使用了一个“默认”的集群,现在直接访问turbine的http://localhost:8031/turbine.stream

就能看到以下数据:

下面将turbine的地址填入dasdboard,

通过浏览器,分别请求另外两个数据,查看仪表盘结果

就可以发现,仪表盘监控了两个项目。

默认情况下,trubine在注册的实例上查找/hystrix.stream端点,方法是在Eureka中查找其homePageUrl 条目。然后将/hystrix.stream附加

最新文章

  1. Axure 自适应视图
  2. 状态模式(State Pattern)
  3. iOS-触摸事件、手势识别、摇晃事件、耳机线控
  4. Set和Map
  5. Javascript模块化编程之路——(require.js)
  6. .Net 应用程序如何在32位操作系统下申请超过2G的内存【转】
  7. linux解压zip、bz、bz2、z、gz、tar(解包)
  8. angular+rails集成实战
  9. Hibernate 中createQuery与createSQLQuery
  10. EF架构使用随机排序
  11. 动画——animation部分
  12. 用subline text写PHP后台服务器POST请求
  13. Python——Web.py詳解
  14. Swift 3中新的访问控制关键字fileprivate和open
  15. 剑指offer三从头到尾打印链表
  16. man page ,info page 和/usr/share/doc/整理自鸟哥
  17. 《码出高效 Java开发手册》第五章 异常与日志
  18. 使用 Hexo + github 搭建个人博客
  19. hiho 1015 KMP算法 &amp;&amp; CF 625 B. War of the Corporations
  20. Web檢測

热门文章

  1. Linux 系统报错 rcu_preempt detected stalls on CPUs/tasks
  2. 配置RIPng(PT)
  3. 简单Hash函数LongHash
  4. 事务回滚 try catch
  5. Hexo:创建属于你自己的博客
  6. eclipse的常用设置(空间新建后需要的配置)
  7. 使用Autoencoder进行降维
  8. 71.纯 CSS 创作一个跳 8 字型舞的 loader
  9. PHP SoapClient 调用与鉴权,以及对Java和C# 的webservice的兼容处理
  10. QT项目添加现有文件后不能运行,MFC在类视图中自动隐藏类