简体   繁体   English

启用 Hystrix 后自动装配不起作用

[英]Autowire doesn't work after enabling Hystrix

I'm encountering a issue, where autowiring doesn't work when I add Hystrix(EnableHystrix) in my microservice.我遇到了一个问题,当我在我的微服务中添加 Hystrix(EnableHystrix) 时自动装配不起作用。

Controller class: Controller class:

@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);
         ......
         .....
   }
}

Autowiring of ILoginService works perfectively without Hystrix. ILoginService的自动装配在没有 Hystrix 的情况下也能完美运行。

But when I add the dependency( Version 2.2.6 Release)但是当我添加依赖项时(版本 2.2.6 发布)

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

And enable Hystrix as:并启用 Hystrix 作为:

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

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

}

The Autowire of ILoginService fails and NullPointerException is thrown when trying to access the autowired property. ILoginService 的自动装配失败,并在尝试访问自动装配属性时抛出NullPointerException

I have this configuration working fine in other microservices, But fails in this particular microservice.我有这个配置在其他微服务中工作正常,但在这个特定的微服务中失败了。 What is going wrong here?这里出了什么问题?

EDIT: StackTrace is:编辑:StackTrace 是:

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)

In the current form, its kind of hard to tell what exactly happens.在目前的形式下,很难说清楚到底发生了什么。 So I'll provide some thoughts that can lead to the solution:所以我将提供一些可以导致解决方案的想法:

  1. You say,你说,

The Autowire of ILoginService fails and NullPointerException is thrown when trying to access the autowired property. ILoginService 的自动装配失败,并在尝试访问自动装配属性时抛出 NullPointerException。

Usually when its possible to autowire a bean because it doesn't exist or something, spring should throw an exception during the startup and it will be impossible to "access" the autowired property during the statrup because the system won't start, so this is confusing.通常当它可以自动装配一个 bean 因为它不存在或其他东西时,spring 应该在启动期间抛出异常并且在 statrup 期间不可能“访问”自动装配的属性,因为系统不会启动,所以这个令人困惑。 Maybe if you could provide a stacktrace and the place where you're trying to access the property it could become more clear...也许如果您可以提供堆栈跟踪以及您尝试访问该属性的位置,它可能会变得更加清晰......

  1. Its possible in theory (again the stacktrace could show that) that someone tries to access the property before the autowiring actually executes.理论上(再次堆栈跟踪可以表明)有人试图在自动装配实际执行之前访问该属性是可能的。 Technically its implemented in spring via BeanPostProcessor with certain priority that might compete with other bean post processors.从技术上讲,它是通过 BeanPostProcessor 在 spring 中实现的,具有一定的优先级,可能会与其他 bean 后处理器竞争。

Try to rewrite the controller to use constructor injection (so that spring would inject the ILoginService during the construction of the controller:尝试重写 controller 以使用构造函数注入(这样 spring 将在ILoginService的构造期间注入 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. After you're doing this, try to place a breakpoint in the constructor and see what exactly gets injected (this is an advantage of constructor injection in this case)在你这样做之后,尝试在构造函数中放置一个断点并查看究竟注入了什么(在这种情况下这是构造函数注入的优势)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM