问题
I successfully configured spring-cloud
(via spring-cloud-starter-hystrix
) to wrap a call to a service.
This all works fine and looks like the following:
@Component
public class MyService {
@HystrixCommand(fallbackMethod = "fallback")
public void longRunning() {
// this could fail
}
public void fallback() {
// fallback code
}
}
My question now is, I would like to log some statistics about the execution error in longRunning()
Trying to access HystrixRequestLog.getCurrentRequest()
within the fallback method throws
java.lang.IllegalStateException: HystrixRequestContext.initializeContext() must be called at the beginning of each request before RequestVariable functionality can be used.
I am looking for a simple way to log the exception of longRunning
if the fallback is called.
testing with v1.0.0.RC2
回答1:
To see a stack trace you can just enable DEBUG logging in com.netflix.hystrix
.
As far as I can tell, to use the HystrixRequestContext
the caller of MyService
has to call HystrixRequestContext.initializeContext()
before using the service. That sucks, so if anyone has a better idea, I'm interested.
回答2:
Starting from Javanica v1.4.21, it allows fallback method to have an argument of Throwable type for accessing the command execution exception like so:
public void fallback(Throwable e) {
// fallback code
LOGGER.error(e.getMessage());
}
To get this feature, your build config needs to override the older version of Javanica pulled in by Spring Cloud.
来源:https://stackoverflow.com/questions/28323773/hystrix-getting-access-to-the-current-execution-state-within-fallback