Spring|Spring Cloud Gateway Hystrix fallback获取异常信息的处理

Gateway Hystrix fallback获取异常信息 gateway fallback后,需要知道请求的是哪个接口以及具体的异常信息,根据不同的请求以及异常进行不同的处理。一开始根据网上一篇博客上的做法:
pom.xml:

org.springframework.cloudspring-cloud-starter-gatewayorg.springframework.cloudspring-cloud-starter-netflix-hystrix

application.yml:
spring:cloud:gateway:discovery:locator:enabled: falselowerCaseServiceId: trueroutes:- id: auth-serveruri: lb://MS-OAUTH2-SERVERpredicates:- Path=/**default-filters:- name: Hystrixargs:name: fallbackcmdfallbackUri: forward:/fallback

然后fallback就是这样:
@RestController@Slf4jpublic class FallbackController {@RequestMapping(value = "https://www.it610.com/fallback")@ResponseStatuspublic Mono fallback(ServerWebExchange exchange, Throwable throwable) {Map 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”本身,并且没有异常信息:
Spring|Spring Cloud Gateway Hystrix fallback获取异常信息的处理
文章图片

后来我重新到HystrixGatewayFilterFactory类中去查看,发现了异常信息其实在exchange里:
Spring|Spring Cloud Gateway Hystrix fallback获取异常信息的处理
文章图片

而请求的接口也通过debug找到了:
Spring|Spring Cloud Gateway Hystrix fallback获取异常信息的处理
文章图片

所以将代码改成如下:
@RestController@Slf4jpublic class FallbackController {@RequestMapping(value = "https://www.it610.com/fallback")@ResponseStatuspublic Mono fallback(ServerWebExchange exchange) {Map 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); }}

正常取到请求路径以及异常信息:
Spring|Spring Cloud Gateway Hystrix fallback获取异常信息的处理
文章图片

关于 hystrix 的异常 fallback method wasn't found
消费者服务--service 的实现如下:
@Servicepublic class BookService {@Autowiredpublic RestTemplaterestTemplate; @HystrixCommand(fallbackMethod = "addServiceFallback")publicBookgetBook( IntegerbookId ){returnrestTemplate.getForObject("http://provider-service/boot/book?bookId={bookId}",Book.class , bookId); }publicStringaddServiceFallback(){System.out.println("error addServiceFallback.... "); return"error" ; }}

就会出现如下所述的异常
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri May 25 14:27:51 CST 2018
There was an unexpected error (type=Internal Server Error, status=500).
fallback method wasn't found: addServiceFallback([class java.lang.Integer])
这是因为指定的 备用方法 addServiceFallback 和 原方法getBook 的参数个数,参数类型 不同造成的;
修改addServiceFallback 方法:
publicString addServiceFallback(IntegerbookId){System.out.println("error addServiceFallback.... "); return"error" ; }

继续运行,就会出现如下所述的异常
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri May 25 14:32:24 CST 2018
There was an unexpected error (type=Internal Server Error, status=500).
Incompatible return types. Command method: public com.bmcc.springboot.model.Book com.bmcc.springboot.service.BookService.getBook(java.lang.Integer); Fallback method: public java.lang.String com.bmcc.springboot.service.BookService.addServiceFallback(java.lang.Integer); Hint: Fallback method 'public java.lang.String com.bmcc.springboot.service.BookService.addServiceFallback(java.lang.Integer)' must return: class com.bmcc.springboot.model.Book or its subclass
这是因为指定的 备用方法 addServiceFallback 和 原方法getBook 虽然 参数个数,参数类型 相同 ,但是 方法的返回值类型不同造成的;
修改addServiceFallback 方法:
publicBookaddServiceFallback(IntegerbookId){System.out.println("error addServiceFallback.... "); returnnew Book() ; }

继续运行,这样就可以看到当一个服务提供者异常关闭时, 消费者(消费者采用轮询的方式消费服务)再继续访问服务时,不会抛出异常页面,而是如下:
{"bookId":0,"bookName":null,"price":null,"publisher":null}

【Spring|Spring Cloud Gateway Hystrix fallback获取异常信息的处理】以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    推荐阅读