SpringCloud实战(八)-断路器监控与集群监控(Hystrix Dashboard)

本文是SpringCloud实战(八)-断路器监控(Hystrix Dashboard),若要关注前文,请点击传送门:

SpringCloud实战(七)-服务链路追踪(Spring Cloud Sleuth)

前文我们介绍了zipkin。本文主要讲述断路器监控Hystrix Dashboard。

一、Hystrix Dashboard简介

Hystrix Dashboard,它主要用来实时监控Hystrix的各项指标信息。通过Hystrix Dashboard反馈的实时信息,可以帮助我们快速发现系统中存在的问题。

二、准备工作

基于前文的service-hi工程进行改造。

三、集成Hystrix-Dashboard

3.1 maven依赖

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

在现有工程中追加dashboard依赖。

3.2 启动类(EurekaClientApplication)

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableCircuitBreaker
@EnableHystrixDashboard
@RestController
public class EurekaClientApplication {

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

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

    @RequestMapping("/hi")
    public String hi(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }

    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

在现有工程中追加@EnableHystrixDashboard注解,开启Dashboard组件,然后启动,访问 http://localhost:8762/hystrix,图示如下:

单节点Hystrix监控访问 http://hystrix-app:port/hystrix.stream

集群监控访问 http://turbine-hostname:port/turbine.stream

我们这里只是搭建了单节点,所以我们访问 http://hystrix-app:port/hystrix.stream,如图所示:

此时停止service-lucy实例,然后我们访问 http://localhost:8762/hiLucy?name=zzx,如图所示:

此时我们成功监测到service-lucy服务异常。

四、Turbine集群监控

单节点Dashboard意义不大,因为我们的模块可能会有很多很多,此时我们需要Dashboard搭建集群监控。

重新新建spirngboot工程,取名为:service-turbine。

4.1 maven依赖

<?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.oal.microservice</groupId>
        <artifactId>openAiLab</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.oal.microservice</groupId>
    <artifactId>service-turbine</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>service-turbine</name>
    <description>turbine</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
    </dependencies>

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

 4.2 启动类

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
@EnableTurbine
public class ServiceTurbineApplication {

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

4.3 配置文件(application.yml)

eureka:
  client:
    serviceUrl:
      defaultZone: http://eureka-serve-01:8761/eureka/

server:
  port: 8766

spring:
  application:
    name: service-turbine

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

turbine:
  app-config: service-feign,service-hi
  aggregator:
    clusterConfig: default
  clusterNameExpression: new String("default")
  combine-host: true
  instanceUrlSuffix:
    default: hystrix.stream

service-turbine端口配置为8766,我们通过turbine.app-config指定需要整合的Dashboard所在服务名,整合多个Dashboard服务用逗号分隔。

4.4 改造service-fegin工程

service-feign改造同service-hi,引入Dashboard依赖并增加@EnableHystrixDashboard注解就可以了。

4.5 演示Dashboard整合

启动上述三个服务(service-feign、service-hi、service-turbine),然后访问 http://localhost:8766/hystrix(service-turbine),如图所示:

我们通过访问集群的方式连接它(http://turbine-hostname:port/turbine.stream ),访问 http://localhost:8766/turbine.stream,如图所示:

 

此时我们成功整合service-feign和service-hi的两块Dashboard。

断路器监控与集群监控搭建完成。

发布了352 篇原创文章 · 获赞 390 · 访问量 37万+

猜你喜欢

转载自blog.csdn.net/qq_19734597/article/details/90046077
今日推荐