Spring Cloud安装指南:从入门到实践

当前位置:首页 > 广场 > Spring Cloud安装指南:从入门到实践

Spring Cloud安装指南:从入门到实践

2024-11-29广场11

Spring Cloud 搴旂敤瀛︿範鍏ラ棬鎸囧崡锛氫粠鍏ラ棬鍒扮簿閫氱殑寰湇鍔℃鏋舵寚鍗?/p>

鏈寚鍗楀叏闈粙缁嶄簡 Spring Cloud 寰湇鍔℃鏋讹紝浠庡熀鏈蹇点€侀厤缃腑蹇冦€佹湇鍔″彂鐜帮紝鍒伴泦鎴愮瓑鍚勪釜鏂归潰锛屼负鍒濆鑰呮彁渚涗簡涓€鏉℃竻鏅扮殑鍏ラ棬璺緞锛屽苟鍔╂偍鍏ㄩ潰鎺屾彙 Spring Cloud 鐨勪娇鐢ㄦ妧宸с€?/p>

涓€銆丼pring Cloud 姒傝堪

Spring Cloud 鏄熀浜?Spring Boot 鐨勫井鏈嶅姟妗嗘灦锛屼负寮€鍙戣€呮彁渚涗簡闆嗘垚鍜岄厤缃绉嶅井鏈嶅姟鐩稿叧鎶€鏈殑渚挎嵎鏂瑰紡锛屽鏈嶅姟鍙戠幇銆侀厤缃腑蹇冦€佽礋杞藉潎琛°€佹柇璺櫒鍙婃湇鍔$綉鍏崇瓑銆傚叾鐩爣鏄畝鍖栧井鏈嶅姟鐨勫紑鍙戝拰閮ㄧ讲杩囩▼锛屾彁渚涗竴濂楀畬鏁淬€佹槗鐢ㄧ殑寰湇鍔″紑鍙戝伐鍏烽泦銆?/p>

浜屻€侀厤缃腑蹇冧笌 Eureka 鏈嶅姟鍙戠幇

Eureka 鏄?Spring Cloud 涓殑鏈嶅姟鍙戠幇缁勪欢锛岄€氳繃鑷姩娉ㄥ唽鍜屽彂鐜版湇鍔★紝浣垮緱鏈嶅姟闂寸殑閫氫俊鍜岀鐞嗘洿涓轰究鎹枫€傞厤缃?Eureka 鐨勬楠ゅ涓嬶細

Spring Cloud安装指南:从入门到实践

1. 娣诲姞渚濊禆锛氬湪椤圭洰鐨?pom.xml 鏂囦欢涓坊鍔?Eureka 鐨勪緷璧栥€?/p>

Spring Cloud安装指南:从入门到实践

2. 閰嶇疆鏂囦欢锛氬湪 application.yml 鏂囦欢涓厤缃?Eureka 鐨勬湇鍔′俊鎭€?/p>

Spring Cloud安装指南:从入门到实践

3. 閰嶇疆涓績鍚姩锛氬畬鎴愪互涓婃搷浣滃悗锛孍ureka 鏈嶅姟鍣ㄥ皢鍚姩锛屽疄鐜版湇鍔$殑鑷姩娉ㄥ唽涓庡彂鐜板姛鑳姐€?/p>

Spring Cloud安装指南:从入门到实践

涓夈€佺瓑宸ョ被鍨嬫鏌ヤ笌 @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

org.springframework.cloud

spring-cloud-starter-openfeign

```

鍒涘缓 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

org.springframework.cloud

spring-cloud-starter-netflix-ribbon

```

閰嶇疆 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 headers);

}

```

鎺ヤ笅鏉ワ紝鎴戜滑鍒涘缓涓€涓嚜瀹氫箟`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>

文章从网络整理,文章内容不代表本站观点,转账请注明【蓑衣网】

本文链接:https://www.baoguzi.com/66365.html

Spring Cloud安装指南:从入门到实践 | 分享给朋友: