@HysrixCommand、@DefaultProperties设置降级服务注意

本文介绍了如何在Spring Boot中使用Hystrix注解实现服务降级,包括一致返回的策略和两种不同方式指定fallback方法:默认属性和方法匹配。通过示例展示了如何在`@HystrixCommand`中配置并理解fallbackMethod和defaultFallback的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. @HysrixCommand()

使用此注解设置降级服务时:

  1. 降级服务返回值与原服务返回值保持一致
  2. 降级服务的参数与原服务参数保持一致
  3. fallbackMethod属性查找的是参数、返回值和原服务保持一致的降级方法(一般使用此属性设置降级方法
  4. defaultFallback属性是查找返回值和原服务一致且参数为空的降级方法
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
@HystrixCommand(fallbackMethod = "payment_Global_FallbackMethod")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
    String res = paymentHystrixService.paymentInfo_TimeOut(id);
    return res;
}

public String payment_Global_FallbackMethod(@PathVariable("id") Integer id) {
    return "Global异常处理信息,请稍后再试,/(ㄒoㄒ)/~~";
}

2. @DefaultProperties

  1. 该注解标注在controller类上面,定义默认的回退方法
    在这里插入图片描述
  2. 原服务标注@HystrixCommand注解,但是不用再声明降级服务方法名
  3. 降级服务方法不应有参数
  4. 降级服务返回值与原服务返回值保持一致
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
@HystrixCommand
public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
    String res = paymentHystrixService.paymentInfo_TimeOut(id);
    return res;
}

public String payment_Global_FallbackMethod() {
    return "Global异常处理信息,请稍后再试,/(ㄒoㄒ)/~~";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值