SpringCloud第五篇-Hystrix Dashboard

Hystrix Dashboard简介

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

在我的第四篇文章断路器讲述了如何使用断路器,并简单的介绍了下Hystrix Dashboard组件,这篇文章更加详细的介绍Hystrix Dashboard。

项目准备

  • servic-hystrix-dashboard:新建项目
  • 拷贝service-hello项目的pom,yml,java文件到该项目中

断路器监控的配置

java类的注解配置

主程序的类名Application修改为HystrixDashboardApplication

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">


    <groupId>comm.bamboo</groupId>
    <version>1.0-SNAPSHOT</version>
    <artifactId>servic-hystrix-dashboard</artifactId>
    <packaging>jar</packaging>

    <name>servic-hystrix-dashboard</name>




    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</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-test</artifactId>
            <scope>test</scope>
        </dependency>


    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>




</project>

以下三个依赖必加,缺一不可(重点)

<!--新增的保护模式处理配置部分:暴露各种指标-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- hystrix和 HystrixDashboard 依赖包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
        </dependency>
yml
server:
  port: 8762
spring:
  application:
    name: service-hystrix-dashboard

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

在程序的入口HystrixDashboardApplication类,

  • 加上@EnableHystrix注解开启断路器,这个是必须的
  • 加上@EnableHystrixDashboard注解,开启HystrixDashboard
  • 并且需要在程序中的接口名上加上@HystrixCommand注解声明断路点和异常回调方法

package com.bamboo;

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.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;

/**
 *
 * 服务端service-hystrix-dashboard
 * 增加断路器和断路器的监控
 *
 * service-hystrix-dashboard Application
 */
@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableHystrix//添加断路器的注解
@EnableHystrixDashboard//添加断路器监控注解
public class HystrixDashboardApplication {


    public static void main(String... args) throws Exception {
        SpringApplication.run(HystrixDashboardApplication.class, args);
    }

    @Value("${server.port}")
    String port;

    @RequestMapping("/hello")
    @HystrixCommand(fallbackMethod = "helloError")//明断路点和回调方法名
    public String home() {
        return "hello from port:" +port;
    }


    //异常处理的回调方法
    public String helloError() {
        return "hello Error,sorry,error!";
    }

}

运行并查看结果

  • 惯例先运行eureka-server
  • 在运行当前的service-hystrix-dashboard

可以在http://localhost:8761/看到服务都已经其起来了
- 先运行一次http://localhost:8762/hello接口服务,运行成功返回字符串(很多教程没有这一步,后面的数据只能看得到ping字符串啥都没,至少我这个版本是如此)
- 在访问http://localhost:8762/hystrix.stream,可以看到返回的数据,如下图1
- 在控制台页面查看曲线图面板,如图2,填好后点击monitor stream按钮可以看到图三

图1
这里写图片描述

图2
这里写图片描述

图3
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值