天天看點

springcloud hystrix(監控、熔斷、降級)spring cloud hystrixHystrix如何解決依賴隔離

spring cloud hystrix

引入依賴

-----------------------------------------------------------------

<!--hystrix-->

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-hystrix</artifactId>

    </dependency>

    <!--hystrix-dashboard 監控-->

        <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>

---------------------------------------------

<code>spring-cloud-starter-hystrix</code> 核心jar

<code>spring-cloud-starter-hystrix-dashboard</code> 監控jar

使用<code>EnableCircuitBreaker</code>或者 <code>EnableHystrix</code> 表明<code>Spring boot</code>工程啟用hystrix,兩個注解是等價的.

--------------------------------------------------------------------

package com.lkl.springcloud.hystrix;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;

import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication

@EnableCircuitBreaker

@EnableHystrixDashboard

public class Application {

    public static void main(String[] args) {

        new SpringApplicationBuilder(Application.class).web(true).run(args);

    }

}

----------------------------------------------------------------------

其中<code>EnableHystrixDashboard</code>注解表示啟動對hystrix的監控,後面會用到

随後模拟一個調用三方依賴服務

<code>controller</code>-&gt; <code>service</code> -&gt; <code>dependency service</code>

---------------------------------------------------------------------

package com.lkl.springcloud.hystrix.controller;

import com.lkl.springcloud.hystrix.service.HystrixService;

import org.springframework.beans.factory.annotation.Autowired;

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

import org.springframework.web.bind.annotation.RestController;

/**

 * 模拟一個對外的接口

 */

@RestController

public class HystrixController {

    @Autowired

    private HystrixService service;

    /**

     * 調用依賴的服務

     */

    @RequestMapping("/call")

    public String callDependencyService(){

        return service.callDependencyService();

-------------------------------------------------------------------

package com.lkl.springcloud.hystrix.service;

import org.springframework.stereotype.Service;

 * 依賴服務

@Service

public class HystrixService {

    private CallDependencyService dependencyService;

    public String callDependencyService() {

        return dependencyService.mockGetUserInfo();

-----------------------------------------------------------------------

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import org.springframework.stereotype.Component;

import java.util.Random;

 * 調用依賴服務,通過hystrix包裝調用服務

@Component

public class CallDependencyService {

    private Random random = new Random();

     * 模拟擷取使用者資訊(通過網絡調用)

     * @return

    @HystrixCommand(fallbackMethod = "fallback")

    public String mockGetUserInfo(){

        int randomInt= random.nextInt(10) ;

        if(randomInt&lt;8){  //模拟調用失敗情況

            throw new RuntimeException("call dependency service fail.");

        }else{

            return "UserName:liaokailin;number:"+randomInt;

        }

    public String fallback(){

        return "some exception occur call fallback method.";

------------------------------------------------------------------------------

<code>HystrixCommand</code> 表明該方法為hystrix包裹,可以對依賴服務進行隔離、降級、快速失敗、快速重試等等hystrix相關功能 

該注解屬性較多,下面講解其中幾個

fallbackMethod 降級方法

commandProperties 普通配置屬性,可以配置HystrixCommand對應屬性,例如采用線程池還是信号量隔離、熔斷器熔斷規則等等

ignoreExceptions 忽略的異常,預設<code>HystrixBadRequestException</code>不計入失敗

groupKey() 組名稱,預設使用類名稱

commandKey 指令名稱,預設使用方法名

<a href="https://s2.51cto.com/wyfs02/M01/06/E9/wKiom1nAht2RIg_wAAiopUwoKco221.png-wh_500x0-wm_3-wmp_4-s_1035124941.png" target="_blank"></a>

<a href="https://s5.51cto.com/wyfs02/M01/A5/99/wKioL1nAhr6zFMtMAAYKpJGISTQ964.png-wh_500x0-wm_3-wmp_4-s_1800811931.png" target="_blank"></a>

ok ~ it’s work ! more about is here

1:Hystrix使用指令模式HystrixCommand(Command)包裝依賴調用邏輯,每個指令在單獨線程中/信号授權下執行。

2:可配置依賴調用逾時時間,逾時時間一般設為比99.5%平均時間略高即可.當調用逾時時,直接傳回或執行fallback邏輯。

3:為每個依賴提供一個小的線程池(或信号),如果線程池已滿調用将被立即拒絕,預設不采用排隊.加速失敗判定時間。

4:依賴調用結果分:成功,失敗(抛出異常),逾時,線程拒絕,短路。 請求失敗(異常,拒絕,逾時,短路)時執行fallback(降級)邏輯。

5:提供熔斷器元件,可以自動運作或手動調用,停止目前依賴一段時間(10秒),熔斷器預設錯誤率門檻值為50%,超過将自動運作。

6:提供近實時依賴的統計和監控

 本文轉自 獨孤環宇 51CTO部落格,原文連結:http://blog.51cto.com/snowtiger/1966590