SpringCloud05之Hystrix熔斷器(基於IDEA)

上一期博客我們介紹瞭如何使用Fegin實現服務的負載均衡,博客鏈接爲https://blog.csdn.net/chenpeixing361/article/details/95616890。這一次我們介紹如何用Hystrix熔斷器防止服務雪崩的情況發生。

首先介紹一下什麼是服務雪崩效應,爲了保證微服務其高可用,單個服務通常會集羣部署。由於網絡原因或者自身的原因,服務並不能保證 100% 可用,如果單個服務出現問題,調用這個服務就會出現線程阻塞,此時若有大量的請求湧入,Servlet 容器的線程資源會被消耗完畢,導致服務癱瘓。服務與服務之間的依賴性,故障會傳播,會對整個微服務系統造成災難性的嚴重後果,這就是服務故障的 “雪崩” 效應。

爲了解決這個問題,業界提出了熔斷器模型。Netflix 開源了 Hystrix 組件,實現了熔斷器模式,Spring Cloud 對這一組件進行了整合。較底層的服務如果出現故障,會導致連鎖故障。當對特定的服務的調用的不可用達到一個閥值(Hystrix 是 5秒20次) 熔斷器將會被打開熔斷器打開後,爲了避免連鎖故障,通過fallback方法可以直接返回一個固定值。

接下來我們以實際的例子來展示熔斷器的作用。我們在之前的項目裏進行修改,首先是Ribbon中添加熔斷器,我們在pom.xml中添加如下代碼:

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

然後我們在Application啓動類中添加@EnableHystrix註解,表明開啓熔斷器應用,代碼如下:

package com.chen.hello.spring.cloud.web.admin.ribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

//開啓熔斷器
@EnableHystrix
@SpringBootApplication
@EnableDiscoveryClient
public class WebAdminRibbonApplication {

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

接着我們在Service中對其中的方法添加@HystrixCommand註解並指定fallbackMethod熔斷方法,當然我們需要編寫熔斷方法hiError,完整代碼如下:

package com.chen.hello.spring.cloud.web.admin.ribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class AdminService {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String sayHi(String message) {
        return restTemplate.getForObject("http://hello-spring-cloud-service-admin/hi?message="+message,String.class);
    }

    //熔斷方法
    public String hiError(String message) {
        return String.format("Hi,your message is: %s but request bad.",message);
    }
}

上述代碼寫好之後,接下來我們便進行測試了,我們開啓8761-8764四個端口,結果和之前Ribbon測試的結果一樣,然後關閉8762和8763兩個服務提供端口,然後輸入網址 http://localhost:8764/hi?message=Ribbon,運行結果如圖所示:

接下來我們把熔斷器加到Fegin中進行測試,其中Feign 是自帶熔斷器的,但默認是關閉的。需要在配置文件中配置打開它,所以我們在application.yml文件中添加如下配置信息:

feign:
  hystrix:
    enabled: true

接着我們創建熔斷器的類AdminServiceHystrix,我們實現Service接口,其代碼如下:

package com.chen.hello.spring.cloud.web.admin.fegin.hystrix;

import com.chen.hello.spring.cloud.web.admin.fegin.service.AdminService;
import org.springframework.stereotype.Component;

//熔斷實現類
@Component
public class AdminServiceHystrix implements AdminService {

    @Override
    public String sayHi(String message) {
        return String.format("Hi,your message is: %s but request bad.",message);
    }
}

然後我們需要在接口中添加fallback屬性並指定接口實現類,完整代碼如下:

package com.chen.hello.spring.cloud.web.admin.fegin.service;

import com.chen.hello.spring.cloud.web.admin.fegin.hystrix.AdminServiceHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

//fallback參數添加熔斷器類
@FeignClient(value = "hello-spring-cloud-service-admin",fallback = AdminServiceHystrix.class)
public interface AdminService {

    @GetMapping(value = "/hi")
    public String sayHi(@RequestParam("message") String message);

}

好了,接下來我們便進行測試了,開啓8765端口,當然8762和8763端口依然是處於關閉的狀態,我們輸入網址http://localhost:8765/hi?message=HelloFeign,運行截圖如下:

好了,這次的熔斷器Hystrix在Ribbon和Feign中的簡單使用就講解到這裏了,我們下期再見!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章