【问题标题】:Why Spring MVC 4 and Hystrix integration not working without Spring boot API?为什么 Spring MVC 4 和 Hystrix 集成在没有 Spring boot API 的情况下无法工作?
【发布时间】: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


【解决方案1】:

当将 AspectJ 与 Spring 一起使用(并且没有 Spring Boot)时,您需要告诉 Spring 对其检测到的 @Aspect 带注释的 bean 执行某些操作。默认情况下,它们会被忽略。

为此,您需要在配置中添加 @EnableAspectJAutoProxy(或 XML 中的 <aop:aspectj-autoproxy />)。这将指示 Spring 应用它在上下文中检测到的 @Aspect 注释 bean。

所有这些都在 Spring 参考指南的 Enabling @AspectJ Support 部分进行了广泛的描述。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-04
    • 2016-06-25
    • 2022-11-24
    • 2020-10-21
    • 2016-02-22
    • 2019-02-07
    • 2019-12-27
    • 2017-10-10
    相关资源
    最近更新 更多