【发布时间】:2016-05-04 06:14:19
【问题描述】:
在我开始之前,让我告诉你我正在尝试将 Spring MVC 4 应用程序与 Hystrix 集成(即使用 hystrix-javanica 来获得完整的注释支持)。下面是我的一段代码....
配置类:
@Configuration
public class BeanConfig {
@Bean
public HystrixCommandAspect hystrixCommandAspect() {
return new HystrixCommandAspect();
}
}
支持 Hystrix 的服务类
@Service(value="userRepository")
public class UserRepositoryImpl implements UserRepository{
@Override
@HystrixCommand(fallbackMethod = "failService",
commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")
},
threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "30"),
@HystrixProperty(name = "maxQueueSize", value = "101"),
@HystrixProperty(name = "keepAliveTimeMinutes", value = "2"),
@HystrixProperty(name = "queueSizeRejectionThreshold", value = "15"),
@HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"),
@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440")
})
public User getUserByAuthentication(String username) {
throw new RuntimeException("delegately throwing exception");//intentionally throwing exception to check fallback service
}
@HystrixCommand
public User failService(String username) {
System.out.println("in the fallback service");
return new User(username);
}
}
这是控制器类
@Autowired
@Qualifier("userRepository")
private UserRepository userRepository;
@RequestMapping(method = RequestMethod.POST)
public String init(HttpServletRequest request, HttpServletResponse response) throws InterruptedException, ExecutionException {
System.out.println("in controller getting value as :" + userRepository.getUserByAuthentication("testvalue"));
return "some page";
}
现在,当我运行这个应用程序时,会抛出异常,但之后不会调用任何后备服务。我尝试调试,但在异常发生后工作流停止。
谁能帮我解决这个问题?
【问题讨论】:
-
将
@EnableAspectJAutoProxy添加到您的配置中。 -
像魅力一样工作......非常感谢@M。 Deinum
-
也为我工作。谢谢@M.Deinum
-
@M.Deinum 请把它写成答案,而不是评论。
标签: java spring spring-mvc netflix hystrix