6.springcloud_hytrix-dashboard监控(Finchley.SR2).md

这是一个从零开始的springcloud的系列教程,如果你从中间开始看,可能会看不明白.请进入我的系列教程开始从头开始学习.spring-cloud教程

Hytrix-Dashboard监控

在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。


沿用上节课创建eureka-client-feign工程,修改eureka-client-feign工程.

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-learn</artifactId>
        <groupId>com.jack</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-client-feign</artifactId>


    <dependencies>
        <!--
        一定要写称spring-cloud-starter-netflix-eureka-client
        如果写错成spring-cloud-netflix-eureka-client,将无法注册
        -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--引入feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

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

        <!--引入hytrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        
        <!--引入hytrix dashboard-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

        <!--
        spring-boot-starter-actuator
        springboot的Actuator提供了运行状态监控的功能,可以通过REST、远程Shell和JMX方式来查看
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

    </dependencies>

</project>

修改EurekaClientFeignApplication

package com.jack.feign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
// 启动feign的注解
@EnableFeignClients
// 启动dashboard
@EnableHystrixDashboard
// 启动hystrix
@EnableHystrix
public class EurekaClientFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientFeignApplication.class, args );
    }
}

application.yml需要加入以下配置,非常关键,因为数据是通过springboot的actuator来持续返回的,我们需要让actuator开放节点给我们,它默认开放health和info来个节点

# springboot的Actuator提供了运行状态监控的功能,可以通过REST、远程Shell和JMX方式来查看。
# 这个配置非常重要,否则会导致/actuator/hystrix.stream无法访问,hystrix.stream节点会持续提供熔断状态
management:
  endpoints:
    web:
#      2.0之前默认是/   2.0默认是 /actuator
#      base-path: /actuator
#      显示健康具体信息  默认不会显示详细信息
#      management.endpoint.health.show-details=always
      exposure:
        # 默认开启health、info,可以通过/actuator/info访问,如果要暴露所有接口,填"*"
        include: hystrix.stream
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

ok,启动服务,访问http://localhost:7773/actuator/hystrix.stream,会发现不断有数据返回,不过因为没有访问api,返回都是空

image.png

当我们访问http://localhost:7773/hello_store api (eureka-client服务没有启动)

此时服务就会出现数据

image.png

我们来打开图形界面控制面板,http://localhost:7773/hystrix

image.png

对着中间输入我们运行状态流服务节点http://localhost:7773/actuator/hystrix.stream

image.png

点击monitor stream.观察流.并且请求一次/hello_store服务.会发现出现一次错误.方法为FeignService#sayStore

image.png

这样我们就可以通过dashboard来观察熔断器的状态了.

但是这个远远是不够的,只能观察一台机器,并没有什么意义.接下来我们会介绍turbine.可以收集所有服务熔断器状态流

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容