成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術文章
文章詳情頁

Spring Cloud Gateway Hystrix fallback獲取異常信息的處理

瀏覽:29日期:2023-07-01 13:39:23
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@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('接口調用失敗,URL={}', request.getPath().pathWithinApplication().value(), throwable);result.put('code', 60002);result.put('data', null);result.put('msg', '接口調用失敗!');return Mono.just(result); }}

但是測試發現,這樣取出來的接口地址只是“/fallback”本身,并且沒有異常信息:

Spring Cloud Gateway Hystrix fallback獲取異常信息的處理

后來我重新到HystrixGatewayFilterFactory類中去查看,發現了異常信息其實在exchange里:

Spring Cloud Gateway Hystrix fallback獲取異常信息的處理

而請求的接口也通過debug找到了:

Spring Cloud Gateway Hystrix fallback獲取異常信息的處理

所以將代碼改成如下:

@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('接口調用失敗,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 Cloud Gateway Hystrix fallback獲取異常信息的處理

關于 hystrix 的異常 fallback method wasn’t found

消費者服務--service 的實現如下:

@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' ; }}

就會出現如下所述的異常

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])

這是因為指定的 備用方法 addServiceFallback 和 原方法getBook 的參數個數,參數類型 不同造成的;

修改addServiceFallback 方法:

public String addServiceFallback(Integer bookId){ System.out.println('error addServiceFallback.... '); return 'error' ;}

繼續運行,就會出現如下所述的異常

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

這是因為指定的 備用方法 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
相關文章:
主站蜘蛛池模板: 亚洲精品字幕一区二区三区 | 精品国产三级a∨在线 | 欧美成a | 欧美成人极品怡红院tv | 国产日本一区二区三区 | 免费一区二区三区四区 | 欧美国产精品亚洲精品第一区 | 国内主播福利视频在线观看 | 久久99精品综合国产首页 | 免费永久在线观看黄网 | 国产福利一区二区三区 | 99视频在线看 | 久草福利在线观看 | 乱人伦中文视频在线 | 欧美性猛片xxxxⅹ免费 | 欧美高清一区二区 | 亚洲成人综合视频 | 最近手机高清中文字幕大全7 | 亚洲91| 国产一级视频在线 | 精品视频在线看 | 色一情一乱一乱91av | 国产三级在线视频播放线 | 一区二区三区影院 | 精品一区二区影院在线 | 欧美有码在线观看 | 免费视频久久 | 久久国产精品高清一区二区三区 | 免费人成年短视频在线观看网站 | 亚洲精品区一区二区三区四 | 亚洲高清在线视频 | 日韩精品另类天天更新影院 | 欧美日韩亚洲第一页 | 一区二区三区四区五区六区 | 天天躁日日躁狠狠躁一级毛片 | 成人免费观看永久24小时 | 亚洲国产精品综合久久20 | 波多野结衣一区二区三区高清在线 | 成人做爰免费网站 | 国产一级毛片卡 | 好吊色综合网天天高清 |