七、Spring Colud Turbine详解(一):Turbine集群监控

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/makyan/article/details/88692673

7.1.概述
1、简介
Turbine是聚合服务器发送事件流数据的一个工具,Hystrix的监控中,只能监控单个节点,
实际生产中都为集群,因此可以通过Turbine来监控集群下Hystrix的metrics情况
Turbine的github地址:https://github.com/Netflix/Turbine


2、使用场景
在复杂的分布式系统中,相同服务的结点经常需要部署上百甚至上千个,很多时候,运维人员希望能够把相同服务的节点状态以一个整体集群的形式展现出来,这样可以更好的把握整个系统的状态。 为此,Netflix又提供了一个开源项目Turbine来提供把多个
hystrix.stream的内容聚合为一个数据源供Dashboard展示。
Turbine使用了Netflix的另一个开源项目Archaius来做配置文件的管理,其提供了非常强大的配置文件管理策略
Archaius的https://github.com/Netflix/archaius


3、turbine 集群配置参数解释:


turbine.aggregator.cluster-config: 
#指定聚合哪些集群,多个使用","分割,默认为default。

turbine.app-config: 
#配置监控服务的列表,表明监控哪些服务多个使用","分割。

 turbine.clusterNameExpression
#用于指定集群名称,当服务数量非常多的时候。可以启动多个Turbine服务来构建不同的聚合集群,
#而该参数可以用来区分这些不同的聚合集群,同时该参数值可以再Hystrix仪表盘中用来定位不同的聚合集群,
#只需在Hystrix Stream的URL中通过cluster参数来指定。
#(1)clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称
#(2)当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default
#(3) 当clusterNameExpression: metadata['cluster']时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC。

turbine.combine-host-port:
#参数设置为true,可以让同一主机上的服务通过主机名与端口号的组合来进行区分,
#默认情况下会以host来区分不同的服务,这会使得在本机调试的时候,本机上的不同服务聚合成一个服务来统计。
#turbine.instanceUrlSuffix=/hystrix.stream, #修改Turbine的收集端点

7.2. 集群监控
1、创建项目futurecloud-turbine,添加依赖


<!--将此项目变为web项目-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>
<!--添加eureka 客户端依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.0.2.RELEASE</version>
</dependency>

<!--添加Turbine依赖,Turbine 汇集监控信息-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
    <version>2.0.2.RELEASE</version>
</dependency>
<!--将聚合后的信息提供给Hystrix Dashboard来集中展示和监控-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    <version>2.0.2.RELEASE</version>
</dependency>

2、spring boot main类上添加注解:

@EnableEurekaClient :注册eureka
@EnableHystrixDashboard :开启HystrixDashboard
@EnableTurbine :启动Turbine

代码如下:


package com.futurecloud.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.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@SpringBootApplication
@EnableEurekaClient//声明为eureka 客户端
@EnableHystrixDashboard
@EnableTurbine //启动Turbine
public class FuturecloudTurbineApplication
{
    public static void main( String[] args )
    {
        SpringApplication.run(FuturecloudTurbineApplication.class,args);
    }
}

2、配置Hystrix 集群


为了做实验,我们需要多个集群项目,
将之前的项目futurecloud-hystrix-cluster复制3份(被监控项目要启动hystrix),形成四个集群项目
futurecloud-hystrix-cluster1、futurecloud-hystrix-cluster2、futurecloud-hystrix-cluster3


futurecloud-hystrix-cluster1 的application.yml配置如下:


server:
  port: 8909
spring:
  application:
    name: futurecloud-hystrix-cluster1
#将此服务注册到eureka 服务上
eureka:
  client:
    serviceUrl:
      defaultZone: http://user:123@localhost:10000/eureka
  instance:
    prefer-ip-address: true  #将注册到eureka服务的实例使用ip地址

#Feign日志的配置
logging:
  level:
    com.futurecloud.feign.interfaces.CustomFeignClient: DEBUG

futurecloud-hystrix-cluster1 的 controller如下:


package com.futurecloud.hystrix.controller;
import com.futurecloud.hystrix.bean.User;
import com.futurecloud.hystrix.feignClient.FeignClientInterfaces;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
public class FuturecloudHystrixController {

    @Autowired
   private FeignClientInterfaces feignClientInterfaces;

    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    public User getUserById(@PathVariable("id") Long id){
        User user = feignClientInterfaces.getUserById(id);
        return user;
    }

    /**
     * 1、Hystrix的基本使用:
     * 使用Hystrix熔断器,当调用findUserById失败后,调用forbackFindUserById方法
     * @param id
     * @return
     */
    @HystrixCommand(fallbackMethod = "fallbackCluster1")
    @RequestMapping(value = "/cluster1/{id}",method = RequestMethod.GET)
    public User cluster1(@PathVariable("id") Long id){
        User user = feignClientInterfaces.findUserById(id);
        int a = 4/0;
        return user;
    }
    public User fallbackCluster1(Long id){
        User user = new User();
        user.setId(-400L);
        user.setUsername("hystrix-fallback");
        user.setMail("[email protected]");
        user.setPhone("13838384381");
        user.setCreateDate(new Date());
        return  user;
    }
}

futurecloud-hystrix-cluster2 、futurecloud-hystrix-cluster3 配置同上,
只是端口、服务、controller方法名改变


3、配置futurecloud-turbine项目的application.yml中使用配置


server:
  port: 9988
spring:
  application:
    name: futurecloud-turbine
#将此服务注册到eureka 服务上
eureka:
  client:
    serviceUrl:
      defaultZone: http://user:123@localhost:10000/eureka
  instance:
    prefer-ip-address: true  #将注册到eureka服务的实例使用ip地址

turbine:
  aggregator:
    clusterConfig: default #指定聚合哪些集群,多个使用","分割,默认为default。
  appConfig: futurecloud-hystrix-cluster1,futurecloud-hystrix-cluster2,futurecloud-hystrix-cluster3   #配置监控服务的列表,表明监控哪些服务多个使用","分割
  # 指定集群名称
  cluster-name-expression: "'default'"
  #参数设置为true,可以让同一主机上的服务通过主机名与端口号的组合来进行区分,
  #默认情况下会以host来区分不同的服务,这会使得在本机调试的时候,本机上的不同服务聚合成一个服务来统计。
  combine-host-port: true

4、依次启动服务:

futurecloud-service
futurecloud-user
futurecloud-hystrix-cluster1
futurecloud-hystrix-cluster2
futurecloud-hystrix-cluster3
futurecloud-turbine

5、解决
启动futurecloud-turbine报错:

”path”:”/actuator/hystrix.stream”,”status”:404,”error”:”Not Found”

这是因为springboot2.x使用了endpoint,解决办法在前面章节《Hystrix的健康监测》中提到,
我们采用在Spring boot main class中配置Hystrix,详细代码如下:


/**
 * 配置Hystrix ,使其显示健康监测信息
 * @return
 */
@Bean
public ServletRegistrationBean getServlet() {
    HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
    registrationBean.setLoadOnStartup(1);
    registrationBean.addUrlMappings("/actuator/health","/actuator/hystrix.stream");
    registrationBean.setName("HystrixMetricsStreamServlet");
    return registrationBean;
}

访问 http://localhost:9988/hystrix ,出现HystrixDashBoard界面,
在界面中输入 http://localhost:9988/turbine.stream
注意,输入的是Hystrix turbine本机的host + port + /turbine.stream


Turbine集群监控这里插入图片描述

猜你喜欢

转载自blog.csdn.net/makyan/article/details/88692673