在Ribbon和Feign使用熔斷器

使用熔斷器防止服務雪崩

 

概述

在微服務架構中,根據業務來拆分成一個個的服務,服務與服務之間可以通過 RPC 相互調用,在 Spring Cloud 中可以用 RestTemplate + RibbonFeign 來調用。爲了保證其高可用,單個服務通常會集羣部署。由於網絡原因或者自身的原因,服務並不能保證 100% 可用,如果單個服務出現問題,調用這個服務就會出現線程阻塞,此時若有大量的請求涌入,Servlet 容器的線程資源會被消耗完畢,導致服務癱瘓。服務與服務之間的依賴性,故障會傳播,會對整個微服務系統造成災難性的嚴重後果,這就是服務故障的 “雪崩” 效應。

爲了解決這個問題,業界提出了熔斷器模型。

Netflix 開源了 Hystrix 組件,實現了熔斷器模式,Spring Cloud 對這一組件進行了整合。在微服務架構中,一個請求需要調用多個服務是非常常見的,如下圖:

較底層的服務如果出現故障,會導致連鎖故障。當對特定的服務的調用的不可用達到一個閥值(Hystrix 是 5 秒 20 次) 熔斷器將會被打開。

熔斷器打開後,爲了避免連鎖故障,通過 fallback 方法可以直接返回一個固定值。

Ribbon 中使用熔斷器

pom.xml 中增加依賴

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

在 Application 中增加 @EnableHystrix 註解

package com.funtl.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;

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

在 Service 中增加 @HystrixCommand 註解

在 Ribbon 調用方法上增加 @HystrixCommand 註解並指定 fallbackMethod 熔斷方法

package com.funtl.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 "Hi,your message is :\"" + message + "\" but request error.";
    }
}

測試熔斷器

此時我們關閉服務提供者,再次請求 http://localhost:8764/hi?message=HelloRibbon 瀏覽器會顯示:

Hi,your message is :"HelloRibbon" but request error.

Feign 中使用熔斷器

Feign 是自帶熔斷器的,但默認是關閉的。需要在配置文件中配置打開它,在配置文件增加以下代碼:

feign:
  hystrix:
    enabled: true

在 Service 中增加 fallback 指定類

package com.funtl.hello.spring.cloud.web.admin.feign.service;

import com.funtl.hello.spring.cloud.web.admin.feign.service.hystrix.AdminServiceHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "hello-spring-cloud-service-admin", fallback = AdminServiceHystrix.class)
public interface AdminService {

    @RequestMapping(value = "hi", method = RequestMethod.GET)
    public String sayHi(@RequestParam(value = "message") String message);
}

創建熔斷器類並實現對應的 Feign 接口

package com.funtl.hello.spring.cloud.web.admin.feign.service.hystrix;

import com.funtl.hello.spring.cloud.web.admin.feign.service.AdminService;
import org.springframework.stereotype.Component;

@Component
public class AdminServiceHystrix implements AdminService {

    @Override
    public String sayHi(String message) {
        return "Hi,your message is :\"" + message + "\" but request error.";
    }
}

測試熔斷器

此時我們關閉服務提供者,再次請求 http://localhost:8765/hi?message=HelloFeign 瀏覽器會顯示:

Hi,your message is :"HelloFeign" but request error.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章