SpringCloud第十二篇:断路器聚合监控(Hystrix Turbine)(Finchley版本)

       上一篇文章讲述了如何利用Hystrix Dashboard监控断路器Hystrix command。当我们有很多个服务的时候,此时就需要聚合监控Hystrix Dashboard的数据。这就需要用到Spring Cloud的另一个组件了,即Hystrix Turbine

一、Hystrix Turbine简介

       单独的Hystrix Dashboard的数据并没有什么多大的价值,要想看整个系统的Hystrix Dashboard数据就需要用到Hystrix TurbineHystrix Turbine将每个服务Hystrix Dashboard数据进行了整合。Hystrix Turbine的使用非常简单,只需要引入相应的依赖和加上注解和配置就可以了。

二、准备工作

       复用上一篇文章的SpringCloud_Client工程,并且创建一个SpringCloud_Client2工程、内容和结构基本相同,不同的地方如下所示:(这个application.properties属于SpringCloud_Client2),还需要在SpringCloud_Client工程的application.properties文件里面添加management.context-path=/actuator这个标签

server.port=8763
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.application.name=service-lucky
management.endpoints.web.exposure.include=*
management.endpoints.web.cors.allowed-origins=*
management.endpoints.web.cors.allowed-methods=*
management.context-path=/actuator
package com.zit;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;


/*@EnableEurekaClient表明自己是个eureka客户端*/
@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
public class EurekaClientApplicationTwo {

    public static void main(String[] args) {
	    SpringApplication.run(EurekaClientApplicationTwo.class, args);
    }
	
    @Value("${server.port}")
    String port;
	
    @RequestMapping("/lucky")
    @HystrixCommand(fallbackMethod = "luckyError")
    public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "lucky " + name + " ,i am from port:" + port;
    }

    public String luckyError(String name) {
        return "hi,"+name+",sorry,luckyError!";
    }
}

三、创建SpringCloud_Server_Turbine工程

       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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <artifactId>SpringCloud_Server_Turbine</artifactId>
  <packaging>jar</packaging>

  <name>SpringCloud_Server_Turbine</name>
 
  <parent>
	 <groupId>com</groupId>
  	 <artifactId>SpringCloud_Common</artifactId>
     <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 断路器监控引入的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
    </dependencies>

</project>

       application.properties内容如下

server.port=8764
spring.application.name=service-turbine
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
management.endpoints.web.exposure.include=*
management.endpoints.web.cors.allowed-origins=*
management.endpoints.web.cors.allowed-methods=*
turbine.app-config=service-hi,service-lucy
turbine.aggregator.clusterConfi=default
turbine.clusterNameExpression=new String("default")
turbine.combine-host=true
turbine.instanceUrlSuffix=/actuator/hystrix.stream

       SpringCloudServerTurbine内容如下,@EnableTurbine表示开启turbine@EnableTurbine注解包含了@EnableDiscoveryClient注解,即开启了注册服务。

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

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
@EnableTurbine
public class SpringCloudServerTurbine {
	
	
    public static void main( String[] args )
    {
        SpringApplication.run(SpringCloudServerTurbine.class, args);
    }
}

四、Turbine演示

       依次启动SpringCloud_ServerSpringCloud_ClientSpringCloud_Client2SpringCloud_Server_Turbine四个工程

1、打开浏览器输入:http://localhost:8764/turbine.stream,界面如下

2、打开浏览器依次输入http://localhost:8762/hi?name=tomhttp://localhost:8763/lucky?name=tom

3、 打开浏览器输入:http://localhost:8763/hystrix,界面如下

4、在上面的界面中依次输入http://localhost:8764/turbine.stream、2000、aa,然后点击Monitor Stream

       可以看到这个页面聚合了2个servicehystrix dashbord数据。 

 5、打开浏览器输入:http://localhost:8764/turbine.stream,界面如下

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐的小三菊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值