天天看點

【springCloud】熔斷器Hystrix的使用 + 可視化監控Hystrix Dashboard和Turbine(5)斷路器(熔斷器)Hystrix一. 概述二、斷路器(熔斷器)的使用三、運作 注冊中心、服務提供者、feign服務消費者斷路器儀表盤Hystrix Dashboard和Turbine

斷路器(熔斷器)Hystrix

一. 概述

1.雪崩效應

在微服務架構中通常會有多個服務層調用,基礎服務的故障可能會導緻級聯故障,進而造成整個系統不可用的情況,這種現象被稱為服務雪崩效應。服務雪崩效應是一種因“服務提供者”的不可用導緻“服務消費者”的不可用,并将不可用逐漸放大的過程。

如果下圖所示:A作為服務提供者,B為A的服務消費者,C和D是B的服務消費者。A不可用引起了B的不可用,并将不可用像滾雪球一樣放大到C和D時,雪崩效應就形成了。

【springCloud】熔斷器Hystrix的使用 + 可視化監控Hystrix Dashboard和Turbine(5)斷路器(熔斷器)Hystrix一. 概述二、斷路器(熔斷器)的使用三、運作 注冊中心、服務提供者、feign服務消費者斷路器儀表盤Hystrix Dashboard和Turbine

2.斷路器(熔斷器)Hystrix

被調用的服務達到一定的呼叫阈(yù)值,斷開服務,調用備用服務,

舉個例子:使用者請求

訂單服務

,訂單支付完成,請求

積分服務

但是

積分服務出現故障,可以調用備用服務,将積分資訊存入

資料庫

【springCloud】熔斷器Hystrix的使用 + 可視化監控Hystrix Dashboard和Turbine(5)斷路器(熔斷器)Hystrix一. 概述二、斷路器(熔斷器)的使用三、運作 注冊中心、服務提供者、feign服務消費者斷路器儀表盤Hystrix Dashboard和Turbine

3.資源隔離

在Hystrix中, 主要通過線程池來實作資源隔離. 通常在使用的時候我們會根據

調用的遠端服務

劃分出

多個線程池

. 例如調用産品服務的Command放入A線程池, 調用賬戶服務的Command放入B線程池. 這樣做的主要優點是運作環境被隔離開了. 這樣就算調用服務的代碼存在bug或者由于其他原因導緻

自己所線上程池被耗盡時, 不會對系統的其他服務造成影響

. 但是帶來的代價就是維護多個線程池會對系統帶來額外的性能開銷. 如果是對性能有嚴格要求而且确信自己調用服務的用戶端代碼不會出問題的話, 可以使用Hystrix的信号模式(Semaphores)來隔離資源

二、斷路器(熔斷器)的使用

1.上一章建立的子子產品:Feign

spring-cloud-starter-openfeign已經自帶了Hystrix,我們隻需要修改一些配置資訊就可以了

feign:
  hystrix:
    enabled: true
           

2.建立備用服務類,ServiceHiHystrix

ServiceHiHystrix.java

package com.example.feign;

import org.springframework.stereotype.Component;

@Component
public class ServiceHiHystrix implements serviceHi{
    @Override
    public String sayHIfromServerHi(String name) {
        return "通路失敗"+name;
    }
}

           

3.接口中添加 建好的FallBACK類

serviceHi

package com.example.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "server-hi",fallback = ServiceHiHystrix.class)#添加fallback
public interface serviceHi {
    @GetMapping("/hi/{name}")
    String sayHIfromServerHi(@PathVariable(value = "name") String name);
}

           

本地controller内容不變

package com.example.feign;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class helloController {
    @Autowired
    private serviceHi serviceHi;

    @GetMapping("hello/{name}")
    public String sayHi(@PathVariable(value = "name") String name){
        return serviceHi.sayHIfromServerHi(name);
    }
}

           

三、運作 注冊中心、服務提供者、feign服務消費者

1.正常通路本地controller,通路正常
【springCloud】熔斷器Hystrix的使用 + 可視化監控Hystrix Dashboard和Turbine(5)斷路器(熔斷器)Hystrix一. 概述二、斷路器(熔斷器)的使用三、運作 注冊中心、服務提供者、feign服務消費者斷路器儀表盤Hystrix Dashboard和Turbine

2.關閉服務提供者,模拟服務不可用當機,之後繼續通路本地方法,傳回備用服務資訊

【springCloud】熔斷器Hystrix的使用 + 可視化監控Hystrix Dashboard和Turbine(5)斷路器(熔斷器)Hystrix一. 概述二、斷路器(熔斷器)的使用三、運作 注冊中心、服務提供者、feign服務消費者斷路器儀表盤Hystrix Dashboard和Turbine

斷路器儀表盤Hystrix Dashboard和Turbine

1.介紹

Hystrix的主要優點之一是它收集關于每個HystrixCommand的一套名額。Hystrix儀表闆以有效的方式顯示每個斷路器的運作狀況。
           

長這個樣子,實測對中文并不友好

【springCloud】熔斷器Hystrix的使用 + 可視化監控Hystrix Dashboard和Turbine(5)斷路器(熔斷器)Hystrix一. 概述二、斷路器(熔斷器)的使用三、運作 注冊中心、服務提供者、feign服務消費者斷路器儀表盤Hystrix Dashboard和Turbine

Turbine

但是隻使用Hystrix Dashboard的話, 你隻能看到單個應用内的服務資訊, 這明顯不夠. 我們需要一個工具能讓我們

彙總系統内多個服務的資料

顯示到Hystrix Dashboard上

, 這個工具就是Turbine.

在複雜的分布式系統中,相同服務的節點經常需要部署上百甚至上千個,很多時候,運維人員希望能夠把相同服務的節點狀态以一個整體叢集的形式展現出來,這樣可以更好的把握整個系統的狀态。

2.使用方法

建立springboot子類 HystrixDashboardTurbine 用來收集服務的熔斷資訊

  • 引入兩個依賴
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <!--spring boot 1.X:spring-cloud-starter-hystrix-->
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <!--spring boot 1.X:spring-cloud-starter-hystrix-dashboard-->
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
           
  • 主類添加兩個注解
package com.example.hystrixdashboardturbine;

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

@EnableTurbine
@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashboardTurbineApplication {

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

}

           
  • yml配置
server:
  # 服務端口号
  port: 8767
spring:
  application:
    # 服務名,即serviceId
    name: hystrix-dashboard-turbine
eureka:
  client:
    serviceUrl:
      # 安全認證的服務注冊中心位址
      defaultZone: http://peer1:8771/eureka
# 熔斷器turbine
turbine:
  aggregator:
    cluster-config: default
  cluster-name-expression: new String("default")
  app-config: service-feign,service-feign2  #配置從那些服務收集熔斷監控      使用 ,隔開多個服務

           

fegin服務 複制一份,改名為 service-feign2,調用不同的注冊服務

【springCloud】熔斷器Hystrix的使用 + 可視化監控Hystrix Dashboard和Turbine(5)斷路器(熔斷器)Hystrix一. 概述二、斷路器(熔斷器)的使用三、運作 注冊中心、服務提供者、feign服務消費者斷路器儀表盤Hystrix Dashboard和Turbine

主類添加兩個注解

【springCloud】熔斷器Hystrix的使用 + 可視化監控Hystrix Dashboard和Turbine(5)斷路器(熔斷器)Hystrix一. 概述二、斷路器(熔斷器)的使用三、運作 注冊中心、服務提供者、feign服務消費者斷路器儀表盤Hystrix Dashboard和Turbine

填入網址

http://localhost:8767/hystrix/
http://localhost:8767/turbine.stream
           
【springCloud】熔斷器Hystrix的使用 + 可視化監控Hystrix Dashboard和Turbine(5)斷路器(熔斷器)Hystrix一. 概述二、斷路器(熔斷器)的使用三、運作 注冊中心、服務提供者、feign服務消費者斷路器儀表盤Hystrix Dashboard和Turbine
【springCloud】熔斷器Hystrix的使用 + 可視化監控Hystrix Dashboard和Turbine(5)斷路器(熔斷器)Hystrix一. 概述二、斷路器(熔斷器)的使用三、運作 注冊中心、服務提供者、feign服務消費者斷路器儀表盤Hystrix Dashboard和Turbine

Github參考位址:https://github.com/jw-star/springCloudPractice/tree/master/parents

springCloud中文文檔:https://www.springcloud.cc/spring-cloud-dalston.html

參考部落格位址:https://blog.csdn.net/qqxx6661/article/details/88559088

繼續閱讀