Spring Cloud安装指南:从入门到实践
Spring Cloud 搴旂敤瀛︿範鍏ラ棬鎸囧崡锛氫粠鍏ラ棬鍒扮簿閫氱殑寰湇鍔℃鏋舵寚鍗?/p>
鏈寚鍗楀叏闈粙缁嶄簡 Spring Cloud 寰湇鍔℃鏋讹紝浠庡熀鏈蹇点€侀厤缃腑蹇冦€佹湇鍔″彂鐜帮紝鍒伴泦鎴愮瓑鍚勪釜鏂归潰锛屼负鍒濆鑰呮彁渚涗簡涓€鏉℃竻鏅扮殑鍏ラ棬璺緞锛屽苟鍔╂偍鍏ㄩ潰鎺屾彙 Spring Cloud 鐨勪娇鐢ㄦ妧宸с€?/p>
涓€銆丼pring Cloud 姒傝堪
Spring Cloud 鏄熀浜?Spring Boot 鐨勫井鏈嶅姟妗嗘灦锛屼负寮€鍙戣€呮彁渚涗簡闆嗘垚鍜岄厤缃绉嶅井鏈嶅姟鐩稿叧鎶€鏈殑渚挎嵎鏂瑰紡锛屽鏈嶅姟鍙戠幇銆侀厤缃腑蹇冦€佽礋杞藉潎琛°€佹柇璺櫒鍙婃湇鍔$綉鍏崇瓑銆傚叾鐩爣鏄畝鍖栧井鏈嶅姟鐨勫紑鍙戝拰閮ㄧ讲杩囩▼锛屾彁渚涗竴濂楀畬鏁淬€佹槗鐢ㄧ殑寰湇鍔″紑鍙戝伐鍏烽泦銆?/p>
浜屻€侀厤缃腑蹇冧笌 Eureka 鏈嶅姟鍙戠幇
Eureka 鏄?Spring Cloud 涓殑鏈嶅姟鍙戠幇缁勪欢锛岄€氳繃鑷姩娉ㄥ唽鍜屽彂鐜版湇鍔★紝浣垮緱鏈嶅姟闂寸殑閫氫俊鍜岀鐞嗘洿涓轰究鎹枫€傞厤缃?Eureka 鐨勬楠ゅ涓嬶細
1. 娣诲姞渚濊禆锛氬湪椤圭洰鐨?pom.xml 鏂囦欢涓坊鍔?Eureka 鐨勪緷璧栥€?/p>
2. 閰嶇疆鏂囦欢锛氬湪 application.yml 鏂囦欢涓厤缃?Eureka 鐨勬湇鍔′俊鎭€?/p>
3. 閰嶇疆涓績鍚姩锛氬畬鎴愪互涓婃搷浣滃悗锛孍ureka 鏈嶅姟鍣ㄥ皢鍚姩锛屽疄鐜版湇鍔$殑鑷姩娉ㄥ唽涓庡彂鐜板姛鑳姐€?/p>
涓夈€佺瓑宸ョ被鍨嬫鏌ヤ笌 @EnableDiscoveryClient
绛夊伐绫诲瀷妫€鏌ュ湪 Spring Cloud 涓富瑕佹秹鍙?@EnableDiscoveryClient 鐨勪娇鐢紝澹版槑鏈嶅姟鎻愪緵鑰呰韩浠斤紝骞惰嚜鍔ㄦ敞鍐屽埌鏈嶅姟鍙戠幇缁勪欢涓€?/p>
鍥涖€丒ureka 涓?Zuul 鐨勯泦鎴?/h3>
1. Eureka 鏈嶅姟閰嶇疆锛氬湪鏈嶅姟瀹炰緥涓紝閰嶇疆 Eureka 鐨勬湇鍔′俊鎭€?/p>
2. Zuul 缃戝叧闆嗘垚锛歓uul 浣滀负 API 缃戝叧锛屽彲瀹炵幇璺敱銆佽繃婊ょ瓑鍔熻兘銆傞€氳繃娣诲姞 Zuul 鐨勪緷璧栧苟閰嶇疆璺敱瑙勫垯锛屽彲瀹炵幇瀵瑰鏈嶅姟鐨勭粺涓€绠$悊銆?/p>
浜斻€丗eign 涓?Ribbon 鐨勯泦鎴?/h3>
Feign 鏄敤浜庡垱寤?HTTP 瀹㈡埛绔殑搴擄紝绠€鍖栦簡 HTTP 璋冪敤鐨勪唬鐮併€傚湪 Spring Cloud 涓紝缁撳悎 Ribbon 璐熻浇鍧囪 锛孎eign 瀹㈡埛绔彲浠ュ疄鐜版洿鍔犵伒娲诲拰楂樻晥鐨勬湇鍔¤皟鐢ㄣ€?/p>
涓€銆丗eign 瀹㈡埛绔殑渚挎嵎璋冪敤
涓轰簡杞绘澗璋冪敤鏈嶅姟锛屾垜浠鍏堥渶瑕佹坊鍔?Feign 鐨勪緷璧栧苟鍒涘缓 Feign 瀹㈡埛绔€?/p>
娣诲姞渚濊禆
鍦ㄩ」鐩殑渚濊禆绠$悊涓紝寮曞叆 Spring Cloud 鐨?Feign 瀹㈡埛绔惎鍔ㄥ櫒锛?/p>
```xml
```
鍒涘缓 Feign 瀹㈡埛绔?/p>
鍒涘缓涓€涓帴鍙o紝浣跨敤 `@FeignClient` 娉ㄨВ鏍囪锛屼互澹版槑杩欐槸涓€涓?Feign 瀹㈡埛绔€備緥濡傦細
```java
@FeignClient(name = "eureka-client", fallback = EurekaClientFallBack.class)
public interface EurekaClient {
@GetMapping("/test")
String test();
}
```
褰撴湇鍔′笉鍙敤鏃讹紝鍙互瀹氫箟涓€涓洖閫€绫伙紝濡?`EurekaClientFallBack`锛屾潵澶勭悊寮傚父鎯呭喌銆?/p>
浜屻€丷ibbon 璐熻浇鍧囪 鐨勯瓍鍔?/h3>
Ribbon 鎻愪緵浜嗗鎴风璐熻浇鍧囪 鍔熻兘锛岄€氳繃绠€鍗曢厤缃紝鍗冲彲瀹炵幇鍩轰簬绛栫暐鐨勮姹傚垎鍙戙€?/p>
娣诲姞渚濊禆
鍦ㄩ」鐩腑寮曞叆 Ribbon 鐨勪緷璧栵細
```xml
```
閰嶇疆 Ribbon
鍦?`application.yml` 鏂囦欢涓厤缃?Ribbon 鐨勮礋杞藉潎琛$瓥鐣ャ€備緥濡傦細
```yaml
spring:
cloud:
loadbalancer:
ribbon:
enabled: true
```
杩欐牱锛孯ibbon 灏变細鏍规嵁閰嶇疆鐨勭瓥鐣ヨ繘琛岃姹傚垎鍙戯紝鎻愬崌绯荤粺鐨勫彲鎵╁睍鎬у拰鎬ц兘銆?/p>
涓? Spring Cloud Config Server - 閰嶇疆涓績閮ㄧ讲 馃槉 馃槉 馃槉 馃槉 馃槉 馃槉 馃槉 馃槉 馃槉 馃槉 馃槉 馃槉 馃槉 馃槉 馃槉 馃槉 馃槉 馃槉 馃槉 鈥嬮厤缃腑蹇冪鐞嗗簲鐢ㄩ厤缃湁鍔╀簬缁熶竴绠$悊鍜岀淮鎶ゃ€傞儴缃查厤缃腑蹇冪殑姝ラ濡備笅锛氣€嬧€嬫坊鍔犱緷璧栧紩鍏?Config Server 鐨勪緷璧栵細鈥嬧€嬪湪椤圭洰涓坊鍔犱互涓嬩緷璧栵細鈥嬧€嬫坊鍔犱緷璧栤€嬧€嬮厤缃腑蹇冮儴缃茬殑绗竴姝ユ槸娣诲姞 Config Server 鐨勪緷璧栵細 鈥嬪惎鍔ㄦ湇鍔″櫒閫氳繃閰嶇疆绔彛鍜?Git 閰嶇疆浠撳簱鐨勫湴鍧€鏉ュ惎鍔?Config Server 鏈嶅姟鍣ㄣ€備娇鐢?`@EnableConfigServer` 娉ㄨВ鍚敤 Config Server 鏀寔閰嶇疆涓績鍔熻兘銆傞厤缃腑蹇冩搷浣滀富瑕佹秹鍙婅幏鍙栧拰淇敼閰嶇疆鏂囦欢鐨勮繃绋嬨€備娇鐢?`@ConfigurationProperties` 娉ㄨВ缁戝畾閰嶇疆鏂囦欢灞炴€э紝鏂逛究鍦ㄤ唬鐮佷腑浣跨敤銆傗€嬧€嬪ご缁撹矗鎿嶄綔绀轰緥Feign 瀹㈡埛绔彲浠ラ€氳繃娣诲姞鑷畾涔?HTTP 澶达紝浠ヤ究鏇村ソ鍦版帶鍒惰姹傚拰鍝嶅簲銆傞€氳繃杩欑鏂瑰紡锛屽彲浠ョ伒娲诲湴澶勭悊璇锋眰鍜屽搷搴旂殑澶撮儴淇℃伅锛屾弧瓒崇壒瀹氱殑涓氬姟闇€姹傘€傞€氳繃浠ヤ笂姝ラ锛屾垜浠彲浠ヨ交鏉惧湴瀹炵幇鏈嶅姟鐨勪究鎹疯皟鐢ㄣ€佽礋杞藉潎琛$殑閰嶇疆浠ュ強閰嶇疆涓績鐨勭鐞嗗拰鎿嶄綔銆傝繖浜涘姛鑳藉皢澶уぇ鎻愰珮绯荤粺鐨勫彲缁存姢鎬у拰鍙墿灞曟€э紝涓哄井鏈嶅姟鏋舵瀯鎻愪緵寮哄ぇ鐨勬敮鎸併€傞泦鎴怓eign瀹㈡埛绔笌鑷畾涔夆€斺€旀繁鍖朣pring Cloud瀹炶返浣撻獙
鍦ㄥ井鏈嶅姟鏋舵瀯涓紝鎴戜滑甯稿父浼氫娇鐢⊿pring Cloud涓嶧eign瀹㈡埛绔叡鍚屽崗浣滄潵瀹炵幇璺ㄦ湇鍔$殑杩滅▼璋冪敤銆傝繖娆★紝鎴戜滑涓€璧锋潵娣卞叆浜嗚В濡備綍娣诲姞涓€涓嚜瀹氫箟浠ュ寮篎eign瀹㈡埛绔殑鍔熻兘銆?/p>
鎴戜滑瀹氫箟涓€涓悕涓衡€渁pi-service鈥濈殑Feign瀹㈡埛绔帴鍙o紝鐢ㄤ簬涓庤繙绋嬫湇鍔¤繘琛岄€氫俊锛?/p>
```java
@FeignClient(name = "api-service")
public interface ApiService {
@RequestLine("GET /test")
String test(@HeaderMap Map
}
```
鎺ヤ笅鏉ワ紝鎴戜滑鍒涘缓涓€涓嚜瀹氫箟`CustomHeadersFeignClientInterceptor`锛屽畠瀹炵幇浜哷RequestInterceptor`鎺ュ彛銆傝繖涓殑涓昏浠诲姟鏄负姣忎釜璇锋眰娣诲姞鐗瑰畾鐨勮嚜瀹氫箟澶翠俊鎭€傝繖閲屾垜浠负璇锋眰娣诲姞浜嗕竴涓悕涓衡€淴-Custom-Header鈥濈殑澶翠俊鎭紝鍏跺€间负鈥淐ustom Value鈥濄€?/p>
```java
class CustomHeadersFeignClientInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
requestTemplate.header("X-Custom-Header", "Custom Value");
}
}
```
閫氳繃浠ヤ笂鐨勪唬鐮佺ず渚嬶紝鎴戜滑宸茬粡灞曠ず浜嗗浣曞湪Spring Cloud椤圭洰涓泦鎴愯嚜瀹氫箟鍒癋eign瀹㈡埛绔腑銆傚湪瀹為檯鎿嶄綔涓紝杩欑鎶€鏈兘澶熷府鍔╂垜浠疄鐜拌澶氬姛鑳斤紝濡傛坊鍔犺璇佷俊鎭€佹棩蹇楄褰曠瓑銆傞€氳繃杩欑鏂瑰紡锛屾垜浠彲浠ユ洿鐏垫椿鍦扮鐞嗗井鏈嶅姟涔嬮棿鐨勯€氫俊銆傝繖姝f槸瀹炶返鐨勯噸瑕佹€ф墍鍦ㄢ€斺€斿彧鏈夌湡姝e湪椤圭洰涓簲鐢ㄨ繖浜涙妧鏈紝鎴戜滑鎵嶈兘鐪熸鍦版帉鎻″畠浠€傚缓璁湪瀹為檯椤圭洰涓皾璇曞拰搴旂敤杩欎簺鎶€鏈紝浠ユ繁鍖栧Spring Cloud鍜屽井鏈嶅姟鏋舵瀯鐨勭悊瑙c€傛湰娆℃寚鍗椾互娓呮櫚鐨勬楠ゅ拰璇﹀敖鐨勪唬鐮佺ず渚嬩负璇昏€呮彁渚涗簡涓€涓疄璺佃矾寰勶紝甯姪璇昏€呬粠姒傚康鐞嗚В鍒板疄闄呮搷浣滃叏闈㈡帉鎻pring Cloud鐨勫簲鐢ㄤ笌閰嶇疆銆傚笇鏈涗綘鍦ㄥ疄璺典腑鑳藉鎰熷彈鍒拌繖浜涙妧鏈殑榄呭姏涓庝环鍊笺€?/p>
文章从网络整理,文章内容不代表本站观点,转账请注明【蓑衣网】