十三:Spring Cloud 之Hystrix Dashboard

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

1. 简介

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

Hystrix Dashboard提供了数字与图形化的调用监控情况。

2. 代码实现

2.1 涉及的模块及整体步骤

2.1.1 涉及的模块

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

2.1.2 整体步骤

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

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

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</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: 8785

spring:
  application:
    name: eureka-hystrix-dashboard

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

2.4.4 bootstrap.yml

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

2.4.5 EurekaHystrixDashboardApplication

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

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 EurekaHystrixDashboardApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaHystrixDashboardApplication.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.";
    }
}

3. 验证

3.1 创建SpringBoot启动类

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

3.1.1 EurekaServerSingletonApplication

3.1.2 EurekaHystrixDashboardApplication

3.2 启动

  1. EurekaServerSingletonApplication
  2. EurekaHystrixDashboardApplication

3.3 查看Hystrix Dashboard

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

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

eureka-hystrix-dashboard

  1. 访问:http://localhost:8785/actuator/hystrix.stream 查看SpringBoot关于Hystrix的监控数据
  2. 访问Hystrix界面前两个输入框依次输入:http://localhost:8785/actuator/hystrix.stream2000在这里插入图片描述

4. 思考

  • 现在的仅限于单机监控,如何在分布式环境下查看监控信息
  • 如何与调用链监控整合,也就是如何搭建一个完整的监控平台
  • 现在这个要输入查询条件,如何定制化首界面和展示项,更接近于实用和生产

5. 补充

5.1 资料

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

https://github.com/Netflix/hystrix

猜你喜欢

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