Spring Cloud Gateway Hystrix fallback獲取異常信息的處理
gateway fallback后,需要知道請求的是哪個接口以及具體的異常信息,根據(jù)不同的請求以及異常進(jìn)行不同的處理。一開始根據(jù)網(wǎng)上一篇博客上的做法:
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@Slf4jpublic 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('接口調(diào)用失敗,URL={}', request.getPath().pathWithinApplication().value(), throwable);result.put('code', 60002);result.put('data', null);result.put('msg', '接口調(diào)用失敗!');return Mono.just(result); }}
但是測試發(fā)現(xiàn),這樣取出來的接口地址只是“/fallback”本身,并且沒有異常信息:
后來我重新到HystrixGatewayFilterFactory類中去查看,發(fā)現(xiàn)了異常信息其實(shí)在exchange里:
而請求的接口也通過debug找到了:
所以將代碼改成如下:
@RestController@Slf4jpublic 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('接口調(diào)用失敗,URL={}', delegate.getRequest().getURI(), exception);if (exception instanceof HystrixTimeoutException) { result.put('msg', '接口調(diào)用超時');} else if (exception != null && exception.getMessage() != null) { result.put('msg', '接口調(diào)用失敗: ' + exception.getMessage());} else { result.put('msg', '接口調(diào)用失敗');}return Mono.just(result); }}
正常取到請求路徑以及異常信息:
消費(fèi)者服務(wù)--service 的實(shí)現(xiàn)如下:
@Servicepublic 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' ; }}
就會出現(xiàn)如下所述的異常
Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri May 25 14:27:51 CST 2018There was an unexpected error (type=Internal Server Error, status=500).fallback method wasn’t found: addServiceFallback([class java.lang.Integer])
這是因?yàn)橹付ǖ?備用方法 addServiceFallback 和 原方法getBook 的參數(shù)個數(shù),參數(shù)類型 不同造成的;
修改addServiceFallback 方法:
public String addServiceFallback(Integer bookId){ System.out.println('error addServiceFallback.... '); return 'error' ;}
繼續(xù)運(yùn)行,就會出現(xiàn)如下所述的異常
Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri May 25 14:32:24 CST 2018There 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
這是因?yàn)橹付ǖ?備用方法 addServiceFallback 和 原方法getBook 雖然 參數(shù)個數(shù),參數(shù)類型 相同 ,但是 方法的返回值類型不同造成的;
修改addServiceFallback 方法:
public Book addServiceFallback(Integer bookId){ System.out.println('error addServiceFallback.... '); return new Book() ;}
繼續(xù)運(yùn)行,這樣就可以看到當(dāng)一個服務(wù)提供者異常關(guān)閉時, 消費(fèi)者(消費(fèi)者采用輪詢的方式消費(fèi)服務(wù))再繼續(xù)訪問服務(wù)時,不會拋出異常頁面,而是如下:
{'bookId':0,'bookName':null,'price':null,'publisher':null}
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 如何在jsp界面中插入圖片2. ASP實(shí)現(xiàn)加法驗(yàn)證碼3. python selenium 獲取接口數(shù)據(jù)的實(shí)現(xiàn)4. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)5. 詳解JSP 內(nèi)置對象request常見用法6. 利用ajax+php實(shí)現(xiàn)商品價格計算7. Python matplotlib 繪制雙Y軸曲線圖的示例代碼8. jsp EL表達(dá)式詳解9. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除10. springboot集成與使用Sentinel的方法
