您好,登录后才能下订单哦!
本篇内容介绍了“Spring Cloud 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); } }
正常取到请求路径以及异常信息:
消费者服务--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}
“Spring Cloud Gateway Hystrix fallback怎么获取异常信息”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。