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. Python通過format函數(shù)格式化顯示值2. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))3. css進(jìn)階學(xué)習(xí) 選擇符4. 詳解Python模塊化編程與裝飾器5. html小技巧之td,div標(biāo)簽里內(nèi)容不換行6. python使用ctypes庫調(diào)用DLL動態(tài)鏈接庫7. python web框架的總結(jié)8. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式9. Python下使用Trackbar實(shí)現(xiàn)繪圖板10. Python性能測試工具Locust安裝及使用
