十四:Spring Cloud 之Hystrix Dashboard结合Turbine

版权声明:本文为博主原创文章,觉得稍有帮助,可点赞、转载注明出处。 https://blog.csdn.net/chenghuaying/article/details/82820396

1. 简介

Turbine (provided by the Spring Cloud Netflix project), aggregates multiple instances Hystrix metrics streams, so the dashboard can display an aggregate view. Turbine uses the DiscoveryClient interface to lookup relevant instances.

Hystrix是Netflix解决自己业务不稳定性的一个限流容错框架,可以帮助我们解决微服务架构体系中的限流、降级、熔断等功能。提高系统稳定性,提供了完善的监控实现,并且Hystrix可以根据监控数据动态调整内部处理机制。

Hystrix Dashboard提供了数字与图形化的调用监控情况。结合Turbine可实现多Hystrix实例监控整合。

2. 代码实现

2.1 涉及的模块及整体步骤

2.1.1 涉及的模块

  • eureka-server-singleton:微服务的服务注册中心,限流熔断针对的是服务调用异常的处理。
  • eureka-hystrix-dashboard
  • eureka-hystrix-dashboard-another
  • eureka-hystrix-dashboard-turbine:图形化展示数据监控情况

2.1.2 整体步骤

  1. 实现eureka-server-singleton
  2. 实现eureka-hystrix-dashboard
  3. 实现eureka-hystrix-dashboard-another
  4. 实现eureka-hystrix-dashboard-turbine
  5. 调用eureka-hystrix-dashboard与eureka-hystrix-dashboard-another提供的服务接口
  6. 打开eureka-hystrix-dashboard-turbine的监控界面,输入相关信息,查看调用情况

2.2 源代码

2.2.1 Github地址

https://github.com/andyChenHuaYing/spring-cloud-demo

2.3 eureka-server-singleton

二:Spring Cloud 之Eureka服务发布与注册没有任何区别。

2.4 eureka-hystrix-dashboard

十三:Spring Cloud 之Hystrix Dashboard#2.4没有任何区别

2.4 eureka-hystrix-dashboard-another

2.4.1 整体实现

  1. pom.xml:引入相关依赖,具体查看下方具体代码
  2. application.yml:指定端口,应用名,注册中心访问地址信息
  3. bootstrap.yml:指定公开SpringBoot的actuator监控端点为所有
  4. EurekaHystrixDashboardApplication:Spring boot启动类,提供一个简单的REST服务接口

2.4.2 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>spring-cloud-finchley-demo</artifactId>
        <groupId>org.oscar.scd</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-hystrix-dashboard-another</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix-dashboard -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>
    </dependencies>
</project>

2.4.3 application.yml

server:
  port: 8786

spring:
  application:
    name: eureka-hystrix-dashboard-another

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

2.4.4 bootstrap.yml

management:
  endpoints:
    web:
      exposure:
        include: '*'

2.4.5 EurekaHystrixDashboardAnotherApplication

package org.oscar.scd.eureka.hystrix.dashboard.another;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
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.RestController;

@EnableHystrix
@RestController
@EnableEurekaClient
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
@EnableHystrixDashboard
public class EurekaHystrixDashboardAnotherApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaHystrixDashboardAnotherApplication.class, args);
    }

    @Value("${spring.application.name}")
    private String appName;

    @RequestMapping("/appName")
    @HystrixCommand(fallbackMethod = "errorMsg")
    public String getAppName() {
        return this.appName;
    }

    @SuppressWarnings("unused")
    public String errorMsg() {
        return "Error msg by hystrix.";
    }
}

2.4 eureka-hystrix-dashboard-turbine

2.4.1 整体实现

  1. pom.xml:引入相关依赖,具体查看下方具体代码
  2. application.yml:指定端口,应用名,注册中心访问地址信息
  3. EurekaHystrixDashboardTurbineApplication:Spring boot启动类,使用注解开启Hystrix Turbine监控

2.4.2 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>spring-cloud-finchley-demo</artifactId>
        <groupId>org.oscar.scd</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-hystrix-dashboard-turbine</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix-dashboard -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
    </dependencies>
</project>

2.4.3 application.yml

server:
  port: 8787
spring:
  application:
    name: eureka-hystrix-dashboard-turbine
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

turbine:
  app-config: eureka-hystrix-dashboard-another,eureka-hystrix-dashboard
  aggregator:
    clusterConfig: default
  clusterNameExpression: new String("default")
  combine-host: true
  instanceUrlSuffix:
    default: actuator/hystrix.stream


2.4.4 EurekaHystrixDashboardApplication

package org.oscar.scd.eureka.hystrix.dashboard.turbine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
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;
import org.springframework.web.bind.annotation.RestController;

@EnableHystrix
@EnableTurbine
@RestController
@EnableEurekaClient
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
@EnableHystrixDashboard
public class EurekaHystrixDashboardTurbineApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaHystrixDashboardTurbineApplication.class, args);
    }

}

3. 验证

3.1 创建SpringBoot启动类

最简单的方式添加一个SpringBoot启动类型的启动类就行,如果对SpringBoot有所了解或者看过前面系列的文章,这里很简单。

3.1.1 EurekaServerSingletonApplication

3.1.2 EurekaHystrixDashboardApplication

3.1.2 EurekaHystrixDashboardAnotherApplication

3.1.2 EurekaHystrixDashboardTurbineApplication

3.2 启动

  1. EurekaServerSingletonApplication
  2. EurekaHystrixDashboardApplication
  3. EurekaHystrixDashboardAnotherApplication
  4. EurekaHystrixDashboardTurbineApplication

3.3 查看Hystrix Dashboard

  1. 访问:http://localhost:8785/hystrix
    在这里插入图片描述

  2. 访问:http://localhost:8785/appName调用服务

eureka-hystrix-dashboard

扫描二维码关注公众号,回复: 3357939 查看本文章
  1. 访问:http://localhost:8786/appName调用服务

eureka-hystrix-dashboard-another

  1. 访问:http://localhost:8787/turbine.stream 查看Turbine聚合的监控信息
  2. 访问http://localhost:8787/hystrixHystrix界面前两个输入框依次输入:http://localhost:8787/turbine.stream2000。注意是8787端口的turbine.stream,如果没有数据,多调用几次服务端口
    在这里插入图片描述

4. 思考

  • 如何与调用链监控整合,也就是如何搭建一个完整的监控平台
  • 现在这个要输入查询条件,如何定制化首界面和展示项,更接近于实用和生产
  • 监控项或者监控信息是否可定制化,Spring Boot的actuator如何使用的,具体有哪些endpoint。

5. 补充

5.1 资料

九:对微服务限流容错的理解

十一:对微服务调用链监控的理解

十:对微服务监控系统分层和监控架构的理解

https://github.com/Netflix/hystrix

http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi_spring-cloud-consul-turbine.html

猜你喜欢

转载自blog.csdn.net/chenghuaying/article/details/82820396
今日推荐