springcloud报错-------关于 hystrix 的异常 FallbackDefinitionException:fallback method wasn't found
2021-01-14 12:15
                         标签:nbsp   anon   efi   ons   client   调用   完全   hid   ret    典型如下   springcloud报错-------关于 hystrix 的异常 FallbackDefinitionException:fallback method wasn't found 标签:nbsp   anon   efi   ons   client   调用   完全   hid   ret    原文地址:https://www.cnblogs.com/caoxinfang/p/12941513.html第一种
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
文章链接:http://soscw.com/index.php/essay/41779.html