Hystrix 不会抛出 HystrixRuntimeException,而是空消息错误
Hystrix does not throw HystrixRuntimeException, and instead empty message error
我在 spring 中使用 Hystrix(spring-cloud-dependencies
的 Camden.SR7 版本)- 在服务层启动应用程序,没有回退方法。其中一种服务方法如下所示:
@HystrixCommand(commandKey = "prefs",
commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")})
@Override
public void savePrefs(PrefsRequestParams requestParams) throws Exception {
...
}
如果此类方法执行时间超过 2 秒,Hystrix 将抛出 java.util.concurrent.TimeoutException: null,REST 响应将如下所示:
{
"timestamp": 1509452672714,
"status": 500,
"error": "Internal Server Error",
"exception": "java.util.concurrent.TimeoutException",
"message": "No message available",
"path": "/prefs"
}
对于这样的响应,不清楚实际上是从哪个方法抛出异常。如果我将 spring-cloud-dependencies
版本更改为 Brixton.SR5(以前的版本),它会 returns 明确响应:
{
"timestamp": 1509452426819,
"status": 500,
"error": "Internal Server Error",
"exception": "com.netflix.hystrix.exception.HystrixRuntimeException",
"message": "prefs timed-out and fallback failed.",
"path": "/prefs"
}
所以新版本的Hystrix(实际上是新版本的spring-cloud-dependencies)不会抛出HystrixRuntimeException。这是一个错误还是我应该以另一种方式配置 Hystrix 以接收明确的消息错误?
我使用以下 Maven 依赖项:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.2.7.RELEASE</version>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
...
从 Maven 依赖树中我看到它使用 com.netflix.hystrix:hystrix-core:jar:1.5.6:compile
作为 spring-cloud-dependencies
版本 Camden.SR7,
和版本 Brixton.SR5 - com.netflix.hystrix:hystrix-core:jar:1.5.3:compile
.
更新到 Javanica 1.5.12 解决了这个问题。
从 1.5.7 开始,还有一个选项可以为所有未忽略的异常强制抛出 HystrixRuntimeException:
@HystrixCommand(raiseHystrixExceptions = {HystrixException.RUNTIME_EXCEPTION})
@Override
public void savePrefs(PrefsRequestParams requestParams) throws Exception {
...
}
我在 spring 中使用 Hystrix(spring-cloud-dependencies
的 Camden.SR7 版本)- 在服务层启动应用程序,没有回退方法。其中一种服务方法如下所示:
@HystrixCommand(commandKey = "prefs",
commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")})
@Override
public void savePrefs(PrefsRequestParams requestParams) throws Exception {
...
}
如果此类方法执行时间超过 2 秒,Hystrix 将抛出 java.util.concurrent.TimeoutException: null,REST 响应将如下所示:
{
"timestamp": 1509452672714,
"status": 500,
"error": "Internal Server Error",
"exception": "java.util.concurrent.TimeoutException",
"message": "No message available",
"path": "/prefs"
}
对于这样的响应,不清楚实际上是从哪个方法抛出异常。如果我将 spring-cloud-dependencies
版本更改为 Brixton.SR5(以前的版本),它会 returns 明确响应:
{
"timestamp": 1509452426819,
"status": 500,
"error": "Internal Server Error",
"exception": "com.netflix.hystrix.exception.HystrixRuntimeException",
"message": "prefs timed-out and fallback failed.",
"path": "/prefs"
}
所以新版本的Hystrix(实际上是新版本的spring-cloud-dependencies)不会抛出HystrixRuntimeException。这是一个错误还是我应该以另一种方式配置 Hystrix 以接收明确的消息错误?
我使用以下 Maven 依赖项:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.2.7.RELEASE</version>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
...
从 Maven 依赖树中我看到它使用 com.netflix.hystrix:hystrix-core:jar:1.5.6:compile
作为 spring-cloud-dependencies
版本 Camden.SR7,
和版本 Brixton.SR5 - com.netflix.hystrix:hystrix-core:jar:1.5.3:compile
.
更新到 Javanica 1.5.12 解决了这个问题。
从 1.5.7 开始,还有一个选项可以为所有未忽略的异常强制抛出 HystrixRuntimeException:
@HystrixCommand(raiseHystrixExceptions = {HystrixException.RUNTIME_EXCEPTION})
@Override
public void savePrefs(PrefsRequestParams requestParams) throws Exception {
...
}