0

我想通過使用Feign客戶端實現回退,但沒有獲得成功。其最簡單的代碼請在下面找到。當使用假客戶端休息呼叫失敗時,不會調用回退方法

主類

@SpringBootApplication 
@EnableDiscoveryClient 
@RestController 
@EnableFeignClients 
public class EurekaClient1Application { 

    @Autowired 
    public DiscoveryClient discoveryClient; 

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

    @Autowired 
    FeingInterface feingInterface; 




    @GetMapping("/hi/{name}") 
    public String test(@PathVariable String name) 
    { 
     String h = feingInterface.test(name); 

     return h; 
    } 
} 

佯接口

@FeignClient(name="client22",fallback=FallBack.class) 
public interface FeingInterface { 

    @GetMapping("/hiname/{name}") 
    public String test(@PathVariable("name") String name); 

} 

後備類

@Component 
class FallBack implements FeingInterface{ 

    @Override 
    public String test(String name) { 
     // TODO Auto-generated method stub 
     return "fall back methord being called"; 
    } 

} 

得到錯誤在其餘客戶端而不是從備用方法

「時間戳」:1501950134118, 「狀態」:500, 「錯誤」: 「內部服務器錯誤」, 「異常」: 「了java.lang.RuntimeException」, 「消息」:「com.netflix.client .ClientException:負載均衡器沒有爲客戶端可用的服務器:client22" ,

爲了讓我通過client22尤里卡ID是不是有在尤里卡服務器的備用方法的消息。我在pom中僞裝假裝。有人可以看看這個。

+0

我發現與此相關的一個問題混帳基本上說「與後備豆@FeignClient註釋的接口不能自動裝配,因爲它不是一個獨特的豆」,這問題被關閉,現在@FeignClient現在是一個主要的豆類( https://github.com/spring-cloud/spring-cloud-netflix/issues/899)。但在這種情況下,我錯過了沒有發現。 – rocky

回答

2

回退實際上不是由Feign本身處理,而是由斷路器處理。所以,你需要把蝟(這是Netflix的斷路器)在classpath中,並使其在application.yml文件是這樣的:

feign: 
    hystrix: 
    enabled: true 

如果您使用「雲:春雲起動在您的build.gradle或pom.xml文件中,「開放式」應該會自動放在您的類路徑中。

+0

請將您的解決方案的解釋(它是如何連接的問題) – user7294900

+0

好吧,我編輯的答案 – user7346048