學習springcloud的Hystrix(熔斷器)。記錄其中碰見的問題(參考純潔的微笑)

服務中心,註冊服務,調用服務完成之後。多個服務關聯性調用,有時候,出現服務鏈式調用,若是上層奔潰,大批量的請求整個下層所有奔潰。對於這種狀況,springclud給咱們提供了,熔斷器-Hystrixweb

1.application.properties配置文件增長開啓熔斷配置spring

spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

#######熔斷器開啓###########
feign.hystrix.enabled=true

2.新增接口的實現類,做用,服務失敗,熔斷器調用app

package com.example.servicefeign.impleServer;

import com.example.servicefeign.interfaceServer.HelloRemote;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

@Component//將實現經過bean注入
public class HelloRemoteHystrix implements HelloRemote {

    @Override
    public String hello(@RequestParam(value = "name") String name){
        return "熔斷器返回結果:" + name;
    }
}

@Component 經過註解,將其做爲bean對象ide

3.FeignClient註解中,增長失敗返回類的引用測試

package com.example.servicefeign.interfaceServer;

import com.example.servicefeign.impleServer.HelloRemoteHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name= "spring-cloud-producer",fallback = HelloRemoteHystrix.class)
//name:遠程服務名,及spring.application.name配置的名稱
//服務熔斷的時候返回fallback類中的內容
public interface HelloRemote {

    @RequestMapping(value = "/hello")
    String hello(@RequestParam(value = "name") String name);

}

FeignClient裏面,增長fallback,失敗回調類。.net

測試,將註冊的服務程序,直接中止,進行訪問,返回成功server

相關文章
相關標籤/搜索