在添加Hystrix命令后出现错误。

huangapple 未分类评论44阅读模式
英文:

Getting error after adding Hystrix command

问题

添加了Hystrix命令后,出现以下错误 -

执行HystrixCommand.run()时出错。继续回退逻辑...:java.lang.IllegalStateException: 未找到绑定线程的请求:您是否在实际网络请求之外引用请求属性,或者在最初接收线程之外处理请求?如果您实际在网络请求内操作,仍然收到此消息,您的代码可能在DispatcherServlet之外运行:在这种情况下,使用RequestContextListener或RequestContextFilter来暴露当前请求。位于org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)的代码处。

我的应用程序类如下 -

@SpringBootApplication 
@EnableDistributedSession(regionName="DATASERVICES")
@EnableDiscoveryClient
@EnableCircuitBreaker
public class SampleApplication {

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

我发现错误发生在尝试在Service类中获取会话的代码行。我实现了Hystrix的Service类如下 -

@Service
public class TransactionsUSStandardServiceImpl implements TransactionsUSStandardService {

   @Autowired
   private RestTemplate restTemplate;

   @Autowired
   HttpServletRequest request;

   @Override
   @HystrixCommand(commandKey = "getXyzCommand", threadPoolKey = "getXyzThreadPoolKey", fallbackMethod = "xyzFallback")
   public List<String> getXyz(String arg1, HttpHeaders headers, String arg2) throws Exception {
        HttpSession session = request.getSession(false);
        ...
        ...
   }

   public List<String> xyzFallback(String arg1, HttpHeaders headers, String arg2, Throwable t) throws Exception {
        LOGGER.error(".......");
        throw new XyzException("Hystrix CircuitBreaker Fallback while executing getXyz() method", t);
   }

}

在SVN中,我们保留了以下配置 -

hystrix:
  command:
    defaultHystrixCommand:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 30000
    getXyzCommand:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 30000
  threadpool:
    getXyzThreadPoolKey:
      coreSize: 20
      maximumSize: 20
      maxQueueSize: 5

在我的Hystrix配置中是否有什么遗漏吗?

英文:

After adding the Hystrix command am getting the following error -

Error executing HystrixCommand.run(). Proceeding to fallback logic ...: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) at 

My application class looks like this -

@SpringBootApplication 
@EnableDistributedSession(regionName=&quot;DATASERVICES&quot;)
@EnableDiscoveryClient
@EnableCircuitBreaker
public class SampleApplication {

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

I have seen the error is happening at the line where we are trying to get the session in Service class. My service class where I have implemented the Hystrix looks like this -

@Service
public class TransactionsUSStandardServiceImpl implements TransactionsUSStandardService {

   @Autowired
   private RestTemplate restTemplate;

   @Autowired
   HttpServletRequest request;

   @Override
   @HystrixCommand(commandKey = &quot;getXyzCommand&quot;, threadPoolKey = &quot;getXyzThreadPoolKey&quot;, fallbackMethod = &quot;xyzFallback&quot;)
   public List&lt;String&gt; getXyz(String arg1,	HttpHeaders headers, String arg2) throws Exception {
        HttpSession session = request.getSession(false);
        ...
        ...
   }

   public List&lt;String&gt; xyzFallback(String arg1, HttpHeaders headers, String arg2, Throwable t) throws Exception {
	    LOGGER.error(&quot;.......&quot;);
	    throw new XyzException(&quot;Hystrix CircuitBreaker Fallback while executing getXyz() method&quot;, t);
   }

   }

In the SVN we kept our configuration as -

hystrix:
  command:
    defaultHystrixCommand:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 30000
    getXyzCommand:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 30000
  threadpool:
    getXyzThreadPoolKey:
      coreSize: 20
      maximumSize: 20
      maxQueueSize: 5

Am I missing anything in my Hystrix configuration?

huangapple
  • 本文由 发表于 2020年7月27日 14:55:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/63110146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定