spring cloud netflix (09) Ribbon+Hystrix模式下使用Hystrix仪表盘

前言

完整知识点:spring cloud netflix 系列技术栈

Hystrix仪表盘

Hystrix在spring cloud netflix体系中是熔断器,我对他的简单理解就是当访问的方法由于各种原因的影响,不能正常的对外提供服务,此时熔断器就是就会马上停止这个请求对该方法的访问,并作出相关的提示,告知用户该方法无法提供正常的服务,防止由于一个方法的阻塞,造成整个系统性能的下降。而熔断器仪表盘就是对这个过程的一个可视化显示。

搭建Ribbon+Hystrix下的hystrix仪表盘

  • 完整项目的目录结构
    在这里插入图片描述

  • hystrix仪表盘的pom文件

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
  • 完整的pom文件
<?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">
    <modelVersion>4.0.0</modelVersion>
    <!-- 关联统一的依赖管理 -->
    <parent>
        <groupId>com.baiyang</groupId>
        <artifactId>spring-cloud-netflix-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <!-- 指向统一的依赖管理的位置 -->
        <!-- 完整项目(知识点)请查看https://blog.csdn.net/weixin_41756573/article/details/103439742-->
        <relativePath>../spring-cloud-netflix-dependencies/pom.xml</relativePath>
    </parent>

    <groupId>com.baiyang</groupId>
    <artifactId>spring-cloud-netflix-web-admin-ribbon</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!-- Spring Boot Begin -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->

        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</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>
        <!-- Spring Cloud End -->

        <!-- 解决 thymeleaf 模板引擎一定要执行严格的 html5 格式校验问题 -->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 配置程序的入口类 -->
                    <mainClass>com.baiyang.spring.cloud.netflix.web.admin.ribbon.AdminWebRibbonApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • application入口类
package com.baiyang.spring.cloud.netflix.web.admin.ribbon;

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;

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class AdminWebRibbonApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminWebRibbonApplication.class,args);
    }
}
  • HystrixDashboardConfiguration配置类
package com.baiyang.spring.cloud.netflix.web.admin.ribbon.config;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HystrixDashboardConfiguration {

    @Bean
    public ServletRegistrationBean getServletRegistrationBean(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}
  • application.yml
spring:
  application:
    name: spring-cloud-netflix-web-admin-ribbon

server:
  port: 8764

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

thymeleaf:
    cache: false
    mode: LEGACYHTML5
    encoding: UTF-8
    servlet:
      content-type: text/html

访问结果

http://localhost:8764/hystrix
在这里插入图片描述

Hystrix仪表盘如何使用

在这里插入图片描述
配置完成后,点击Monitor Stream按钮,进入如下界面
在这里插入图片描述
新开页面访问8764服务器上的一个方法
http://localhost:8764/hello?message=kino
在这里插入图片描述
切换到hystrix仪表盘界面
从界面中的信息(比如颜色对应)我们可以看出,我们进行了一次访问,并失败,错误率为100%。
在这里插入图片描述
我们使服务正常运行
在这里插入图片描述
再来观察hystrix仪表盘
在这里插入图片描述

Hystrix仪表盘总结

在这里插入图片描述

其他信息会慢慢在相关博客中更新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值