启用 Hystrix 后 Autowire 不起作用

问题描述

我遇到了一个问题,当我在微服务中添加 Hystrix(EnableHystrix) 时,自动装配不起作用。

控制器类:

@RestController
@RequestMapping("/login")
@Slf4j
public class LoginController {

    @Autowired
    ILoginService loginService;


    @ApiOperation(value = "Validate user credentials",response = LoginUser.class)
    @PostMapping(value = "/validateCredentials")
    private ResponseEntity<LoginUser> validateUserCredentials(@RequestBody LoginUser logginUser) {
        LoginUser user=loginService.validateUser(logginUser);
         ......
         .....
   }
}

ILoginService自动装配在没有 Hystrix 的情况下完美运行。

但是当我添加依赖项时(2.2.6 版发布)

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
 </dependency>

并启用 Hystrix 为:

@EnableFeignClients("com.abcd")
@SpringBootApplication
@EnableHystrix
public class MyApplication{

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class,args);
    }

}

ILoginService 的自动装配失败,并在尝试访问自动装配属性时抛出 NullPointerException

我有这个配置在其他微服务中工作正常,但在这个特定的微服务中失败。这里出了什么问题?

编辑: 堆栈跟踪是:

java.lang.NullPointerException
at com.demo.login.controller.LoginController.validateUserCredentials(LoginController.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(UnkNown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(UnkNown Source)
at java.lang.reflect.Method.invoke(UnkNown Source)
at org.springframework.web.method.support.invocableHandlerMethod.doInvoke(invocableHandlerMethod.java:190)
at org.springframework.web.method.support.invocableHandlerMethod.invokeForRequest(invocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletinvocableHandlerMethod.invokeAndHandle(ServletinvocableHandlerMethod.java:105)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.dispatcherServlet.dodispatch(dispatcherServlet.java:1040)
at org.springframework.web.servlet.dispatcherServlet.doService(dispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcmetricsFilter.doFilterInternal(WebMvcmetricsFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)2021-01-28 17:09:12.999 DEBUG 1800 --- [nio-9200-exec-8] com.demo.login.ExceptionsHelper      : Exception: 

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
at org.apache.tomcat.util.net.socketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(UnkNown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(UnkNown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(UnkNown Source)

解决方法

在目前的形式中,很难说到底发生了什么。 因此,我将提供一些可能导致解决方案的想法:

  1. 你说,

尝试访问自动装配属性时,ILoginService 的自动装配失败并抛出 NullPointerException。

通常,当可以自动装配 bean 时,因为它不存在或其他原因,spring 应该在启动期间抛出异常,并且在 statrup 期间不可能“访问”自动装配的属性,因为系统不会启动,所以这很混乱。也许如果您可以提供堆栈跟踪以及您尝试访问该属性的位置,它可能会变得更加清晰......

  1. 理论上有可能(再次堆栈跟踪可以表明)有人试图在自动装配实际执行之前访问该属性。从技术上讲,它是在 spring 中通过 BeanPostProcessor 实现的,具有一定的优先级,可能会与其他 bean 后处理器竞争。

尝试重写控制器以使用构造函数注入(这样spring会在控制器的构建过程中注入ILoginService

@RestController
public class LoginController {
    // Note,no autowiring here
    private final ILoginService loginService;

    // If you are on old spring you need to mark this constructor with @Autowired,// in the recent spring versions its not required anymore as long as this is the only 
    // constructor of your class
    public LoginController(ILoginService loginService) {
       this.loginService = loginService;
    }
  1. 执行此操作后,尝试在构造函数中放置一个断点并查看究竟注入了什么(在这种情况下,这是构造函数注入的一个优势)