【酷q框架源码】【易源码软件】【公墓 flash 源码】Unicorn源码
1.springcloud2022ï¼
springcloud2022ï¼
å¾®æå¡æ¡æ¶ä¹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ï¼,酷q框架源码è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;
}
}