从零开始学spring cloud(十一) -------- hystrix监控

 

 

1、官方文档阅读

服务启动后,能够经过/health和hystrix.stream查看效果,实际上,访问上述两个地址,会出现404,这是由于spring boot版本的问题, html

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

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

 

使用的spring cloud的版本是spring

<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"]

 

而且在启动类中,增长:springboot

    @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的文档:app

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

当中有很具体的说明。微服务

2、实践

启动user,eureka,movie服务,spa

访问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注册,则如下示例有效:

咱们在配置文件中,增长相似的配置:

 1 spring:
 2   application:
 3     name: microservice-hystrix-turbine
 4 server:
 5   port: 8031
 6 
 7 eureka:
 8   client:
 9     serviceUrl:
10       defaultZone: http://user:password123@192.168.1.114:8761/eureka
11   instance:
12     prefer-ip-address: true
13     instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
14 
15 turbine:
16   aggregator:
17     clusterConfig: MICROSERVICE-CONSUMER-MOVIE-FEGIN-WITH-HYSTRIX
18   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附加

相关文章
相关标签/搜索