1.Spring Boot 项目配合Hystrix 实现全局RestController超时熔断(api超过30秒返回timeout)
2.Hystrixä»ç»
3.springcloud2022ï¼
Spring Boot 项目配合Hystrix 实现全局RestController超时熔断(api超过30秒返回timeout)
为了在Spring Boot项目中实现全局RestController的超时熔断,当API调用超过秒时返回timeout,我们需要寻找一种更高效的方法,避免手动为上百个控制器添加@HystrixCommand注解,收盘买源码这将是一项繁重的任务。起初,监听按键 源码我尝试使用切面编程(AspectJ)包裹RestController,配置@HystrixCommand,但这似乎与HystrixCommand的特性产生了冲突。通过深入Hystrix源码分析和实例化HystrixCommand,我成功实现了全局的超时熔断策略,无需每个API都单独标注。
实现的源码授权平台关键在于在pom文件中添加适当的Hystrix依赖,并确保返回值使用ResponseEntity以控制响应码。对于非ResponseEntity类型的返回值,通过优化处理,spring路由源码可以避免超时后返回的意外情况。这个过程虽然注解方式简洁,但控制不当可能引发问题。container源码解读在遇到注解失效或效果不佳时,通过创建私有实例进行管理通常更易于调试和问题定位。
Spring Boot的starter虽然提供了快速搭建项目的便利,但在精细化控制上可能面临挑战。因此,对于这类场景,注解的便利性和实例化管理的灵活性是需要权衡的。
Hystrixä»ç»
对Hystrixè³é»å·²ä¹ ï¼æè¿å好æ³å¨é¡¹ç®ä¸ä½¿ç¨è¿ä¸ªç¥å¨å°±é¡ºå¸¦ç 究äºä¸æï¼å¾å¤ç»èæ¥ä¸åæ·±å ¥ç 究åªè½æå®è§ä¸çå个æ¦å¿µè®²è§£ä¸ä¸ï¼è¿ä¸ªä»ç»çç´ æ大é½æ¥èªgithubä¸çHystrixå®ç½ã
æè°ä¸å¾èåè¨ï¼ä½å¡è½å¤ç¨å¾çæ¥è¡¨ç¤ºèä¸è½å¤è¡¨ç¤ºæ¸ æ¥çï¼å°±ä¸å¤ç¨æåæè¿°äºï¼çå¾è¯å®æ¯çæåè¦è®©äººæ¥çæ´ç½ä¸äºãå½ç¶æè¿æ¯é常建议å»githubä¸çHystrixå®æ¹wikiå»çåæ±åå³çææ¡£ï¼å¨åèæç®é¨åå·²ç»ç»åºäºé¾æ¥ã
æåæä¸ç¹ï¼å°±æ¯å¨Hystrixçå®ç°å½ä¸å¤§é使ç¨äºRxJavaçå¼æºå çææ¯ï¼è¿ä¸ªææ¯ä¹å没æä¹ç 究è¿ï¼æ以åé¢çå¾å¤æºç çåææ´å¤ä¾§éè¿ç¨åæèä¸ä¼æ·±å ¥ç»èï¼æå ´è¶£çå¯ä»¥èªå·±æ·±å ¥ç 究ä¸ï¼æå°±åå¤åªå¤©å¾ç©ºå¥½å¥½å»ç 究ä¸ä¸ï¼æ¯ç«RxJavaè¿ä¸ªä¸è¥¿å·ç§°æ¯ä¸ä¸ªéè¿ä½¿ç¨å¯è§å¯åºåæ¥ç¼åå¼æ¥ååºäºäºä»¶çç¨åºçåºã
hystrixçåºç°å³ä¸ºè§£å³éªå´©æåºï¼å®éè¿å个æ¹é¢çæºå¶æ¥è§£å³è¿ä¸ªé®é¢
Hystrixçé离主è¦æ¯ä¸ºæ¯ä¸ªä¾èµç»ä»¶æä¾ä¸ä¸ªé离ç线ç¨ç¯å¢ï¼æä¾ä¸¤ç§æ¨¡å¼çé离ï¼
Hystrixççæå¨å ¶å®å¯ä»¥ç解为就æ¯ä¸ä¸ªç»è®¡ä¸å¿ï¼ç»è®¡ä¸å®æ¶é´çªå£å 访é®æ¬¡æ°ï¼æå次æ°ï¼å¤±è´¥æ¬¡æ°çæ°å¼å¤å®æ¯å¦åççæãåççµè·¯çæçè¿ç¨å¦ä¸ï¼
hystrixå·¥ä½åç-è±æç
hystrixå·¥ä½åç-ä¸æç
å ³äºRxJavaç详解
springcloudï¼
å¾®æå¡æ¡æ¶ä¹SpringCloudç®ä»
å¨äºè§£SpringCloudä¹åå äºè§£ä¸ä¸å¾®æå¡æ¶æéè¦èéçæ ¸å¿å ³é®ç¹ï¼å¦ä¸å¾ï¼
对äºä»¥ä¸çæ ¸å¿å ³é®ç¹çå¤çï¼ä¸éè¦æ们éå¤é 车轮ï¼SpringCloudå·²ç»å¸®æ们éæäºï¼å®ä½¿ç¨SpringBooté£æ ¼å°ä¸äºæ¯è¾æççå¾®æå¡æ¡æ¶ç»åèµ·æ¥ï¼å±è½æäºå¤æçé ç½®åå®ç°åçï¼ä¸ºå¿«éæ建微æå¡æ¶æçåºç¨æä¾äºä¸å¥åºç¡è®¾æ½å·¥å ·åå¼åæ¯æã
SpringCloudææä¾çæ ¸å¿åè½å å«ï¼
SpringCloudæ¶æå¾
SpringCloudå项ç®
SpringCloudæä¸çå项ç®å¤§è´å¯ä»¥å为两类ï¼
å¦ä¸ï¼
1.SpringCloudä¸SpringBoot
SpringBootå¯ä»¥è¯´æ¯å¾®æå¡æ¶æçæ ¸å¿ææ¯ä¹ä¸ãéè¿å¨SpringBootåºç¨ä¸æ·»å SpringMVCä¾èµï¼å°±å¯ä»¥å¿«éå®ç°åºäºRESTæ¶æçæå¡æ¥å£ï¼å¹¶ä¸å¯ä»¥æä¾å¯¹HTTPæ åå¨ä½çæ¯æãèä¸SpringBooté»è®¤æä¾JackJsonåºååæ¯æï¼å¯ä»¥è®©æå¡æ¥å£è¾å ¥ãè¾åºæ¯æJSONçãå æ¤ï¼å½ä½¿ç¨SpringCloudè¿è¡å¾®æå¡æ¶æå¼åæ¶ï¼ä½¿ç¨SpringBootæ¯ä¸æ¡å¿ ç»ä¹è·¯ã
2.SpringCloudä¸æå¡æ²»ç(Eureka)
æå¡æ²»çæ¯SpringCloudçæ ¸å¿ï¼å¨å®ç°ä¸å ¶æä¾äºä¸¤ä¸ªéæ©ï¼å³ConsulåNetflixçEurekaã
Eurekaæä¾äºæå¡æ³¨åä¸å¿ãæå¡åç°å®¢æ·ç«¯ï¼ä»¥å注åæå¡çUIçé¢åºç¨ã
å¨Eurekaçå®ç°ä¸ï¼èç¹ä¹é´ç¸äºå¹³çï¼æé¨å注åä¸å¿âææâä¹ä¸ä¼å¯¹æ´ä¸ªåºç¨é æå½±åï¼å³ä½¿é群åªå©ä¸ä¸ªèç¹åæ´»ï¼ä¹å¯ä»¥æ£å¸¸å°æ²»çæå¡ãå³ä½¿æææå¡æ³¨åèç¹é½å®æºï¼Eureka客æ·ç«¯ä¸æç¼åçæå¡å®ä¾å表信æ¯ï¼ä¹å¯è®©æå¡æ¶è´¹è è½å¤æ£å¸¸å·¥ä½ï¼ä»èä¿éå¾®æå¡ä¹é´äºç¸è°ç¨çå¥å£®æ§ååºç¨çå¼¹æ§ã
3.SpringCloudä¸å®¢æ·ç«¯è´è½½åè¡¡(Ribbon)
Ribboné»è®¤ä¸Eureakè¿è¡æ ç¼æ´åï¼å½å®¢æ·ç«¯å¯å¨çæ¶åï¼ä»Eurekaæå¡å¨ä¸è·åä¸ä»½æå¡æ³¨åå表并维æ¤å¨æ¬å°ï¼å½æå¡æ¶è´¹è éè¦è°ç¨æå¡æ¶ï¼Ribbonå°±ä¼æ ¹æ®è´è½½åè¡¡çç¥éæ©ä¸ä¸ªåéçæå¡æä¾è å®ä¾å¹¶è¿è¡è®¿é®ã
SpringCloudéè¿éæNetflixçFeign项ç®ï¼ä¸ºå¼åè æä¾äºå£°æå¼æå¡è°ç¨ï¼ä»èç®åäºå¾®æå¡ä¹é´çè°ç¨å¤çæ¹å¼ã并ä¸é»è®¤Feign项ç®éæäºRibbonï¼ä½¿å¾å£°æå¼è°ç¨ä¹æ¯æ客æ·ç«¯è´è½½åè¡¡åè½ã
4.SpringCloudä¸å¾®æå¡å®¹éãé级(Hystrix)
为äºç»å¾®æå¡æ¶ææä¾æ´å¤§çå¼¹æ§ï¼å¨SpringCloudä¸ï¼éè¿éæNetflixä¸å项ç®Hystrixï¼éè¿ææä¾ç@HystrixCommand注解å¯ä»¥è½»æ¾ä¸ºæ们æå¼åçå¾®æå¡æä¾å®¹éãåéãé级çåè½ãæ¤å¤ï¼Hystrixä¹é»è®¤éæå°Feignå项ç®ä¸ã
Hystrixæ¯æ ¹æ®âæè·¯å¨â模å¼èå建ãå½Hystrixçæ§å°ææå¡åå åçæ éä¹åï¼å°±ä¼è¿å ¥æå¡çæå¤çï¼å¹¶åè°ç¨æ¹è¿åä¸ä¸ªç¬¦åé¢æçæå¡é级å¤çï¼fallbackï¼ï¼èä¸æ¯é¿æ¶é´ççå¾ æè æåºè°ç¨å¼å¸¸ï¼ä»èä¿éæå¡è°ç¨æ¹ç线ç¨ä¸ä¼è¢«é¿æ¶é´ãä¸å¿ è¦å°å ç¨ï¼é¿å æ éå¨åºç¨ä¸çè延é æçéªå´©æåºã
èHystrixç仪表ç项ç®ï¼Dashboardï¼å¯ä»¥çæ§å个æå¡è°ç¨ææ¶èçæ¶é´ã请æ±æ°ãæåççï¼éè¿è¿ç§è¿ä¹å®æ¶ççæ§ååè¦ï¼å¯ä»¥åæ¶åç°ç³»ç»ä¸æ½å¨é®é¢å¹¶è¿è¡å¤çã
5.SpringCloudä¸æå¡ç½å ³(Zuul)
SpringCloudéè¿éæNetflixä¸çZuulå®ç°APIæå¡ç½å ³åè½ï¼æä¾å¯¹è¯·æ±çè·¯ç±åè¿æ»¤ä¸¤ä¸ªåè½
è·¯ç±åè½è´è´£å°å¤é¨è¯·æ±è½¬åå°å ·ä½çå¾®æå¡å®ä¾ä¸ï¼æ¯å®ç°å¤é¨è®¿é®ç»ä¸å ¥å£çåºç¡ã
è¿æ»¤å¨åè½åè´è´£å¯¹è¯·æ±çå¤çè¿ç¨è¿è¡å¹²é¢ï¼æ¯å®ç°è¯·æ±æ ¡éªãæå¡èåçåè½çåºç¡ã
éè¿Zuulï¼å¯ä»¥å°ç»ç²åº¦çæå¡ç»åèµ·æ¥æä¾ä¸ä¸ªç²ç²åº¦çæå¡ï¼ææ请æ±é½å¯¼å ¥ä¸ä¸ªç»ä¸çå ¥å£ï¼å¯¹å¤æ´ä¸ªæå¡åªéè¦æ´é²ä¸ä¸ªAPIæ¥å£ï¼å±è½äºæå¡ç«¯çå®ç°ç»èãéè¿Zuulçåå代çåè½ï¼å¯ä»¥å®ç°è·¯ç±å¯»åï¼å°è¯·æ±è½¬åå°å端çç²ç²åº¦æå¡ä¸ï¼å¹¶åä¸äºéç¨çé»è¾å¤çãæ¤å¤ï¼Zuulé»è®¤ä¼ä¸Eurekaæå¡å¨è¿è¡æ´åï¼èªå¨ä»Eurekaæå¡å¨ä¸è·åææ注åçæå¡å¹¶è¿è¡è·¯ç±æ å°ï¼å®ç°APIæå¡ç½å ³èªå¨é ç½®ã
6.SpringCloudä¸æ¶æ¯ä¸é´ä»¶(Stream)
SpringCloud为ç®ååºäºæ¶æ¯çå¼åï¼æä¾äºStreamå项ç®ï¼éè¿å»ºç«æ¶æ¯åºç¨æ½è±¡å±ï¼æ建äºæ¶æ¯æ¶åãåç»æ¶è´¹åæ¶æ¯åççåè½å¤çï¼å°ä¸å¡åºç¨ä¸çæ¶æ¯æ¶åä¸å ·ä½æ¶æ¯ä¸é´ä»¶è¿è¡è§£è¦ï¼ä½¿å¾®æå¡åºç¨å¼åä¸å¯ä»¥é常æ¹ä¾¿å°ä¸KafkaåRabbitMQçæ¶æ¯ä¸é´ä»¶è¿è¡éæã
SpringCloudBusåºäºStreamè¿è¡æ©å±ï¼å¯ä»¥ä½ä¸ºå¾®æå¡ä¹é´çäºä»¶ãæ¶æ¯æ»çº¿ï¼ç¨äºæå¡é群ä¸ç¶æååçä¼ æã
æ¯å¦SpringCloudConfigåå©Busï¼å¯ä»¥å®ç°é ç½®çå¨æå·æ°å¤çã
7.SpringCloudä¸åå¸å¼é ç½®ä¸å¿(Config)
é对微æå¡æ¶æä¸çé ç½®æ件管çéæ±ï¼SpringCloudæä¾äºä¸ä¸ªConfigå项ç®ãSpringCloudConfigå ·æä¸å¿åãçæ¬æ§å¶ãæ¯æå¨ææ´æ°åè¯è¨ç¬ç«çç¹æ§ã
å¨Configå项ç®ä¸å°å¾®æå¡åºç¨å为两ç§è§è²ï¼é ç½®æå¡å¨ï¼ConfigServerï¼åé 置客æ·ç«¯ï¼ConfigClientï¼ã使ç¨é ç½®æå¡å¨éä¸å°ç®¡çææé ç½®å±æ§æ件ï¼é ç½®æå¡ä¸å¿å¯ä»¥å°é ç½®å±æ§æ件åå¨å°GitãSVNçå ·æçæ¬ç®¡çä»åºä¸ï¼ä¹å¯ä»¥åæ¾å¨æ件系ç»ä¸ãé»è®¤éç¨Gitçæ¹å¼è¿è¡åå¨ï¼å æ¤å¯ä»¥å¾å®¹æå°å¯¹é ç½®æ件è¿è¡ä¿®æ¹ï¼å¹¶å®ç°çæ¬æ§å¶ã
8.SpringCloudä¸å¾®æå¡é¾è·¯è¿½è¸ª(Sleuth)
SpringCloudä¸çSleuthå项ç®ä¸ºå¼åè æä¾äºå¾®æå¡ä¹é´è°ç¨çé¾è·¯è¿½è¸ªã
Sleuthæ ¸å¿ææ³å°±æ¯éè¿ä¸ä¸ªå ¨å±çIDå°åå¸å¨åå¾®æå¡æå¡èç¹ä¸ç请æ±å¤ç串èèµ·æ¥ï¼è¿åäºè°ç¨å ³ç³»ï¼å¹¶åå©æ°æ®åç¹ï¼å®ç°å¯¹å¾®æå¡è°ç¨é¾è·¯ä¸çæ§è½æ°æ®çééã
å æ¤ï¼éè¿Sleuthå¯ä»¥å¾æ¸ æ¥å°äºè§£å°ä¸ä¸ªç¨æ·è¯·æ±ç»è¿äºåªäºæå¡ãæ¯ä¸ªæå¡å¤çè±è´¹äºå¤é¿æ¶é´ï¼ä»èå¯ä»¥å¯¹ç¨æ·ç请æ±è¿è¡åæãæ¤å¤ï¼éè¿å°ééçæ°æ®åéç»Zipkinè¿è¡åå¨ãç»è®¡ååæï¼ä»èå¯ä»¥å®ç°å¯è§åçåæåå±ç¤ºï¼å¸®å©å¼åè 对微æå¡å®æ½ä¼åå¤çã
9.SpringCloudä¸å¾®æå¡å®å ¨(Security)
SpringCloudSecurity为æ们æä¾äºä¸ä¸ªè®¤è¯åé´æçå®å ¨æ¡æ¶ï¼å®ç°äºèµæºææã令ç管ççåè½ï¼åæ¶ç»åZuulå¯ä»¥å°è®¤è¯ä¿¡æ¯å¨å¾®æå¡è°ç¨è¿ç¨ä¸ç´æ¥ä¼ éï¼ç®åäºæ们è¿è¡å®å ¨ç®¡æ§çå¼åã
SpringCloudSecurityé»è®¤æ¯æOAuth2.0认è¯åè®®ï¼å æ¤åç¹ç»å½ä¹å¯ä»¥é常容æå®ç°ï¼å¹¶ä¸OAuth2.0æçæç令çå¯ä»¥ä½¿ç¨JWTçæ¹å¼ï¼è¿ä¸æ¥ç®åäºå¾®æå¡ä¸çå®å ¨ç®¡çã
.SpringCloudçå ¶ä»å项ç®
èªå®ä¹springcloud-gatewayçæå¤ç
ä¸ãåºæ¯
使ç¨springcloudgatewayåï¼æäºçæï¼é®é¢ä¹å°±éä¹èæ¥ï¼æå¡é´è°ç¨æäºhystrixå¯ä»¥åæ¶çæé¤åæ¥å£ãåæå¡çé®é¢ï¼å¯¹ç³»ç»å¾æ帮å©ãä½æ¯ï¼ä¸æ¯ææçæ¥å£é½æ¯æçæ¶é´å å®æçï¼ä¸æ¯ææçæ¥å£é½å¯ä»¥è®¾ç½®ä¸æ ·çè¶ æ¶æ¶é´çï¼
é£ä¹æ们é¢ä¸´ä¸ä¸ªé®é¢ï¼é£å°±æ¯ç¾åä¹çæ¥å£é½å¯ä»¥å¨1så å®ç¾å®æï¼ä½æ¯å°±æ¯é£å 个ç¹æ®æ¥å£ï¼éè¦åå ç§ï¼å åç§ççå¾ æ¶é´ï¼èé»è®¤çæçæ¶é´ååªæä¸ä¸ªã
äºãåæ
å¨åé¢springcloudgatewayæºç 解æä¹è¯·æ±ç¯ä¸æ们ç¥é请æ±ä¼ç»è¿ä¸äºåçè¿æ»¤å¨ï¼GatewayFilterï¼,èspringcloudgatewayçé级çæå¤çå°±æ¯ç±ä¸ä¸ªç¹æ®çè¿æ»¤å¨æ¥å¤ççï¼éè¿æºç åææä»¬å ³æ³¨å°HystrixGatewayFilterFactoryè¿ä¸ªç±»ï¼è¿ä¸ªç±»çä½ç¨å°±æ¯ç产GatewayFilterç¨çï¼æ们çä¸å®çå®ç°
å¯ä»¥çå°çº¢æ¡å¤æåæ建äºä¸ä¸ªå¿åçGatewayFilter对象è¿åï¼è¿ä¸ªå¯¹è±¡å¨æ¥å£è¯·æ±è¿ç¨ä¸ä¼è¢«å è½½å°è¿æ»¤å¨é¾æ¡ä¸ï¼ä»ç»çå°è¿éæ¯å建äºä¸ä¸ªRouteHystrixCommandè¿ä¸ªå½ä»¤å¯¹è±¡ï¼æç»è°ç¨command.toObservable()æ¹æ³å¤ç请æ±ï¼å¦æè¶ æ¶çæè°ç¨resumeWithFallbackæ¹æ³
éè¿æºç åægatewayå¨è·¯ç±æ¶å¯ä»¥æå®HystrixCommandKeyï¼å¹¶ä¸å¯¹HystrixCommandKeyè®¾ç½®è¶ æ¶æ¶é´
ä¸ãæ¹æ¡
ç¥éç½å ³çæçåç就好åäº,èªå®ä¹çæçè¿æ»¤å¨é ç½®å°æ¥å£è¯·æ±è¿ç¨ä¸ï¼ç±è¿æ»¤å¨æ¥è¯»åæ¥å£çæé 置并æ建HystrixObservableCommandå¤ç请æ±ã
èªå®ä¹ä¸ä¸ªç±»XXXGatewayFilterFactory继æ¿AbstractGatewayFilterFactoryï¼å°apiå对åºçtimeouté ç½®åï¼æ¥å®ç°ç»åå°å ·ä½æ¥å£ççæé ç½®ï¼å ·ä½å®ç°å¦ä¸ï¼
packageorg.unicorn.framework.gateway.filter;
importcn.hutool.core.collection.CollectionUtil;
importcom.netflix.hystrix.HystrixCommandGroupKey;
importcom.netflix.hystrix.HystrixCommandKey;
importcom.netflix.hystrix.HystrixCommandProperties;
importcom.netflix.hystrix.HystrixObservableCommand;
importcom.netflix.hystrix.exception.HystrixRuntimeException;
importorg.springframework.beans.factory.ObjectProvider;
importorg.springframework.cloud.gateway.filter.GatewayFilter;
importorg.springframework.cloud.gateway.filter.GatewayFilterChain;
importorg.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
importorg.springframework.cloud.gateway.support.ServerWebExchangeUtils;
importorg.springframework.cloud.gateway.support.TimeoutException;
importorg.springframework.core.annotation.AnnotatedElementUtils;
importorg.springframework.mand;
if(CollectionUtil.isNotEmpty(apiTimeoutList)){
//requestå¹é å±äºé£ç§æ¨¡å¼
ApiHystrixTimeoutapiHystrixTimeout=getApiHystrixTimeout(apiTimeoutList,path);command=newUnicornRouteHystrixCommand(config.getFallbackUri(),exchange,chain,initSetter(apiHystrixTimeout.getApiPattern(),apiHystrixTimeout.getTimeout()));}else{
command=newUnicornRouteHystrixCommand(config.getFallbackUri(),exchange,chain,initSetter(serviceId(exchange),null));
}
returncommand;
}/
***@paramapiTimeoutList*@parampath*@return*/privateApiHystrixTimeoutgetApiHystrixTimeout(ListapiTimeoutList,Stringpath){for(ApiHystrixTimeoutapiTimeoutPattern:apiTimeoutList){
if(this.antPathMatcher.match(apiTimeoutPattern.getApiPattern(),path)){
returnapiTimeoutPattern;
}}
ApiHystrixTimeoutapiHystrixTimeout=newApiHystrixTimeout();
apiHystrixTimeout.setApiPattern("default");
apiHystrixTimeout.timeout=null;
returnapiHystrixTimeout;
}@Override
publicGatewayFilterapply(Configconfig){return(exchange,chain)-{
UnicornRouteHystrixCommandcommand=initUnicornRouteHystrixCommand(exchange,chain,config);
returnMono.create(s-{Subscriptionsub=command.toObservable().subscribe(s::success,s::error,s::success);
s.onCancel(sub::unsubscribe);
}).onErrorResume((Function)throwable-{if(throwableinstanceofHystrixRuntimeException){
HystrixRuntimeExceptione=(HystrixRuntimeException)throwable;
HystrixRuntimeException.FailureTypefailureType=e.getFailureType();switch(failureType){caseTIMEOUT:
returnMono.error(newTimeoutException());
caseCOMMAND_EXCEPTION:{
Throwablecause=e.getCause();
if(causeinstanceofResponseStatusException||AnnotatedElementUtils.findMergedAnnotation(cause.getClass(),ResponseStatus.class)!=null){
returnMono.error(cause);
}}
default:
break;
}}