hystrix不会抛出hystrixruntimeexception,而是抛出空消息错误

eblbsuwk  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(409)

我使用hystrix(camden.sr7版本) spring-cloud-dependencies )在没有回退方法的服务层上的spring引导应用程序中。服务的方法之一如下所示:

@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(以前的版本),它返回明确的响应:

{
    "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云依赖)不会抛出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 .

m528fe3b

m528fe3b1#

更新到javanica1.5.12解决了这个问题。
从1.5.7开始,还有一个选项可以强制为所有未忽略的异常抛出hystrixruntimeexception:

@HystrixCommand(raiseHystrixExceptions = {HystrixException.RUNTIME_EXCEPTION})
@Override
public void savePrefs(PrefsRequestParams requestParams) throws Exception {
    ...
}

相关问题