Turbine来实现Hystrix Dashboard的集群监控

1.Spring Cloud提供的Turbine就是将监控信息进行聚合,并将聚合的监控信息提供给Hystrix Dashboard进行集中的展示和监控的组件,使用它可以实现监控整个集群的效果。

新建项目:ms-turbine-consumer

1.pom文件:

 <!-- 注册eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- 打印日志 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--  web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- feigin的依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <!-- hystrix 的依赖  -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <!--hystrix command -->
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-javanica</artifactId>
            <version>1.5.12</version>
        </dependency>
        <!-- dashboard  -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
        </dependency>
        <!-- turbine-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-turbine</artifactId>
        </dependency>

2.application.yml文件:

下面的turbine的几个参数的解释:
(1)turbine.app-config指定要监控的应用名字,多个可以使用逗号隔开。上面我们监控的是“ms-hystrix-consumer”服务。
(2)turbine.cluster-name-expression=default,表示集群的名字为default 
(3)turbine.combine-host-port=true表示同一主机上的服务通过host和port的组合来进行区分,默认情况下是使用host来区分,这样会使本地调试有问题。

server:
  port: 8007
spring:
  application:
    name: ms-turbine-consumer
eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://ljf:123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

turbine:
  aggregator:
    clusterConfig:  default
  appConfig: ms-hystrix-consumer
  clusterNameExpression: "'default'"

#设置查看指标
management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: "*"
        exclude: "-"

3.在启动类:添加turbine注解:

package com.ljf.weifuwu.springcloud.turbine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableTurbine
public class TurbineApp
{
    public static void main( String[] args )
    {
        SpringApplication.run(TurbineApp.class, args);
        System.out.println( "turbine启动起来了!" );
    }
}

4.启动服务:ms-eureka-center(8761)、ms-eureaka-provider(7901)、ms-hystrix-consumer(8004)、ms-hystrix-consumer(8005)、ms-hystrix-dashboard(8006)、ms-turbine-consumer(8007)

5.访问:

http://localhost:8004//hystrix-consumer/1

http://localhost:8005//hystrix-consumer/1

http://localhost:8007/turbine.stream

http://localhost:8006/hystrix

点击monitor stream:

 拼命刷新:http://localhost:8004//hystrix-consumer/1  ,http://localhost:8005//hystrix-consumer/1   之后的结果:

查看集群的状态 :http://localhost:8007/turbine.stream?cluster=default     

二修改 集群的名称,指定监控的服务 

1.修改项目ms-turbine-consumer的配置文件的turbine的配置:

turbine:
  aggregator:
    clusterConfig: ljf-turbine    #自定义
  app-config: ms-hystrix-consumer
  cluster-name-expression: metadata['cluster']
  combine-host-port: true

2.修改监控项目的元信息:在项目ms-hystrix-consumer修改配置文件:

 

3.重新启动这些服务:ms-eureka-center(8761)、ms-eureaka-provider(7901)、ms-hystrix-consumer(8004)、ms-hystrix-consumer(8005)、ms-hystrix-dashboard(8006)、ms-turbine-consumer(8007)

#查看这个集群的聚合信息,进行监控:http://localhost:8007/turbine.stream?cluster=ljf-turbine

放到dashboard查看:

最后,有一个疑问:为何http://localhost:8007/clusters,查询不到可用的clusters的信息呢????

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章