初识 Spring Cloud 和微服务
Tags: springcloud, dubbo, microservices
Published: 2017-10-12 04:17:56

---
title: åˆè¯† Spring Cloud å’Œå¾®æœåŠ¡
published: true
description: äº†è§£ Spring Cloud çš„ç»„æˆï¼Œç»„ä»¶ï¼Œä¸Ž Dubbo çš„å¯¹æ¯”ï¼Œå¾®æœåŠ¡ç‰¹æ€§
tags: spring cloud, dubbo, microservices
---


###[Spring Cloud åœ¨å›½å†…ä¸­å°åž‹å…¬å¸èƒ½ç”¨èµ·æ¥å—ï¼Ÿ](http://www.ityouknow.com/springcloud/2017/09/11/can-use-springcloud.html)
![spring cloud applicable?](https://thepracticaldev.s3.amazonaws.com/i/vcreojyx5mwve1x4cx1b.jpg)

###[å¤§è¯ Spring Cloud](http://www.ityouknow.com/springcloud/2017/05/01/simple-springcloud.html)
![microservices concerns](https://thepracticaldev.s3.amazonaws.com/i/cozngarse85g3qyz74j2.png)

![microservices_springcloud](https://thepracticaldev.s3.amazonaws.com/i/ypica8toc390twisov7t.png)

###Spring Cloud ä¸‹çš„å·¥ç¨‹
* Spring Cloud Configï¼šä¾é  git ä»“åº“å®žçŽ°çš„ä¸­å¿ƒåŒ–é…ç½®ç®¡ç†ã€‚é…ç½®èµ„æºå¯ä»¥æ˜ å°„åˆ° Spring çš„ä¸åŒå¼€å‘çŽ¯å¢ƒä¸­ï¼Œä½†æ˜¯ä¹Ÿå¯ä»¥ä½¿ç”¨åœ¨éž Spring åº”ç”¨ä¸­ã€‚
* Spring Cloud Netflixï¼šä¸åŒçš„ Netflix OSS ç»„ä»¶çš„é›†åˆï¼šEurekaã€Hystrixã€Zuulã€Archaius ç­‰ã€‚
* Spring Cloud Busï¼šäº‹ä»¶æ€»çº¿ï¼Œåˆ©ç”¨åˆ†å¸ƒå¼æ¶ˆæ¯å°†å¤šä¸ªæœåŠ¡è¿žæŽ¥èµ·æ¥ã€‚éžå¸¸é€‚åˆåœ¨é›†ç¾¤ä¸­ä¼ æ’­çŠ¶æ€çš„æ”¹å˜äº‹ä»¶ï¼ˆä¾‹å¦‚ï¼šé…ç½®å˜æ›´äº‹ä»¶ï¼‰
* Spring Cloud Consulï¼šæœåŠ¡å‘çŽ°å’Œé…ç½®ç®¡ç†ï¼Œç”± Hashicorp å›¢é˜Ÿå¼€å‘ã€‚
![sc_projects](https://thepracticaldev.s3.amazonaws.com/i/ep6cd1bi6ln3zaenyofd.png)

###Spring Cloud Netflix
* æœåŠ¡å‘çŽ°ï¼šEureka-server å®žä¾‹ä½œä¸ºæœåŠ¡æä¾›è€…ï¼Œå¯ä»¥æ³¨å†Œåˆ°æœåŠ¡æ³¨å†Œä¸­å¿ƒï¼ŒEureka å®¢æˆ·ç«¯å¯ä»¥é€šè¿‡ Spring ç®¡ç†çš„ bean å‘çŽ°å®žä¾‹ï¼›
* æ–­è·¯å™¨ï¼šåˆ©ç”¨æ³¨è§£ï¼Œå¯ä»¥åˆ›å»ºä¸€ä¸ªç®€å•çš„ Hystrix å®¢æˆ·ç«¯ï¼›é€šè¿‡ Java é…ç½®æ–‡ä»¶å¯ä»¥åˆ›å»ºå†…åµŒçš„ Hystrix æŽ§åˆ¶é¢æ¿ï¼›
* å£°æ˜Žå¼ REST å®¢æˆ·ç«¯ï¼šä½¿ç”¨ Feign å¯ä»¥åˆ›å»ºå£°æ˜Žå¼ã€æ¨¡æ¿åŒ–çš„ HTTP å®¢æˆ·ç«¯ï¼›
* å®¢æˆ·ç«¯è´Ÿè½½å‡è¡¡å™¨ï¼šRibbon
* è·¯ç”±å™¨å’Œè¿‡æ»¤å™¨ï¼šZuul å¯ä»¥åœ¨å¾®æœåŠ¡æž¶æž„ä¸­æä¾›è·¯ç”±åŠŸèƒ½ã€èº«ä»½éªŒè¯ã€æœåŠ¡è¿ç§»ã€é‡‘ä¸é›€å‘å¸ƒç­‰åŠŸèƒ½ï¼Œè¯¦è§ [æ¨¡å—æž¶æž„å›¾ ZUUL]()ã€‚

###Dubbo vs Spring Cloud
![dubbo_springcloud](https://thepracticaldev.s3.amazonaws.com/i/jedxz2vd7vaecsskq6xh.png)

![dubbo_springcloud2](https://thepracticaldev.s3.amazonaws.com/i/rwxmmu94bwhf9e3x75uy.jpg)

###å¾®æœåŠ¡çŽ¯å¢ƒå±‚çº§
![å¾®æœåŠ¡çŽ¯å¢ƒå±‚çº§](https://thepracticaldev.s3.amazonaws.com/i/6yv0wams0vlout769xyb.jpg)

###ä¹å¤§ç‰¹æ€§
â€”â€” æ¥è‡ª Martin Fowler çš„ [Microservices](https://martinfowler.com/articles/microservices.html)
* æœåŠ¡ç»„ä»¶åŒ–
* æŒ‰ä¸šåŠ¡ç»„ç»‡å›¢é˜Ÿ
* åš â€œäº§å“â€ çš„æ€åº¦
* æ™ºèƒ½ç«¯ç‚¹ä¸Žå“‘ç®¡é“
* åŽ»ä¸­å¿ƒåŒ–æ²»ç†
* åŽ»ä¸­å¿ƒåŒ–ç®¡ç†æ•°æ®
* åŸºç¡€è®¾æ–½è‡ªåŠ¨åŒ–
* å®¹é”™è®¾è®¡
* æ¼”è¿›å¼è®¾è®¡

###æ€Žæ ·ä½¿ä¸€ä¸ªå¾®æœåŠ¡ â€œäº§å“å°±ç»ªâ€
â€”â€” æ¥è‡ª Susan Fowler-Rigetti  åŽŸæ–‡ä¸è¯¦ï¼Œå¼•è‡ªï¼š[å®žçŽ°å¾®æœåŠ¡ï¼Œæ¯ä¸ªç»„ç»‡éƒ½ä¼šé¢ä¸´çš„ 6 ä¸ªæŒ‘æˆ˜](https://mp.weixin.qq.com/s/mIC2QkEigDYUSomWW66YKw)
* ç¨³å®šæ€§å’Œå¯é æ€§
ä½¿ç”¨å¾®æœåŠ¡ï¼Œä¼šå¸¦æ¥æ›´å¤šçš„å˜æ›´å’Œæ›´å¿«çš„éƒ¨ç½²ï¼Œè¿™å°±å¯¼è‡´äº†ä¸ç¨³å®šæ€§ã€‚å¥¹ç§°ï¼Œä¸€ä¸ªå¯é çš„å¾®æœåŠ¡åº”è¯¥æ˜¯èƒ½å¤Ÿè¢«å…¶å®¢æˆ·ã€ä¾èµ–å’Œç”Ÿæ€æ‰€ä¿¡ä»»çš„ã€‚å¥¹è®¤ä¸ºç¨³å®šæ€§å’Œå¯é æ€§æ˜¯æ¯æ¯ç›¸å…³çš„ï¼Œå¤§å¤šæ•°ç¨³å®šæ€§éœ€æ±‚ä¼šä¼´éšå¯é æ€§éœ€æ±‚ã€‚ä¸€æ¡åœ¨è¿›å…¥ç”Ÿäº§çŽ¯å¢ƒå‰å…·å¤‡å¤šä¸ªé˜¶æ®µçš„éƒ¨ç½²æµæ°´çº¿å°±æ˜¯ä¸€ä¸ªå¾ˆå¥½çš„ä¾‹å­ã€‚
* å¯æ‰©å±•æ€§å’Œæ€§èƒ½
å¤§å¤šæ•°äººè®¤ä¸ºä»–ä»¬èƒ½å¤Ÿé€šè¿‡å¾®æœåŠ¡æ— å¿èŽ·å¾—å¯æ‰©å±•æ€§ï¼Œä½†è¿™å¯¹äºŽå·¨å¤§çš„è§„æ¨¡è€Œè¨€å¹¶éžå¦‚æ­¤ã€‚éšç€æµé‡çš„å¢žåŠ ï¼Œå®ƒä»¬åº”è¯¥å¾—åˆ°é€‚å½“çš„æ‰©å±•ã€‚
è®¸å¤šè¯­è¨€åœ¨è®¾è®¡ä¸Šå°±æ— æ³•åšåˆ°é«˜æ•ˆçš„æ‰©å±•ï¼Œå› ä¸ºå®ƒä»¬æ— æ³•åšåˆ°å¹¶å‘ã€åˆ†åŒºå’Œé«˜æ•ˆã€‚è¿™ä½¿å¾—ç”¨é‚£äº›è¯­è¨€å¼€å‘çš„å¾®æœåŠ¡éš¾ä»¥å¾—åˆ°åˆç†çš„æ‰©å±•ã€‚Fowler-Rigetti è°¢ç»æŒ‡å‡ºå…·ä½“çš„è¯­è¨€ï¼Œä½†å¥¹è¯´ï¼Œâ€œæˆ‘å¾ˆè‚¯å®šè‡ªå·±èƒ½æƒ³åˆ°ä¸€äº›ã€‚â€
å¥¹è§£é‡Šé“ï¼Œå¯æ‰©å±•æ€§æ˜¯æŒ‡å¾®æœåŠ¡èƒ½å¤Ÿå¤„ç†å¤šå°‘è¯·æ±‚ (è¯‘è€…ï¼šå¯æ‰©å±•æ€§æ˜¯æŒ‡ç³»ç»Ÿä¸ºåº”å¯¹ä¸šåŠ¡å¢žåŠ è€Œå¯¹è‡ªèº«è¿›è¡Œç›¸åº”æ‰©å±•çš„èƒ½åŠ›ï¼Œæœ‰äº›æ—¶å€™ä¹Ÿä½œä¼¸ç¼©æ€§ï¼Œå³åœ¨ä¸šåŠ¡ç¼©å‡çš„æ—¶å€™ï¼Œç³»ç»Ÿè§„æ¨¡åšç›¸åº”çš„æ”¶ç¼©)ï¼Œæ€§èƒ½åˆ™æ˜¯æŒ‡æœåŠ¡èƒ½å¤Ÿå¤šå¥½åœ°å¤„ç†è¿™äº›ä»»åŠ¡ (è¯‘è€…ï¼šè¿™åº”è¯¥å« QoS)ã€‚ä¸€ä¸ªé«˜æ€§èƒ½çš„æœåŠ¡åº”è¯¥åˆç†åœ°ä½¿ç”¨èµ„æºã€é«˜æ•ˆåœ°å¤„ç†ä»»åŠ¡ã€å¿«é€Ÿåœ°å¤„ç†è¯·æ±‚ã€‚
å¦‚æžœå¾®æœåŠ¡æ— æ³•å¾—åˆ°é¢„æœŸçš„æ‰©å±•ï¼Œé‚£ä¹ˆå…¶å‡ºçŽ°æ•…éšœçš„æ¦‚çŽ‡ä¼šæ€¥å‰§ä¸Šå‡ã€‚å»¶è¿Ÿçš„å¢žé•¿ä¼šå¯¼è‡´ä½Žä¸‹çš„å¯ç”¨æ€§ã€‚
* å®¹é”™å’Œç¾å¤‡
ä¸ºäº†ä¿è¯å¯ç”¨æ€§è¿™ä¸ªç»ˆæžç›®æ ‡ï¼Œå¼€å‘è€…éœ€è¦ç¡®ä¿ä»»ä½•å¾®æœåŠ¡å‡ºçŽ°æ•…éšœåŽå‡ä¸ä¼šå¯¼è‡´ç³»ç»Ÿå®•æŽ‰ã€‚å› æ­¤å¼€å‘è€…éœ€è¦çŸ¥é“æ‰€æœ‰çš„æ•…éšœæ¨¡å¼ï¼Œå¹¶ä¸”åšå¥½å¤‡ä»½å·¥ä½œï¼Œä»¥åº”å¯¹æ•…éšœçš„å‘ç”Ÿã€‚
æˆåŠŸç¾å¤‡çš„å…³é”®æ˜¯å¥å£®çš„å¼¹æ€§æµ‹è¯•ï¼Œå®ƒåŒ…æ‹¬ä»£ç æµ‹è¯•ã€è´Ÿè½½æµ‹è¯•ï¼Œä»¥åŠå«å…¶å®ƒä¸»åŠ¨æ€§æµ‹è¯•çš„æ··æ²Œæµ‹è¯•ã€‚æ¯ä¸ªæ•…éšœæ¨¡å¼éƒ½åº”è¯¥åœ¨ç”Ÿäº§çŽ¯å¢ƒä¸­å¤çŽ°ï¼Œä»¥è§‚å¯Ÿèƒ½å¦ â€œå­˜æ´»â€ã€‚
ç»™å®šå¾®æœåŠ¡çŽ¯å¢ƒçš„å¤æ‚åº¦å’Œå¤æ‚çš„ä¾èµ–é“¾ï¼Œæ•…éšœæ˜¯éš¾ä»¥é¿å…çš„ï¼ˆè¯‘è€…ï¼šæ€»è§‰å¾—è¿™å¥è¯æœ¬æ¥å°±æœ‰é—®é¢˜ï¼‰ã€‚å¾®æœåŠ¡å¿…é¡»èƒ½å¤Ÿæ‰¿å—æ¥è‡ªå†…éƒ¨å’Œå¤–éƒ¨çš„æ•…éšœã€‚
* ç›‘æŽ§å’Œæ–‡æ¡£
Fowler-Rigetti è¯´ï¼Œâ€œæˆ‘å‘çŽ°åœ¨å¾®æœåŠ¡æž¶æž„ä¸­ï¼Œç³»ç»Ÿçš„çŠ¶æ€æ°¸è¿œå’Œä¸Šä¸€ç§’ä¸åŒã€‚å¦‚æžœä½ ä¸çŸ¥æ™“ç³»ç»Ÿçš„çŠ¶æ€ï¼Œåœ¨ç³»ç»Ÿæ•…éšœæ—¶ä½ å°†æ— æ³•å¾—çŸ¥ï¼Œè¿™ä¼šå¯¼è‡´æœ€ç»ˆçš„å¤±è´¥ã€‚â€
æ‹¥æœ‰ä¸€æ¬¾å¥½çš„ç›‘æŽ§å·¥å…·æ¥å±•ç¤ºç³»ç»Ÿåœ¨ä»»æ„æ—¶åˆ»çš„çŠ¶æ€ï¼Œè¿™æ˜¯å¾ˆå…³é”®çš„ã€‚ç¼ºå°‘è‰¯å¥½çš„ç›‘æŽ§å·¥å…·æ˜¯é€ æˆæœåŠ¡ä¸­æ–­çš„ç¬¬äºŒå¤§åŽŸå› ã€‚
æ—¥å¿—æ˜¯ç›‘æŽ§çš„æœ¬è´¨éƒ¨åˆ†ï¼Œå› ä¸ºä½ å°†å‡ ä¹Žä¸å¯èƒ½å¤çŽ° bugã€‚çŸ¥æ™“å‘ç”Ÿäº†ä»€ä¹ˆçš„å”¯ä¸€æ–¹å¼å°±æ˜¯ç¡®ä¿ä½ åœ¨é‚£æ—¶è®°å½•äº†ç³»ç»Ÿçš„çŠ¶æ€ã€‚å”¯ä¸€çš„æ‰‹æ®µä¾¿æ˜¯åˆç†çš„æ—¥å¿—è®°å½•ã€‚è¿™ä½¿å¾—æˆ‘ä»¬èƒ½å¤Ÿè½»æ¾ä¿¡ä»»æœåŠ¡ã€‚
æ–‡æ¡£å¯¹äºŽæ¯ä¸ªå¼€å‘è€…è€Œè¨€éƒ½æ˜¯é˜»ç¢ï¼Œä½†å®ƒç¡®å®žæ˜¯å…³é”®çš„ã€‚å®ƒç§»é™¤äº†æŠ€æœ¯å€ºåŠ¡ï¼Œå¹¶ä½¿å¾—å›¢é˜Ÿæ–°æˆå‘˜èƒ½å¤Ÿèµ¶ä¸Šè¿›åº¦ã€‚

###[å¾®æœåŠ¡å’Œæ¼”è¿›å¼æž¶æž„](http://insights.thoughtworkers.org/evolutionary-architecture-micro-services/)