Spring Cloud 2.x系列之断路器集群监控Turbine

简介: 前几篇已经实现了对单个服务实例的监控,当然在实际应用中,单个实例的监控数据没有多大的价值,我们其实更需要的是一个集群系统的监控信息,这时就需要引入Turbine。Turbine能够汇集监控信息,并将聚合后的信息提供给Hystrix Dashboard来集中展示和监控。

前几篇已经实现了对单个服务实例的监控,当然在实际应用中,单个实例的监控数据没有多大的价值,我们其实更需要的是一个集群系统的监控信息,这时就需要引入Turbine。Turbine能够汇集监控信息,并将聚合后的信息提供给Hystrix Dashboard来集中展示和监控。

本文将结合之前学习的注册中心Eureka、服务提供者Provider、断路器Hystrix和仪表盘Dashboard,学习断路器集群监控Turbine,最终整体架构如下:

2787132ce17a71ac797a158f103cac5eae35d511

项目说明:

注册中心:

    sc-eureka-server

服务提供者:

    sc-server-turbine-hystrix-dashboard-node1

    sc-server-turbine-hystrix-dashboard-node2

服务消费者、断路器:

    sc-client-turbine-hystrix-dashboard-node1

    sc-client-turbine-hystrix-dashboard-node2

集群监控、仪表盘:

    sc-client-turbine-hystrix

1、注册中心项目sc-eureka-server参考Spring Cloud 2.x系列之eureka注册中心单机

2、服务提供者项目sc-server-turbine-hystrix-dashboard-node1、sc-server-turbine-hystrix-dashboard-node2由项目sc-eureka-client-provider改造而来。 参考Spring Cloud 2.x系列之服务注册&服务提供者

sc-server-turbine-hystrix-dashboard-node1:

7b2820b4281118968bc92de67e052336e6e28526

sc-server-turbine-hystrix-dashboard-node2:

161db314bf75d7cbbd4613b2264711386c93d410

3、服务消费者、断路器项目sc-client-turbine-hystrix-dashboard-node1、sc-client-turbine-hystrix-dashboard-node2由项目sc-feign-hystrix-dashboard改造而来。

sc-client-turbine-hystrix-dashboard-node1

(1)bootstrap.yml修改

server:

  port: 5801

(2)application.yml文件修改

79a44ff3bbe3dd8dbd0f507583972fa087f858e6

(3)去掉springcloud启动类的EnableHystrixDashboard注解

sc-client-turbine-hystrix-dashboard-node2

(1)bootstrap.yml修改

server:

  port: 5802

(2)application.yml文件修改

6253b4dd86d52bdd156d047abd0eb9613ea9ba37

(3)去掉springcloud启动类的EnableHystrixDashboard注解

注: turbine只能监控Hystrix服务。不是Hystrix的服务不能监控。所以配置文件都配置了如下配置项

be03a2cf4ef6655e23740c2fe624a51691de7fbb

4、 新建集群监控、仪表盘项目sc-client-turbine-hystrix,对应的pom.xml文件如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>

   <groupId>spring-cloud</groupId>

   <artifactId>sc-client-turbine-hystrix</artifactId>

   <version>0.0.1-SNAPSHOT</version>

   <packaging>jar</packaging>


   <name>sc-client-turbine-hystrix</name>

   <url>http://maven.apache.org</url>



  <parent>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-parent</artifactId>

      <version>2.0.4.RELEASE</version>

   </parent>

   <dependencyManagement>

      <dependencies>

        <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-dependencies</artifactId>

           <version>Finchley.RELEASE</version>

           <type>pom</type>

           <scope>import</scope>

        </dependency>


      </dependencies>

   </dependencyManagement>



   <properties>

      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

      <maven.compiler.source>1.8</maven.compiler.source>

      <maven.compiler.target>1.8</maven.compiler.target>

   </properties>


   <dependencies>

   <!-- <dependency>

         <groupId>org.springframework.cloud</groupId>

          <artifactId>spring-cloud-starter-turbine</artifactId>

         <version>1.4.5.RELEASE</version>

      </dependency>-->


      <dependency>

          <groupId>org.springframework.cloud</groupId>

          <artifactId>spring-cloud-starter-netflix-turbine</artifactId>

      </dependency>


   <!-- <dependency>

         <groupId>org.springframework.cloud</groupId>

          <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>

         <version>1.4.5.RELEASE</version>

      </dependency>-->



      <dependency>

          <groupId>org.springframework.cloud</groupId>

          <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>

      </dependency>


      <dependency>

          <groupId>org.springframework.boot</groupId>

          <artifactId>spring-boot-starter-actuator</artifactId>

      </dependency>

      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

      </dependency>

   </dependencies>

</project>

5、 新建配置文件application.yml

spring:

  application:

    name: sc-client-turbine-hystrix

eureka:

  client:

    registerWithEureka: true #是否将自己注册到Eureka服务中,默认为true

    fetchRegistry: true #是否从Eureka中获取注册信息,默认为true

    serviceUrl:

      defaultZone:http://localhost:5001/eureka/  

server:

  port: 9091


turbine:

  appConfig: sc-client-turbine-hystrix-dashboard-node1,sc-client-turbine-hystrix-dashboard-node2 # 配置Eureka中的serviceId列表,表明监控哪些服务

  clusterNameExpression: newString("default")

  # 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

  instanceUrlSuffix: /hystrix.stream

  aggregator:

    clusterConfig: default  # 指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问

6、新建springboot启动类

package sc.client.turbine;



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
@EnableHystrixDashboard
@EnableTurbine
@EnableEurekaClient
public class TurbineApplication {



   public static void main(String[] args) {

        SpringApplication.run(TurbineApplication.class, args);

   }


}

7、按照如下先后顺序启动项目

(1)sc-eureka-server

(2)sc-server-turbine-hystrix-dashboard-node1

        sc-server-turbine-hystrix-dashboard-node2

(3)sc-client-turbine-hystrix-dashboard-node1

        sc-client-turbine-hystrix-dashboard-node2

(4)sc-client-turbine-hystrix

8、访问注册中心http://127.0.0.1:5001/确认服务都启动成功

c9ade08b005db46935f9ff4e8799ec2e418fd599

9、验证Turbine集群监控

访问http://localhost:9091/turbine.stream,返回如下数据

eb24d8951425c160123bd3656646f594df28c859

并且会不断刷新以获取实时的监控数据,说明与单个的监控类似,返回监控项目的信息。进行图形化监控查看,输入http://localhost:9091/hystrix,返回如下界面

28db2e825686d49c1c0b914dcbbb1433ea56a669

输入http://localhost:9091/turbine.stream,然后点击Monitor Stream ,可以看到出现如下界面

d0e5aa05b92a7ef95eabace72e827ffff2bed61f

没有出现任何监控图表。分别访问http://127.0.0.1:5801/feign/user/getUser/1和http://127.0.0.1:5802/feign/user/listUser(尽量多访问几次)

0b8fbda16117c6dbd9f69a5bd786e653de8427d5

返回查看Turbine集群监控图表界面

22c51e528ad6d50028ccafb506da2b49084cbc86

 源码:

https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-client-turbine-hystrix-dashboard-node1
https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-client-turbine-hystrix-dashboard-node2
https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-server-turbine-hystrix-dashboard-node1
https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-server-turbine-hystrix-dashboard-node2
https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-client-turbine-hystrix

本文作者: java乐园

本文来自云栖社区合作伙伴“ java乐园”,了解相关信息可以关注“ java乐园

相关文章
|
监控 负载均衡 Java
深入理解Spring Cloud中的服务网关
深入理解Spring Cloud中的服务网关
|
设计模式 监控 Java
解析Spring Cloud中的断路器模式原理
解析Spring Cloud中的断路器模式原理
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
Spring Cloud Alibaba 发布了 Scheduling 任务调度模块 [#3732]提供了一套开源、轻量级、高可用的定时任务解决方案,帮助您快速开发微服务体系下的分布式定时任务。
16538 118
|
消息中间件 监控 Java
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
423 6
|
Java 关系型数据库 MySQL
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
340 5
|
缓存 监控 Java
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
371 5
|
负载均衡 Java Spring
Spring cloud gateway 如何在路由时进行负载均衡
Spring cloud gateway 如何在路由时进行负载均衡
2677 15
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
631 3
|
消息中间件 Java 开发者
Spring Cloud微服务框架:构建高可用、分布式系统的现代架构
Spring Cloud是一个开源的微服务框架,旨在帮助开发者快速构建在分布式系统环境中运行的服务。它提供了一系列工具,用于在分布式系统中配置、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话、集群状态等领域的支持。
761 5