存在的问题:
1、每个业务方法对应一个兜底的方法,代码膨胀。
2、统一和自定义的分开。
解决方法:使用@DefaultProperties(defaultFallback=“”)
1:1 每个业务方法配置一个服务降级方法,技术上可以,实际上很傻X。
1:N 除了个别重要核心业务有专属,其他普通的可以通过@DefaultProperties(defaultFallback=“”)
统一跳转到统一处理结果页面。
通用的和独享的各自分开,避免代码膨胀,合理减少了代码量。
代码
package com.springcloud.controller;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.springcloud.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author dc
* @date 2020/7/29 - 15:33
*/
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class PaymentHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping("/consumer/payment/hystrix/ok")
public String paymentInfo_OK(Integer id) {
String result = paymentHystrixService.paymentInfo_OK(id);
return result;
}
@GetMapping("/consumer/payment/hystrix/timeout")
/*@HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties =
{@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
value="1500")})*/
//fallbackMethod:服务降级方法
//name:属性名 value:属性值(当控制器运行时间超过该值时,将会进入服务降级方法)
@HystrixCommand //声明服务降级注解
public String paymentInfo_TimeOut(Integer id) {
String result = paymentHystrixService.paymentInfo_TimeOut(id);
return result;
}
/*public String paymentTimeOutFallbackMethod(Integer id) {
return "我是消费者81,对方支付系统繁忙请10秒钟后再试或者自己运行出错,请检查自己!";
}*/
//全局服务降级方法
public String payment_Global_FallbackMethod() {
return "全局服务降级!!!!!!";
}
}