Spring Cloud Gateway Hystrix fallback获取异常信息的处理
作者:指尖凉
Gateway Hystrix fallback获取异常信息
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); } }
正常取到请求路径以及异常信息:
关于 hystrix 的异常 fallback method wasn't found
消费者服务--service 的实现如下:
@Service public class BookService { @Autowired public RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "addServiceFallback") public Book getBook( Integer bookId ){ return restTemplate.getForObject("http://provider-service/boot/book?bookId={bookId}",Book.class , bookId); } public String addServiceFallback(){ 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 方法:
public String addServiceFallback(Integer bookId){ 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 方法:
public Book addServiceFallback(Integer bookId){ System.out.println("error addServiceFallback.... "); return new Book() ; }
继续运行,这样就可以看到当一个服务提供者异常关闭时, 消费者(消费者采用轮询的方式消费服务)再继续访问服务时,不会抛出异常页面,而是如下:
{"bookId":0,"bookName":null,"price":null,"publisher":null}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。