码迷,mamicode.com
首页 > 编程语言 > 详细

springcloud报错-------关于 hystrix 的异常 FallbackDefinitionException:fallback method wasn't found

时间:2020-05-23 11:34:33      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:nbsp   anon   efi   ons   client   调用   完全   hid   ret   

典型如下

第一种

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;
}
}

 

  1. @HystrixCommand(fallbackMethod = "fallbackHi")
  2.  
    public String getHi(String x) {
  3.  
    String msg = restTemplate.getForObject("http://jack/hi", String.class);
  4.  
    return msg;
  5.  
    }
  6.  
     
  7.  
    public String fallbackHi(){
  8.  
    return "can‘t say hi";
  9.  

springcloud报错-------关于 hystrix 的异常 FallbackDefinitionException:fallback method wasn't found

标签:nbsp   anon   efi   ons   client   调用   完全   hid   ret   

原文地址:https://www.cnblogs.com/caoxinfang/p/12941513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!