关于 hystrix 的异常 fallback method wasn't found

在 Spring Cloud 中使用断路器 hystrix 后,可能会遇到异常:com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: fallback method wasn't found

典型如下:

@HystrixCommand(fallbackMethod = "fallbackHi")
public String getHi(String x) {
    String msg = restTemplate.getForObject("http://jack/hi", String.class);
    return msg;
}

public String fallbackHi(){
    return "can't say hi";
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这样就会出现如上所述的异常,这是因为指定的 备用方法 和 原方法 的参数个数,类型不同造成的;

所以需要统一参数的个数,类型:

@HystrixCommand(fallbackMethod = "fallbackHi")
public String getHi(String x) {
    String msg = restTemplate.getForObject("http://jack/hi", String.class);
    return msg;
}

public String fallbackHi(String x){
    return "can't say hi, and get: " + x;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这样就可以解决上述的异常了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误是因为无法找到名为 "feign/hystrix/FallbackFactory.class" 的类路径资源,所以会报错。 这可能是因为您的项目缺少了相应的依赖或配置文件。根据引用的描述,您提到的 spring.xml 配置文件可能位于 resource 目录下,但您的项目中可能没有正确加载该资源。 根据引用的描述,您可能需要导入 Feign 相关的依赖包。确保您的项目中已经添加了以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 请仔细检查您的项目配置和依赖是否正确,并确保所需的类路径资源存在于正确的位置。如果问题仍然存在,请检查文件路径或联系相关开发人员进行进一步的调试和解决。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [解决报错class path resource [spring.xml] cannot be opened because it does not exist......](https://blog.csdn.net/qq_42490860/article/details/129534982)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [SpringCloud启动Provider微服务时出现java.io.FileNotFoundException异常](https://blog.csdn.net/qq_46010154/article/details/111250624)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值