Spring Boot Hystrix - 在调用rest模板之前会为异常调用回退方法

问题描述

我刚刚在研究断路器模式,并在 baeldung 的网站上看到了一篇很棒的文章。是关于 Spring Cloud hystrix 用于实现这种设计模式。

它说下面的代码(没有我在 getGreeting 方法添加的失败部分)将执行回退方法,如果在 restTemplate 调用期间发生断路器条件。

但是如果我在 rest 模板调用之前添加了一段代码,它会抛出一个异常(除以零的部分),它还会执行回退方法(这里是 defaultGreeting 方法)吗?

@Service
public class GreetingService {

    @HystrixCommand(fallbackMethod = "defaultGreeting")
    public String getGreeting(String username) {
        // failing code before rest template -> I would not ideally put any other code than the network call here but still,sometimes it might be there.
        int x = 0,y = 3;
        int z = y / x;
        
        return new RestTemplate()
          .getForObject("http://localhost:9090/greeting/{username}",String.class,username);
    }
 
    private String defaultGreeting(String username) {
        return "Hello User! what's up?";
    }
    
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)