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

0-爲何須要熔斷器

  在分佈式系統中,各個服務相互調用相互依賴,若是某個服務掛了,極可能致使其餘調用它的一連串服務也掛掉或者在不斷等待中耗盡服務器資源,這種現象稱之爲雪崩效應java

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

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

  【Hystrix Dashboard】:是熔斷器的監控面板,經過它,能直觀的瞭解熔斷器的情況;express

  【Turbine】: 在Dashboard中,咱們要輸入一個一個單獨的服務地址進行監控和了解;那麼多服務,一個一個輸入那不是累死人,有沒有一個工具能把衆多分散的微服務熔斷器監控情況聚合到一塊兒,使得咱們在一個Dashboard就能瞭解衆多服務的熔斷器監控情況呢,有,這個工具就是Turbine;它的做用就是聚合衆多微服務的hystrix監控數據到一塊兒,使得咱們只須要在一個Dashboard就能瞭解全部;服務器

1、使用hystrix

1.一、添加依賴app

 

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

1.二、配置文件中開啓熔斷器開關分佈式

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

1.三、啓動類中增長 @EnableHystrix 註解和beanide

@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.四、增長熔斷器類,實現Feign的接口spring-boot

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.五、增長熔斷器配置類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.六、在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.七、運行測試

user服務正常時:

 

 

 user服務關閉時:

 

 

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

2、Hystrix Dashboard的使用

2.一、熔斷器加了,那麼咱們要監控和查看hystrix的運行狀態,這時候Dashboard上場;

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.三、啓動類添加註解 @EnableHystrixDashboard

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

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

 

 

 

 能夠看到咱們加入的熔斷器的運行狀態

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

3、Turbine 聚合監控數據

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

3.二、新建一個模塊,添加相關依賴:

 <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,.三、修改配置文件,其中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.四、在啓動類中添加註解@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源碼後續再放出

相關文章
相關標籤/搜索