springcloud——服务消费者方全局服务降级的使用

存在的问题:
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 "全局服务降级!!!!!!";
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值