【Hystrix】【07】降级方法不器作用

1.注解需要加载直接调用的方法上,嵌套方法调用不起作用,例如getUserSleepRibbonNotHystrixError

2.如果必须调用可以获取bean调用通过aop代理调用

3.如果不是spring管理的类,无法通过Aware获取ApplicationContext,可以考虑把ApplicationContext做成类变量

4.demo

package com.zhenzhen.demo.springcloud2.orderservice.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zhenzhen.demo.springcloud2.orderservice.service.OrderService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderServiceImpl implements OrderService, ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Autowired
    private RestTemplate restTemplate;

    public String getUserSleepRibbonNotHystrixError(String time) {
        return getUserSleepRibbon(time);
    }

    public String getUserSleepRibbonNotHystrixOk(String time) {
        return applicationContext.getBean(OrderServiceImpl.class).getUserSleepRibbon(time);
    }

    @Override
    @HystrixCommand(fallbackMethod = "defaultStores")
    public String getUserSleepRibbon(String time) {
        return restTemplate.getForObject("http://USER/sleep?time="+time, String.class);
    }

    public String defaultStores(String time) {
        return "服务发生降级";
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值