spring cloud微服務快速教程之(四)熔斷器(Hystrix)及其工具(Dashboard、Turbine)


0-為什么需要熔斷器

  在分布式系統中,各個服務相互調用相互依賴,如果某個服務掛了,很可能導致其他調用它的一連串服務也掛掉或者在不斷等待中耗盡服務器資源,這種現象稱之為雪崩效應

  未來防止系統雪崩,熔斷機制必不可少,就是當一個服務掛掉后,調用它的服務能快速熔斷,不再耗費資源,快速失敗並提供回退方案;

   Hystrix】:是spring cloud的熔斷器組件,提供了熔斷器功能,能夠阻止聯動故障,並提供故障的解決方案,提供系統彈性;

  【Hystrix Dashboard】:是熔斷器的監控面板,通過它,能直觀的了解熔斷器的狀況;

  【Turbine】: 在Dashboard中,我們要輸入一個一個單獨的服務地址進行監控和了解;那么多服務,一個一個輸入那不是累死人,有沒有一個工具能把眾多分散的微服務熔斷器監控狀況聚合到一起,使得我們在一個Dashboard就能了解眾多服務的熔斷器監控狀況呢,有,這個工具就是Turbine;它的作用就是聚合眾多微服務的hystrix監控數據到一起,使得我們只需要在一個Dashboard就能了解所有;

一、使用hystrix

1.1、添加依賴

 

<!-- 斷路器 hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

1.2、配置文件中開啟熔斷器開關

#開啟熔斷器開關
feign:
  hystrix:
    enabled: true

1.3、啟動類中增加 @EnableHystrix 注解和bean

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableFeignClients
public class application
{
    public  static void main(String[] args)
    {
        SpringApplication.run(application.class);
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
 
} }

1.4、增加熔斷器類,實現Feign的接口

package com.anson.service.feign;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @description: 熔斷器類
 * @author: anson
 * @Date: 2020/1/7 11:24
 */
@Component
public class FUserServiceHystrix implements FUserService
{
    @RequestMapping("/user/hello")
    @Override
    public  String hello()
    {
        return "對不起,user服務不可達,請稍后再試!";
    }
}

1.5、增加熔斷器配置類FeignConfig

package com.anson.config;

import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
@Configuration
public class FeignConfig {
    @Bean
    public Retryer feignRetryer() {
        /*
         * 參數說明:
         * 第一個> 重試間隔為100毫秒
         * 第二個> 最大重試時間為1秒
         * 第三個> 最大重試次數為5次
         */
        return new Retryer.Default(100, TimeUnit.SECONDS.toMillis(1), 5);
    }
}

 

1.6、在Feign接口注解中增加fallback,指向熔斷器類

package com.anson.service.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(name = "user",configuration = FeignConfig.class,fallback = FUserServiceHystrix.class)
public interface FUserService
{
@RequestMapping("/user/hello")
public String hello();
}

1.7、運行測試

user服務正常時:

 

 

 user服務關閉時:

 

 

 ---------------------------華麗麗的分割線-------------------------------------------------------

二、Hystrix Dashboard的使用

2.1、熔斷器加了,那么我們要監控和查看hystrix的運行狀態,這時候Dashboard上場;

2.2、添加依賴:

        <!-- dashboard -->
        <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-actuator</artifactId>
        </dependency>

2.3、啟動類添加注解 @EnableHystrixDashboard

2.4、完成,運行測試,打開,http://localhost:8767/hystrix

在里面填入要監控的微服務地址  +/actuator/histrix.stream,如:http://localhost:8768/actuator/histrix.stream,title也填入,點擊按鈕就可以看到結果了:如圖

 

 

 

 可以看到我們加入的熔斷器的運行狀態

----------------------------------------------華麗麗的分割線-----------------------------------------------------------------

三、Turbine 聚合監控數據

3.1、上面Dashboard中,我們輸入http://localhost:8767/actuator/histrix.stream,它只是現實端口8767這個微服務實例的監控數據而已,要看其他的,還得重新輸入,那能不能有個工具將所有微服務的監控數據都聚合到一起,顯示到Dashboard中呢,這個工具就是Turbine 

3.2、新建一個模塊,添加相關依賴:

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

        <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-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

3,.3、修改配置文件,其中app-config是要監控的服務名,多個用逗號分隔;

server:
  port: 8768

spring:
  application:
    name: turbine

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

turbine:
  combine-host-port: true
  app-config: order
  cluster-name-expression: new String("default")
  instanceUrlSuffix: actuator/hystrix.stream
  aggregator:
    cluster-config: default

 

3.4、在啟動類中添加注解@EnableTurbine;

package com.anson;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

/**
 * @description: TODO
 * @author: anson
 * @Date: 2020/1/6 14:27
 */
@SpringBootApplication
@EnableEurekaClient
@EnableTurbine
@EnableHystrixDashboard
public class application
{
    public  static void main(String[] args)
    {
        SpringApplication.run(application.class);
    }

}

完成,啟動運行后,他就會將所有配置文件中添加進來的服務全部的熔斷器監控數據聚合到一起了,

在任意項目的Dashboard面板中填入http://turbine-hostname:port/turbine.stream 的地址樣式,就能看到所有的監控數據了 

,如 : http://localhost:8768/turbine.stream

 

 我們只加了一個熔斷器,你可以多加幾個看看效果;

好了,熔斷器hystrix簡單講解到着,,更深入的后續再詳聊,GIT源碼后續再放出

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM