Spring Cloud Hystrix:未调用 FallbackMethod
Spring Cloud Hystrix : FallbackMethod not invoked
我正在玩弄 Spring Cloud Hystrix,我遇到了一个奇怪的错误,我的 Fallback 方法没有被调用。我的控制器在下面。
@Controller
public class DashboardController {
@LoadBalanced
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "getFareBackup")
@RequestMapping("/dashboard")
public String getFareDashboard(Model m) {
try {
ResponseEntity<List<BusFare>> responseEntity = restTemplate.exchange("http://busfare-service/api/v1/fare/",
HttpMethod.GET, null, new ParameterizedTypeReference<List<BusFare>>() {
});
m.addAttribute("fareList", responseEntity.getBody());
} catch (Exception e) {
e.printStackTrace();
}
return "dashboard";
}
public String getFareBackup(Model m){
System.out.println("Fallback operation called");
m.addAttribute("fareList", new ArrayList<BusFare>().add(new BusFare(1, BigDecimal.valueOf(0.7), "Regular")));
return "dashboard";
}
}
如您所见,我正确设置了 fallbackMethod,但是,当我 运行 服务器并将我的浏览器指向终点时,我得到一个异常,说我的服务器已关闭,据我所知当我的服务关闭时,它应该调用 fallbackMethod,但在我的情况下并非如此,我的 fallbackMethod 基本上没有被调用。
java.lang.IllegalStateException: No instances available for busfare-service
我的代码中遗漏了什么?
看来我喜欢,Hystrix 通过错误处理来处理这个 fallbackMethod。导致我的回退未被调用的代码搞砸的是错误处理。
@HystrixCommand(fallbackMethod = "getFareBackup")
@RequestMapping("/dashboard")
public String getFareDashboard(Model m) {
ResponseEntity<List<BusFare>> responseEntity = restTemplate.exchange("http://busfare-service/api/v1/fare/",
HttpMethod.GET, null, new ParameterizedTypeReference<List<BusFare>>() {
});
m.addAttribute("fareList", responseEntity.getBody());
return "dashboard";
}
使用上面的代码,fallbackMethod 现在可以工作了。
我正在玩弄 Spring Cloud Hystrix,我遇到了一个奇怪的错误,我的 Fallback 方法没有被调用。我的控制器在下面。
@Controller
public class DashboardController {
@LoadBalanced
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "getFareBackup")
@RequestMapping("/dashboard")
public String getFareDashboard(Model m) {
try {
ResponseEntity<List<BusFare>> responseEntity = restTemplate.exchange("http://busfare-service/api/v1/fare/",
HttpMethod.GET, null, new ParameterizedTypeReference<List<BusFare>>() {
});
m.addAttribute("fareList", responseEntity.getBody());
} catch (Exception e) {
e.printStackTrace();
}
return "dashboard";
}
public String getFareBackup(Model m){
System.out.println("Fallback operation called");
m.addAttribute("fareList", new ArrayList<BusFare>().add(new BusFare(1, BigDecimal.valueOf(0.7), "Regular")));
return "dashboard";
}
}
如您所见,我正确设置了 fallbackMethod,但是,当我 运行 服务器并将我的浏览器指向终点时,我得到一个异常,说我的服务器已关闭,据我所知当我的服务关闭时,它应该调用 fallbackMethod,但在我的情况下并非如此,我的 fallbackMethod 基本上没有被调用。
java.lang.IllegalStateException: No instances available for busfare-service
我的代码中遗漏了什么?
看来我喜欢,Hystrix 通过错误处理来处理这个 fallbackMethod。导致我的回退未被调用的代码搞砸的是错误处理。
@HystrixCommand(fallbackMethod = "getFareBackup")
@RequestMapping("/dashboard")
public String getFareDashboard(Model m) {
ResponseEntity<List<BusFare>> responseEntity = restTemplate.exchange("http://busfare-service/api/v1/fare/",
HttpMethod.GET, null, new ParameterizedTypeReference<List<BusFare>>() {
});
m.addAttribute("fareList", responseEntity.getBody());
return "dashboard";
}
使用上面的代码,fallbackMethod 现在可以工作了。