Spring Cloud Gateway Hystrix fallback获取异常信息
Spring Cloud Gateway熔断降级获取详细异常信息
·
gateway fallback后,需要知道请求的是哪个接口以及具体的异常信息,根据不同的请求以及异常进行不同的处理。一开始根据网上一篇博客上的做法:
pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
application.yml:
spring:
cloud:
gateway:
discovery:
locator:
enabled: false
lowerCaseServiceId: true
routes:
- id: auth-server
uri: lb://MS-OAUTH2-SERVER
predicates:
- Path=/**
default-filters:
- name: Hystrix
args:
name: fallbackcmd
fallbackUri: forward:/fallback
然后fallback就是这样:
@RestController
@Slf4j
public class FallbackController {
@RequestMapping(value = "/fallback")
@ResponseStatus
public Mono<Map<String, Object>> fallback(ServerWebExchange exchange, Throwable throwable) {
Map<String, Object> result = new HashMap<>(3);
ServerHttpRequest request = exchange.getRequest();
log.error("接口调用失败,URL={}", request.getPath().pathWithinApplication().value(), throwable);
result.put("code", 60002);
result.put("data", null);
result.put("msg", "接口调用失败!");
return Mono.just(result);
}
}
但是测试发现,这样取出来的接口地址只是“/fallback”本身,并且没有异常信息:
后来我重新到HystrixGatewayFilterFactory类中去查看,发现了异常信息其实在exchange里:
而请求的接口也通过debug找到了:
所以将代码改成如下:
@RestController
@Slf4j
public class FallbackController {
@RequestMapping(value = "/fallback")
@ResponseStatus
public Mono<Map<String, Object>> fallback(ServerWebExchange exchange) {
Map<String, Object> result = new HashMap<>(3);
result.put("code", 60002);
result.put("data", null);
Exception exception = exchange.getAttribute(ServerWebExchangeUtils.HYSTRIX_EXECUTION_EXCEPTION_ATTR);
ServerWebExchange delegate = ((ServerWebExchangeDecorator) exchange).getDelegate();
log.error("接口调用失败,URL={}", delegate.getRequest().getURI(), exception);
if (exception instanceof HystrixTimeoutException) {
result.put("msg", "接口调用超时");
} else if (exception != null && exception.getMessage() != null) {
result.put("msg", "接口调用失败: " + exception.getMessage());
} else {
result.put("msg", "接口调用失败");
}
return Mono.just(result);
}
}
正常取到请求路径以及异常信息:
更多推荐
已为社区贡献1条内容
所有评论(0)