SpringCloud組件:Feign整合Hystrix實(shí)現(xiàn)熔斷機(jī)制

Hystrix簡(jiǎn)述

Netflix開(kāi)源了Hystrix組件,實(shí)現(xiàn)了斷路器模式,SpringCloud對(duì)這一組件進(jìn)行了整合。在微服務(wù)架構(gòu)中,一個(gè)請(qǐng)求需要調(diào)用多個(gè)服務(wù)是非常常見(jiàn)的。

  • 雪崩效應(yīng)
    多個(gè)微服務(wù)之間進(jìn)行復(fù)雜的通信時(shí),如果有一個(gè)服務(wù)出現(xiàn)問(wèn)題,就會(huì)引發(fā)雪崩效應(yīng),導(dǎo)致整個(gè)系統(tǒng)癱瘓。Spring Cloud Hystrix提供了一個(gè)類似于保險(xiǎn)絲的作用,當(dāng)服務(wù)不可用的時(shí)候,Hystrix打開(kāi)斷路器,不再進(jìn)行服務(wù)通信,從而保證自身服務(wù)的可用性。
  • 服務(wù)降級(jí)
    服務(wù)降級(jí)就是在系統(tǒng)高并發(fā)的情況下,可以將一些邊緣服務(wù)進(jìn)行降級(jí)(服務(wù)暫停),將資源優(yōu)先供給核心服務(wù)的處理。

構(gòu)建項(xiàng)目

因?yàn)槿蹟嘀皇亲饔迷谙M(fèi)方(服務(wù)調(diào)用方)這一端,因此我們根據(jù)SpringCloud組件:創(chuàng)建你的第一個(gè)Feign客戶端文章,改動(dòng)tairan-spring-cloud-feign-apitairan-spring-cloud-feign-consumer兩個(gè)項(xiàng)目少量代碼即可。

因?yàn)?code>Feign中已經(jīng)依賴了Hystrix,所以在Maven配置上不用做任何改動(dòng)。

關(guān)于單獨(dú)引入Hystrix依賴,網(wǎng)上有的文章說(shuō)引入的依賴是spring-cloud-starter-hystrix,其實(shí)官方不在推薦使用,推薦使用spring-cloud-starter-netflix-hystrix。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

tairan-spring-cloud-feign-api修改

1. 創(chuàng)建HelloServiceHystrix

創(chuàng)建HelloServiceHystrix類,并實(shí)現(xiàn)HelloService接口,代碼如下:

package com.tairan.chapter.feign.api.hystrix;

import com.tairan.chapter.feign.api.HelloService;
import org.springframework.stereotype.Component;

/**
 * 當(dāng)HelloService中的Feign調(diào)用失敗或超時(shí)時(shí),會(huì)調(diào)用該實(shí)現(xiàn)類的方法
 * 需要注意的是fallback指定的類一定要添加@Component將其加入到Spring容器
 */
@Component
public class HelloServiceHystrix implements HelloService {
    @Override
    public String getMessage() throws Exception {
        return "Get Message Failed!";
    }
}

注意:fallback指定的類一定要添加@Component將其加入到Spring容器

2. 修改HelloService接口

修改HelloService接口,在FeignClient注解中添加fallback,指定Hystrix熔斷異?;卣{(diào)類HelloServiceHystrix,代碼如下:

package com.tairan.chapter.feign.api;

import com.tairan.chapter.feign.api.hystrix.HelloServiceHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "tairan-spring-cloud-feign-provider", fallback = HelloServiceHystrix.class)
public interface HelloService {

    @RequestMapping("/hello")
    String getMessage() throws Exception;

}

tairan-spring-cloud-feign-consumer修改

  1. 開(kāi)啟Feign對(duì)Hystrix的支持,在application.yml文件中添加如下配置:
feign:
  # Dalston SR1(待定)之后的版本默認(rèn)關(guān)閉hystrix對(duì)feign的支持,如果想要使用fallback功能這里必須啟用
  hystrix:
    enabled: true
  1. 入口類修改SpringBootApplication掃描路徑,可以掃描到Hystrix熔斷類,即tairan-spring-cloud-feign-api中的HelloServiceHystrix類,代碼如下所示:
@SpringBootApplication(scanBasePackages = "com.tairan.chapter.feign")
@EnableDiscoveryClient
// 通過(guò)@EnableFeignClients注解開(kāi)啟Spring Cloud Feign的支待功能。
@EnableFeignClients("com.tairan.chapter.feign.api")
public class TairanSpringCloudFeignConsumerApplication {

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

}

運(yùn)行測(cè)試

  1. 啟動(dòng)服務(wù)注冊(cè)中心tairan-spring-cloud-eureka
  2. 啟動(dòng)服務(wù)提供方tairan-spring-cloud-feign-provider
  3. 啟動(dòng)服務(wù)消費(fèi)方tairan-spring-cloud-feign-consumer
  4. 訪問(wèn)tairan-spring-cloud-feign-consumer工程的/feign-hello接口,鏈接:http://localhost:8080/feign-hello,查看訪問(wèn)結(jié)果
  5. 關(guān)閉服務(wù)提供方tairan-spring-cloud-feign-provider
  6. 訪問(wèn)tairan-spring-cloud-feign-consumer工程的/feign-hello接口,鏈接:http://localhost:8080/feign-hello,查看訪問(wèn)結(jié)果

執(zhí)行4操作后,訪問(wèn)鏈接http://localhost:8080/feign-hello后,接口返回結(jié)果如下所示:


執(zhí)行5操作后,訪問(wèn)鏈接http://localhost:8080/feign-hello后,接口返回結(jié)果如下所示:


根據(jù)返回結(jié)果說(shuō)明熔斷成功。

源碼位置

本章源碼已經(jīng)上傳到淡若悠然,請(qǐng)結(jié)合源碼進(jìn)行學(xué)習(xí),感謝閱讀。

碼云地址(本章源碼):https://gitee.com/litairan/tairan-spring-cloud

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容