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