如何解决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 举报,一经查实,本站将立刻删除。