【520,送你一個降級小能手】--【名企大廠必備手冊】Hystrix服務降級詳細說明

前言:

本篇博客主要是通過註解的形式進行服務降級,通過demo展示,鞏固自己的技術掌握,同時爲大家提供參考方案:


@HystrixCommand

設置自身調用超時時間的峯值,峯值內可以正常運行,超過了需要有兜底的方法處理,作服務降級fallback,(我們要知道一旦調用服務方法失敗並拋出了錯誤信息後,會自動調用@HystrixCommand標註好的fallbackMethod調用類中的指定的方法)

服務提供者

修改業務類:
在這裏插入圖片描述

package com.zcw.springcloud.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName : PaymentService
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-05-19 15:48
 */
@Service
public class PaymentService {

    public String paymentInfo_OK(Integer id){
        return "線程池: "+ Thread.currentThread().getName()+"         paymentInfo_OK,id:"+id+"\t"+"O(∩_∩)O哈哈~";
    }

    @HystrixCommand(fallbackMethod ="paymentInfo_TimeOutHandler" ,commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    public String paymentInfo_TimeOut(Integer  id){
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return " 線程池:   "+Thread.currentThread().getName()+" paymentInfo_TimeOut,id:"+id+"\t"+"O(∩_∩)O哈哈~"+ "耗時3秒鐘";
    }
    public String paymentInfo_TimeOutHandler(Integer id ){
        return "線程池:"+Thread.currentThread().getName()+"paymentInfo_TimeOutHandler,id: "+ id+"\t"+"┭┮﹏┭┮";
    }
}


修改啓動類:進行激活

@EnableCircuitBreaker

在這裏插入圖片描述

  • 測試
    在這裏插入圖片描述
    在這裏插入圖片描述
    通過如上截圖,發現兜底方法已經觸發了,進行了超時的設置。
    在這裏插入圖片描述

服務使用者

客戶端降級保護:一般服務降級設置到客戶端

  • 修改YML文件
    在這裏插入圖片描述
feign:
  hystrix:
    enabled: true

  • 修改啓動類:
@EnableHystrix

在這裏插入圖片描述

  • 修改controller類:
    在這裏插入圖片描述
 @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "1500")
    })
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        return paymentHystrixService.paymentInfo_TimeOut(id);
    }
    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
        return "消費者兜底方法┭┮﹏┭┮";
    }

  • 測試
    在這裏插入圖片描述
    在這裏插入圖片描述

總結

我們上面的demo,是通過服務提供者這邊與使用服務者這兩個地方進行分別配置的我們服務降級策略,雖然上面只是一個demo展示,但是代碼裏面耦合度非常的高,在實際的項目開發中,進行不要把業務代碼與我們兜底的方法融合一個類中,如果我們每個一個方法,都有一個兜底的方法,最後整個類,代碼太膨脹了,臃腫不堪。

  • 歸結一下:
    【1】每個業務方法對應一個兜底的方法,代碼膨脹。
    【2】統一和自定義的需要我們後期分開

    解決每個方法配置一個兜底方法的膨脹策略:

    • fegin 接口系列
      配置全局通用的兜底方法:
      在這裏插入圖片描述
      在這裏插入圖片描述
      在這裏插入圖片描述
    • 測試
      在這裏插入圖片描述
      解決代碼耦合度高的問題:使用Feign的Fallback方法
      • 創建一個類:

package com.zcw.springcloud.service;

import org.springframework.stereotype.Component;

/**
 * @ClassName : PaymentFallbackService
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-05-20 17:46
 */
@Component
public class PaymentFallbackService implements PaymentHystrixService {
    @Override
    public String paymentInfo_OK(Integer id) {
        return "PaymentFallbackService ------- fall back ,┭┮﹏┭┮";
    }

    @Override
    public String paymentInfo_TimeOut(Integer id) {
        return "PaymentFallbackService ------- fall back ,┭┮﹏┭┮";
    }
}


  • 配置兜底類
    把我們上面剛創建的類配置到,下面的類中:
    在這裏插入圖片描述
package com.zcw.springcloud.service;

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

/**
 * @ClassName : PaymentHystrixService
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-05-19 17:20
 */
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {
    @GetMapping("/payment/hystrix/ok/{id}")
    String paymentInfo_OK(@PathVariable("id") Integer id);

    @GetMapping("/payment/hystrix/timeout/{id}")
    String paymentInfo_TimeOut(@PathVariable("id")Integer id);
}



  • 測試
    在這裏插入圖片描述
    關閉服務提供者,然後我們再次測試,查看是否有兜底策略:
    在這裏插入圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章