springcloud报错-------关于 hystrix 的异常 FallbackDefinitionException:fallback method wasn't found(代码片段
Posted ixfcao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springcloud报错-------关于 hystrix 的异常 FallbackDefinitionException:fallback method wasn't found(代码片段相关的知识,希望对你有一定的参考价值。
典型如下
第一种
import java.util.List;
@RestController
@RequestMapping("/order")
@DefaultProperties(defaultFallback = "fallback4Wait")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(commandProperties = {
@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "10"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")
})
@RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
public Product findById(@PathVariable Long id){
if (id % 2 == 0 ) {
throw new RuntimeException("") ;
}
return restTemplate.getForObject("http://PRODUCT-SERVICE/product/"+ id,Product.class);
}
/**
* 回退方法的返回值必须与调用者的方法要一致,参数也要完全一致
* @param id
* @return
*/
public Product fallback4Wait(){ // 此处应该没有参数
Product product = new Product();
product.setProductName("当前服务访问压力过大,请稍后重试");
return product;
}
}
----------------------------------------
第二种
import java.util.List;
@RestController
@RequestMapping("/order")
@DefaultProperties(defaultFallback = "fallback4Wait")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient; // 服务发现类
@HystrixCommand(fallbackMethod = "fallback4Wait")
@RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
public Product findById(@PathVariable Long id){
if (id % 2 == 0 ) {
throw new RuntimeException("") ;
}
return restTemplate.getForObject("http://PRODUCT-SERVICE/product/"+ id,Product.class);
}
/**
* 回退方法的返回值必须与调用者的方法要一致,参数也要完全一致
* @param id
* @return
*/
public Product fallback4Wait(Long id){ // 此处有参数与上面一致
Product product = new Product();
product.setProductName("当前服务访问压力过大,请稍后重试");
return product;
}
}
以上是关于springcloud报错-------关于 hystrix 的异常 FallbackDefinitionException:fallback method wasn't found(代码片段的主要内容,如果未能解决你的问题,请参考以下文章
MySQL报错ERROR1872(HY000):Slave failed to initialize
mysql 报错ERROR 1820 (HY000):You must resetpassword