天天看點

SpringCloud全家桶(第三期:熔斷器Hystrix之Hystrix Dashboard儀表盤)

文章目錄

    • 一:什麼是SpringCloud
    • 二:Hystrix Dashboard是什麼
    • 三:Hystrix Dashboard的作用
    • 四:執行個體

一:什麼是SpringCloud

Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state). Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer’s own laptop, bare metal data centres, and managed platforms such as Cloud Foundry.

翻譯如下:

Spring Cloud為開發人員提供了快速建構分布式系統中的一些常見模式的工具(例如配置管理、服務發現、斷路器、智能路由、微代理、控制總線、一次性令牌、全局鎖、上司層選舉、分布式會話、叢集狀态)。分布式系統的協調導緻了鍋爐闆模式,使用Spring Cloud開發人員可以快速建立實作這些模式的服務和應用程式。它們在任何分布式環境中都能很好地工作,包括開發人員自己的筆記本電腦、裸機資料中心和雲計算等托管平台。

SpringCloud主要架構:

  • 服務發現——Netflix Eureka
  • 服務調用——Netflix Feign
  • 熔斷器——Netflix Hystrix
  • 服務網關——Netflix Zuul
  • 分布式配置——Spring Cloud Config
  • 消息總線 —— Spring Cloud Bus

二:Hystrix Dashboard是什麼

Hystrix

中的一個元件,

Hystrix儀表盤

三:Hystrix Dashboard的作用

主要用來實時監控

Hystrix

的各項名額資訊。通過

Hystrix DashBoard

回報的實時資訊,可以幫助我們快速防線系統中存在的問題,進而及時地采取對應措施。

四:執行個體

4.1,我們先建立一個springboot項目

SpringCloud全家桶(第三期:熔斷器Hystrix之Hystrix Dashboard儀表盤)
SpringCloud全家桶(第三期:熔斷器Hystrix之Hystrix Dashboard儀表盤)

4.2 建立完項目後依賴如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.aa</groupId>
    <artifactId>hystrix-dashboard</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hystrix-dashboard</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        
        <!--hystrix儀表盤-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</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>

</project>
           

4.3 接着在啟動類

HystrixDashboardApplication

中加入注解

@EnableHystrixDashboard

,開啟

hystrix儀表盤

SpringCloud全家桶(第三期:熔斷器Hystrix之Hystrix Dashboard儀表盤)

.4.4 建立

application.yml

,加入配置

server:
  port: 9010


spring:
  application:
    name: hystrix-dashboard

eureka:
  client:
    service-url: 
      defaultZone: http://localhost:8888/eureka/
           

4.5 啟動後通路:

http://localhost:9010/hystrix

SpringCloud全家桶(第三期:熔斷器Hystrix之Hystrix Dashboard儀表盤)

4.6 接下來我們要監控某一個服務了,那麼就要有個服務提供

/hystrix.stream

接口,建立一個服務提供者

provider-service

,加入以下依賴,切記不能漏

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

4.7 在啟動類上加上注解

@EnableCircuitBreaker

,開啟熔斷器

SpringCloud全家桶(第三期:熔斷器Hystrix之Hystrix Dashboard儀表盤)

4.8 建立application.yml,加入配置

server:
  port: 9020

spring:
  application:
    name: provider-service
    
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/
           

4.9 啟動後通路

http://localhost:9020/hystrix.stream

SpringCloud全家桶(第三期:熔斷器Hystrix之Hystrix Dashboard儀表盤)

4.9.1 這時候發現報錯了,404,沒有此接口

解決方案:隻要在自己的項目裡配置上這個servlet就ok了

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

    @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;
    }

}
           

重新啟動,再次通路

http://localhost:9020/hystrix.stream

SpringCloud全家桶(第三期:熔斷器Hystrix之Hystrix Dashboard儀表盤)

通路成功,會一直ping。。。 到了這一步,就可以把路徑

http://localhost:9020/hystrix.stream

放入

Hystrix儀表盤

中進行監控了

SpringCloud全家桶(第三期:熔斷器Hystrix之Hystrix Dashboard儀表盤)

走到這一步,就已經成功了,将會監控這個微服務

上一篇:熔斷器Hystrix的使用

下一篇:服務網關Zuul

繼續閱讀