我想通过使用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