姒傝堪
Spring Boot浼佷笟绾у紑鍙戝疄鎴樺叏闈㈡帰璁ㄤ簡鏋勫缓楂樻晥銆佸彲鎵╁睍鐨勭幇浠d紒涓氬簲鐢ㄧ殑鍏抽敭鎶€鏈拰瀹炶返銆傛湰鏂囨棬鍦ㄥ府鍔╁紑鍙戣€呯啛缁冩帉鎻pring Boot鍦ㄤ紒涓氱骇寮€鍙戜腑鐨勫簲鐢紝浠庡熀纭€鍏ラ棬鍒伴珮绾у疄璺碉紝鍐呭鐢熷姩涓旀繁鍏ャ€?/p>
涓€銆丼pring Boot鍩虹鍏ラ棬
浣犻渶瑕佺‘淇濅綘鐨勫紑鍙戠幆澧冨凡瀹夎浜咼ava寮€鍙戝伐鍏峰寘锛圝DK锛夈€傛帴涓嬫潵锛屾垜浠潵瀹夎骞堕厤缃甋pring Boot鐜銆?/p>
1. 瀹夎Spring Initializr
鎵撳紑娴忚鍣紝璁块棶Spring Initializr缃戠珯銆係pring Initializr鏄疭pring Boot椤圭洰蹇€熸惌寤虹殑绁炲櫒銆傛寜鐓т互涓嬫楠ら厤缃」鐩細
閫夋嫨椤圭洰绫诲瀷锛氶€夋嫨Maven Project鎴朑radle Project銆?/p>
閫夋嫨璇█锛氶€夋嫨Java浣滀负寮€鍙戣瑷€銆?/p>
閫夋嫨渚濊禆锛氫负浜嗗垱寤轰竴涓熀鏈殑Web椤圭洰锛岄€夋嫨Spring Web銆?/p>
閰嶇疆椤圭洰淇℃伅锛氬~鍐欓」鐩悕绉般€佺粍ID銆侀」鐩甀D鍜岀増鏈彿銆?/p>
涓嬭浇椤圭洰锛氱偣鍑籇ownload锛屽皢椤圭洰缁撴瀯鍜岄厤缃枃浠朵笅杞藉埌鏈湴銆?/p>
2. 閰嶇疆Spring Boot搴旂敤
鎵撳紑涓嬭浇鐨勯」鐩紝鎵惧埌src/main/java鐩綍涓嬬殑涓荤被锛屾坊鍔犱竴涓畝鍗曠殑Spring Boot搴旂敤鍚姩绫籄pplication.java銆傚湪src/main/resources鏂囦欢澶逛腑娣诲姞涓€涓猘pplication.properties鏂囦欢锛岀敤浜庨厤缃簲鐢ㄥ睘鎬с€備繚瀛樺苟杩愯椤圭洰鍚庯紝閫氳繃娴忚鍣ㄨ闂甽ocalhost:8080锛屼綘灏嗙湅鍒颁竴涓粯璁ょ殑娆㈣繋椤甸潰銆?/p>
浜屻€丼pring Boot鏍稿績姒傚康绠€浠?/h3>
Spring Boot鎻愪緵浜嗕竴绯诲垪杞婚噺绾х殑瑙e喅鏂规锛屾棬鍦ㄥ揩閫熸瀯寤哄拰閮ㄧ讲搴旂敤绋嬪簭銆備互涓嬫槸鏍稿績姒傚康鐨勮缁嗕粙缁嶏細
1. 閰嶇疆灞炴€?/p>
Spring Boot閫氳繃鑷姩鍔犺浇閰嶇疆灞炴€ф枃浠讹紙濡俛pplication.properties鎴朼pplication.yml锛夋潵绠€鍖栧簲鐢ㄩ厤缃€備緥濡傦紝浣跨敤@SpringBootApplication娉ㄨВ涓嶄粎鍚姩搴旂敤锛岃繕鍖呮嫭鑷姩閰嶇疆鍔熻兘鍜岀粍浠舵壂鎻忋€?/p>
2. 渚濊禆绠$悊
寮€鍙戣€呭彲浠ュ埄鐢∕aven鎴朑radle鏋勫缓宸ュ叿杞绘澗绠$悊椤圭洰渚濊禆銆係pring Boot鎻愪緵浜咢SpringBootTest鍜孈RunWith(SpringRunner.class)鐢ㄤ簬娴嬭瘯鏂规硶銆?/p>
3. 鑷姩閰嶇疆鍜屽惎鍔ㄧ被
@SpringBootApplication鍏冩敞瑙i泦涓簡涓変釜娉ㄨВ锛欯SpringBootConfiguration銆丂EnableAutoConfiguration鍜孈ComponentScan銆傝繖涓変釜娉ㄨВ鍏卞悓纭繚搴旂敤鐨勮嚜鍔ㄩ厤缃€佽嚜鍔ㄦ娴嬪拰杩愯鏃剁粍浠剁殑鎵弿銆傝繖鎰忓懗鐫€寮€鍙戣€呭彧闇€鍏虫敞涓氬姟閫昏緫锛岃€屾棤闇€杩囧鍏虫敞閰嶇疆鍜屼緷璧栫鐞嗐€?/p>
4. 璺敱涓庢帶鍒跺櫒
鍦ㄧ紪绋嬬殑涓栫晫閲岋紝鎴戜滑鎬绘槸杩芥眰绠€娲佷笌楂樻晥銆備粖澶╋紝鎴戜滑灏嗛€氳繃Spring Boot妗嗘灦鏉ュ垱寤轰竴浜涘熀纭€鐨勮闂偣锛屽畠浠皢鍝嶅簲HTTP GET璇锋眰骞惰繑鍥炵浉搴旂殑瑙嗗浘鎴栨暟鎹€?/p>
涓€銆佸垱寤虹畝鍗曠殑Spring Boot鎺у埗鍣ㄥ鐞咹TTP GET璇锋眰
璁炬兂鎴戜滑鏈変竴涓畝鍗曠殑Spring Boot椤圭洰锛屽叾涓寘鍚竴涓猔HelloController`绫汇€傝繖涓被浣跨敤Spring鐨勬敞瑙f潵鍒涘缓涓€涓闂偣锛屽搷搴旀墍鏈塇TTP GET璇锋眰鍒版牴鍦板潃鈥?鈥濓紝骞惰繑鍥炲悕涓衡€渋ndex鈥濈殑瑙嗗浘椤甸潰銆備唬鐮佺ず渚嬪涓嬶細
```java
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("/")
public String index() {
return "index";
}
}
```
鍦ㄨ繖涓熀纭€涓婏紝鎴戜滑鍙互杩涗竴姝ユ帰璁ㄥ浣曚娇鐢⊿pring Boot鏋勫缓鏇村鏉傜殑RESTful API銆?/p>
浜屻€佷娇鐢⊿pring MVC鍒涘缓REST API
Spring Boot鍒╃敤Spring MVC妗嗘灦鏉ユ瀯寤篟ESTful API銆備笅闈㈡槸涓€涓畝鍗曠殑渚嬪瓙锛屾紨绀轰簡濡備綍浣跨敤娉ㄨВ鍒涘缓涓€涓猂EST绔偣锛?/p>
```java
package com.example.demo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class ApiController {
@GetMapping("/users")
public ResponseEntity<? > fetchUsers() {
// 杩欓噷妯℃嫙鑾峰彇鐢ㄦ埛鏁版嵁鐨勯€昏緫
return ResponseEntity.ok().body("User data");
}
}
```
涓夈€佷娇鐢⊿pring WebFlux鍒涘缓鍝嶅簲寮廟ESTful API
闄や簡Spring MVC锛孲pring 5杩樺紩鍏ヤ簡Spring WebFlux锛屼竴涓熀浜嶧lux鍜孧ono妯″瀷鐨勬柊鐨刉eb妗嗘灦锛屾彁渚涘紓姝ラ潪闃诲鐨凙PI銆備笅闈㈡槸涓€涓娇鐢⊿pring WebFlux鍒涘缓RESTful API鐨勭ず渚嬶細
```java
package com.example.demo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
@RestController
@RequestMapping("/api")
public class WebFluxController {
@GetMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE)
public Flux getUsers() {
return Flux.just("User 1", "User 2", "User 3");
}
}
```
鍥涖€佷笌鏁版嵁搴撻泦鎴?馃敆馃捇鉁? Spring Boot鏀寔鍚勭鏁版嵁搴撴妧鏈紝濡侸PA鍜孧yBatis绛夈€備綘鍙互杞绘澗鍦板皢鍏朵笌浣犵殑椤圭洰闆嗘垚璧锋潵锛屼粠鑰屽疄鐜版暟鎹殑鎸佷箙鍖栧瓨鍌ㄥ拰妫€绱㈠姛鑳姐€傚悗缁殑鏂囩珷灏嗕細娣卞叆鎺㈣杩欎釜涓婚銆備互涓嬫槸濡備綍浣跨敤JPA锛圝ava Persistence API锛変笌鏁版嵁搴撻泦鎴愶紝鍚屾椂娣诲姞鐢熷姩鎻忚堪鍜屼赴瀵屾枃浣撶殑绠€鍗曠ず渚嬶細
涓€銆佹坊鍔犱緷璧?/h3>
浣犻渶瑕佸湪椤圭洰鐨勬瀯寤洪厤缃枃浠讹紙濡?pom.xml 鎴?build.gradle锛変腑娣诲姞JPA鍜孲pring Boot鐨勭浉鍏充緷璧栥€傝繖鏍凤紝浣犵殑搴旂敤鎵嶈兘浣跨敤JPA杩涜鏁版嵁鎸佷箙鍖栨搷浣溿€傚浜嶮aven椤圭洰锛屾坊鍔犲涓嬩緷璧栵細
```xml
org.springframework.boot
spring-boot-starter-data-jpa
```
瀵逛簬Gradle椤圭洰锛屽垯鍦?build.gradle 鏂囦欢涓坊鍔狅細
```gradle
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
```
浜屻€佹暟鎹簱閰嶇疆
鎺ヤ笅鏉ワ紝鍦?application.properties 鏂囦欢涓厤缃綘鐨勬暟鎹簱杩炴帴淇℃伅銆備緥濡傦紝濡傛灉浣犱娇鐢ㄧ殑鏄疢ySQL鏁版嵁搴擄紝浣犻渶瑕佹彁渚涙暟鎹簱鐨刄RL銆佺敤鎴峰悕銆佸瘑鐮佺瓑淇℃伅銆傝缃甋pring JPA鐨凥ibernate鑷姩DDL鐢熸垚绛栫暐涓烘洿鏂帮紙update锛夈€傝繖鏍凤紝褰撳疄浣撶被鍙戠敓鍙樺寲鏃讹紝Hibernate浼氳嚜鍔ㄦ洿鏂版暟鎹簱琛ㄧ粨鏋勩€傞厤缃涓嬶細
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
```
涓夈€佸疄浣撶被瀹氫箟
瀹氫箟浣犵殑瀹炰綋绫诲苟浣跨敤JPA娉ㄨВ鏍囪瘑瀹冧滑銆備緥濡傦紝涓€涓畝鍗曠殑鐢ㄦ埛瀹炰綋绫诲彲鑳藉寘鍚獻D銆佸鍚嶇瓑灞炴€с€備娇鐢?@Entity 娉ㄨВ鏍囪瘑杩欐槸涓€涓疄浣撶被锛孈Id 鍜?@GeneratedValue 娉ㄨВ鏍囪瘑涓婚敭灞炴€с€?/p>
```java
package com.example.demo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id; // 涓婚敭ID瀛楁锛岃嚜鍔ㄥ闀跨瓥鐣ョ敓鎴怚D鍊笺€傝嫢浣跨敤鍏朵粬绛栫暐鍒欓渶瑕侀澶栭厤缃€? private String name; // 鐢ㄦ埛鍚嶇О瀛楁銆傚厑璁镐负null鎴栫┖瀛楃涓茬瓑璁剧疆鏍规嵁瀹為檯闇€姹傝皟鏁淬€? // 鍖呭惈鏋勯€犲嚱鏁般€乬etter鍜宻etter鏂规硶鐢ㄤ簬绠$悊瀹炰綋瀵硅薄鐨勫睘鎬х姸鎬併€?鐪佺暐鍏朵粬缁嗚妭... } } } 鎺ョ潃瀹氫箟瀹炰綋绫荤殑璁块棶鎺ュ彛鍗矰AO灞傚疄鐜帮紙鏁版嵁璁块棶瀵硅薄锛夈€傝繖閮ㄥ垎浠g爜璐熻矗鏁版嵁鐨勫鍒犳敼鏌ユ搷浣溿€傚湪Spring Boot涓紝鍙互閫氳繃瀹炵幇Repository鎺ュ彛鏉ョ畝鍖栧紑鍙戣繃绋嬨€傚垱寤轰竴涓帴鍙f潵灏佽鏁版嵁璁块棶閫昏緫锛?package com.example.demo; import org.springframework.data.repository.CrudRepository; public interface UserRepository extends CrudRepository {} 娉ㄦ剰杩欎釜鎺ュ彛缁ф壙浜咰rudRepository鎺ュ彛琛ㄦ槑鍏舵彁渚涘熀纭€鐨凜RUD鎿嶄綔鍔熻兘鍖呮嫭澧炲垹鏀规煡鎿嶄綔濡俧ind,save绛夎繖浜涙柟娉曞凡缁忓疄鐜颁簡鍩烘湰閫昏緫鍙渶瑕佹牴鎹疄闄呴渶姹傚疄鐜颁竴浜涢澶栨搷浣滃嵆鍙€傚洓銆佹湇鍔″眰瀹炵幇 鍒涘缓鏈嶅姟灞傜被瀹炵幇鍏蜂綋鐨勪笟鍔¢€昏緫渚嬪鏍规嵁鐢ㄦ埛ID鑾峰彇鐢ㄦ埛淇℃伅鐨勬柟娉曪細 package com.example.demo; import com.example.demo.User; import com.example.demo.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { private final UserRepository userRepository; @Autowired public UserService(UserRepository userRepository) { this userRepository = userRepository; } public User getUserById(Long id) { return userRepository findById(id).orElse(null); } } 浜斻€佸紓甯告崟鑾蜂笌鑷畾涔夊紓甯哥被 鍦⊿pring Boot搴旂敤涓紓甯稿鐞嗘槸鏍稿績鏈哄埗涔嬩竴鍙互閫氳繃浣跨敤@ControllerAdvice娉ㄨВ鏉ュ疄鐜板叏灞€寮傚父澶勭悊鑷畾涔夊紓甯哥被閫氳繃缁ф壙RuntimeException鎴栨洿鍏蜂綋鐨勫紓甯哥被鏉ュ畾涔変笟鍔¢€昏緫寮傚父鍦ㄥ叏灞€寮傚父澶勭悊绫讳腑鎹曡幏杩欎簺鑷畾涔夊紓甯稿苟杩斿洖缁欏墠绔弸濂界殑閿欒淇℃伅鍚屾椂鏀寔璁板綍鏃ュ織绛夊鐞嗘満鍒跺疄鐜扮郴缁熺ǔ瀹氭€т繚闅滀笉鍐嶈禈杩扮粏鑺傚彲鏌ラ槄Spring Boot瀹樻柟鏂囨。杩涗竴姝ヤ簡瑙e紓甯稿鐞嗘満鍒剁殑浣跨敤鏂瑰紡鍙婂叾閲嶈鎬ч€氳繃鏈枃瀵瑰浣曢泦鎴怞PA涓庢暟鎹簱鍋氫簡绠€鍗曚粙缁嶅苟瀵规瘡涓幆鑺傚仛浜嗙敓鍔ㄦ弿杩板笇鏈涜兘瀵硅鑰呮湁鎵€甯姪鍦ㄥ疄闄呭紑鍙戜腑杩橀渶瑕佹牴鎹叿浣撻渶姹傝繘琛岀粏鑺傝皟鏁村拰浼樺寲浠ヨ揪鍒版渶浣虫晥鏋滃悓鏃惰娉ㄦ剰浠g爜璐ㄩ噺鍜屽畨鍏ㄦ€ч棶棰樻槸闈炲父閲嶈鐨勮€冭檻鍥犵礌涓嶅蹇借灏ゅ叾鏄湪娑夊強鏁版嵁搴撴搷浣滅瓑鏂归潰鏇撮渶瑕佹敞鎰忓畨鍏ㄦ€х瓑闂銆?涓婅堪鏂囨湰鍖呭惈浜嗕綘鎵€鎻愪緵鐨勪唬鐮佹瀵瑰簲鍐呭鐨勮В閲婅鏄庝互鍙婁竴浜涙墿灞曠煡璇嗗拰寤鸿璇锋煡闃呯浉鍏虫枃妗d互浜嗚В鏇村缁嗚妭淇℃伅骞剁伒娲诲簲鐢ㄥ埌瀹為檯寮€鍙戜腑浠ユ彁楂樺紑鍙戞晥鐜囧拰浠g爜璐ㄩ噺銆傚畾涔夊叏灞€寮傚父澶勭悊鍣ㄧ被
鍦?`com.example.demo` 鍖呬腑锛屾垜浠湁涓€涓簿蹇冭璁$殑鍏ㄥ眬寮傚父澶勭悊鍣ㄢ€斺€擿GlobalExceptionHandler`銆傚畠鏄熀浜?Spring Framework 鐨勫己澶у姛鑳芥瀯寤虹殑锛岃兘澶熻交鏉惧鐞嗗簲鐢ㄧ▼搴忎腑鐨勯潪娉曞弬鏁板紓甯搞€傚綋閬囧埌 `IllegalArgumentException` 鏃讹紝瀹冨皢杩呴€熷搷搴斿苟杩斿洖涓€涓甫鏈?HTTP 鐘舵€佺爜 `BAD_REQUEST` 鐨勫搷搴斿疄浣擄紝鍚屾椂甯︽湁閿欒娑堟伅 "Invalid input"銆傝繖绉嶈璁$‘淇濅簡鐢ㄦ埛浼氬緱鍒版竻鏅扮殑鍙嶉锛岃€屼笉鏄亣鍒伴毦浠ョ悊瑙g殑閿欒銆?/p>
寰湇鍔℃灦鏋勮璁″師鍒欒В璇?/p>
寰湇鍔℃灦鏋勬槸杞欢鏋舵瀯鐨勪竴绉嶈秼鍔匡紝瀹冩妸涓€涓ぇ鍨嬪簲鐢ㄦ媶鍒嗘垚澶氫釜灏忔湇鍔★紝姣忎釜鏈嶅姟涓撴敞浜庡畬鎴愪竴涓壒瀹氱殑鍔熻兘銆傝繖绉嶈璁℃湁鍏剁嫭鐗圭殑鍘熷垯鍜屽疄璺垫柟娉曘€備互涓嬫槸鍏充簬寰湇鍔℃灦鏋勭殑鍑犱釜鏍稿績璁捐鍘熷垯锛?/p>
鐙珛閮ㄧ讲锛氭瘡涓井鏈嶅姟閮芥槸鐙珛鐨勫疄浣擄紝鎷ユ湁鑷繁鐨勭敓鍛藉懆鏈熴€傝繖鎰忓懗鐫€瀹冧滑鍙互鍗曠嫭閮ㄧ讲銆佹洿鏂板拰鎵╁睍锛屾棤闇€瀵规暣涓簲鐢ㄨ繘琛屽ぇ瑙勬ā鐨勬敼鍔ㄦ垨閮ㄧ讲銆?/p>
鏈嶅姟闂撮€氫俊锛氬井鏈嶅姟涔嬮棿閫氳繃杞婚噺绾х殑閫氫俊鍗忚锛堝 HTTP/REST锛夎繘琛屼氦浜掋€傝繖绉嶈璁$‘淇濅簡鏈嶅姟涔嬮棿鐨勮В鑰︼紝浣垮緱姣忎釜鏈嶅姟鑳藉鐙珛鍙戝睍鍜屽彉鍖栵紝鑰屼笉褰卞搷鍏朵粬鏈嶅姟銆?/p>
API缃戝叧锛欰PI缃戝叧鏄井鏈嶅姟鏋舵瀯涓殑鍏抽敭缁勪欢锛屽畠浣滀负瀹㈡埛绔拰寰湇鍔′箣闂寸殑妗ユ锛屾彁渚涗簡涓€涓粺涓€鐨勮闂偣銆傝繖绠€鍖栦簡瀹㈡埛绔殑浜や簰锛屽悓鏃跺寮轰簡瀹夊叏鎬э紝鍥犱负鎵€鏈夎繘鍏ョ殑璇锋眰閮介渶瑕侀€氳繃缃戝叧杩涜楠岃瘉鍜屾巿鏉冦€?/p>
鐘舵€佹棤鐘舵€侊細涓轰簡鎻愰珮鍙墿灞曟€у拰瀹归敊鑳藉姏锛屽井鏈嶅姟搴斾繚鎸佹棤鐘舵€併€傝繖鎰忓懗鐫€瀹冧滑涓嶅簲渚濊禆鍐呴儴鐨勪細璇濈姸鎬佹垨涓婁笅鏂囦俊鎭紝鑰屾槸渚濊禆浜庡閮ㄥ瓨鍌紙濡傛暟鎹簱锛夊拰閰嶇疆淇℃伅鏉ユ墽琛屾搷浣溿€?/p>
鐙珛鐨勬暟鎹簱锛氭瘡涓井鏈嶅姟閮藉簲鎷ユ湁鑷繁鐨勬暟鎹簱瀹炰緥銆傝繖鏍峰仛鏄负浜嗛伩鍏嶆湇鍔¢棿鐨勪緷璧栧拰鐩镐簰褰卞搷銆傚綋鏌愪釜鏈嶅姟闇€瑕佹洿鏀规垨鍗囩骇鏃讹紝瀹冧笉浼氬奖鍝嶅埌鍏朵粬鏈嶅姟鐨勬暟鎹簱缁撴瀯鎴栨暟鎹畬鏁存€с€?/p>
瀹归敊鏈哄埗锛氬湪寰湇鍔℃灦鏋勪腑锛屾垜浠湡鏈涙煇浜涙湇鍔″嚭鐜版晠闅滄槸甯告€佽€岄潪渚嬪銆傝璁℃椂闇€瑕佽€冭檻鍒板閿欐満鍒讹紝纭繚鏈嶅姟鑳藉浼橀泤鍦板鐞嗘晠闅滃苟鑷姩鎭㈠銆傝繖閫氬父娑夊強鍒拌礋杞藉潎琛°€佺啍鏂櫒妯″紡鍜岄噸璇曟満鍒剁瓑鎶€鏈墜娈电殑搴旂敤銆?.1 瀹炴垬妗堜緥鍒嗘瀽锛氭瀯寤轰竴涓畬鏁寸殑Spring Boot浼佷笟绾у簲鐢?/p>
椤圭洰缁撴瀯姒傝
鎴戜滑棣栧厛闇€瑕佹瀯寤洪」鐩殑鐩綍缁撴瀯銆備互涓嬫槸鎺ㄨ崘鐨勯」鐩粨鏋勶細
```lua
my-app/
|-- src/
|-- main/
|-- java/
|-- com.example.demo/
|-- controllers/
|-- services/
|-- repositories/
|-- utils/
|-- resources/
|-- static/
|-- templates/
|-- application.properties
|-- mvnw
|-- .gitignore
```
鏈嶅姟瀹炵幇璇﹁В
User Microservice锛堢敤鎴峰井鏈嶅姟锛夌殑瀹炵幇涓庣敤鎴锋暟鎹鐞?/p>
鎺у埗鍣ㄥ眰锛圕ontroller Layer锛夊疄鐜帮細
鍦╜com.example.demo`鍖呬腑锛屾垜浠疄鐜癠serController绫伙紝璇ョ被璐熻矗澶勭悊涓庣敤鎴锋暟鎹浉鍏崇殑HTTP璇锋眰銆備互涓嬫槸UserController鐨勭畝鏄撳疄鐜帮細
```java
package com.example.demo;
import com.example.demo.User; // 寮曞叆User绫?/p>
import com.example.demo.UserService; // 寮曞叆UserService鏈嶅姟绫?/p>
import org.springframework.beans.factory.annotation.Autowired; // Spring娉ㄥ叆娉ㄨВ
import org.springframework.web.bind.annotation.; // 寮曞叆Spring Web娉ㄨВ搴?/p>
import java.util.List; // 寮曞叆List闆嗗悎绫?/p>
@RestController // 澹版槑杩欐槸涓€涓猂est鎺у埗鍣?/p>
@RequestMapping("/api/users") // 瀹氫箟鍩虹URL璺緞涓?api/users
public class UserController { // User鎺у埗鍣ㄧ被瀹氫箟寮€濮?/p>
private final UserService userService; // 瀹氫箟UserService瀵硅薄锛岀敤浜庡鐞嗙敤鎴锋湇鍔¢€昏緫
@Autowired // 閫氳繃Spring鐨勮嚜鍔ㄨ閰嶅姛鑳芥敞鍏serService瀵硅薄瀹炰緥
public UserController(UserService userService) { // 鏋勯€犲嚱鏁板畾涔夛紝娉ㄥ叆userService瀵硅薄瀹炰緥鍒板綋鍓嶆帶鍒跺櫒瀹炰緥涓?/p>
this.userService = userService; // 瀹屾垚娉ㄥ叆鎿嶄綔鍚庯紝灏唘serService璧嬪€肩粰褰撳墠瀹炰緥鐨剈serService鍙橀噺銆傚悗缁搷浣滃彲閫氳繃璇ュ彉閲忚皟鐢╱serService鏂规硶瀹炵幇鍔熻兘銆? } // 杩欓噷閫氳繃鏋勯€犳柟娉曟敞鍏serService瀵硅薄瀹炰緥鍒板綋鍓嶆帶鍒跺櫒涓紝鐢ㄤ簬澶勭悊鐢ㄦ埛鐩稿叧鏈嶅姟閫昏緫銆?鎺ヤ笅鏉ユ垜浠彲浠ュ畾涔夊叿浣撶殑HTTP璇锋眰澶勭悊鏂规硶銆傝繖閲屽畾涔変簡涓€涓狦ET璇锋眰澶勭悊鏂规硶getUsers()锛岀敤浜庤幏鍙栨墍鏈夌敤鎴锋暟鎹€傝鏂规硶閫氳繃璋冪敤UserService涓殑getAllUsers()鏂规硶鑾峰彇鎵€鏈夌敤鎴锋暟鎹苟杩斿洖缁欏鎴风銆傝繖鏍锋垜浠氨瀹炵幇浜嗙敤鎴锋暟鎹殑鑾峰彇鍔熻兘銆傚叾浠栫敤鎴风浉鍏虫搷浣滀篃鍙互閫氳繃绫讳技鐨勬柟寮忓疄鐜般€傚湪鍚庣画鐨勪唬鐮佷腑锛屾垜浠彲浠ョ户缁坊鍔犲叾浠栨帶鍒跺櫒绫诲拰鏂规硶鏉ュ疄鐜版洿澶氱殑鍔熻兘銆備緥濡傦紝鎴戜滑鍙互娣诲姞鍏朵粬RESTful API鎺ュ彛鏉ュ疄鐜扮敤鎴锋敞鍐屻€佺櫥褰曘€佷慨鏀瑰瘑鐮佺瓑鍔熻兘銆傞€氳繃杩欎簺鎺ュ彛鎴戜滑鍙互瀹炵幇瀵圭敤鎴风殑澧炲垹鏀规煡绛夋搷浣滀互鍙婄郴缁熺殑鍩烘湰涓氬姟閫昏緫鍔熻兘銆傛渶缁堟垜浠畬鎴愪簡Spring Boot浼佷笟绾у簲鐢ㄧ殑寮€鍙戙€備笅闈㈡槸UserController绫讳腑getUsers鏂规硶鐨勭畝鍗曞疄鐜帮細 鎺ヤ笅鏉ユ垜浠潵鐪嬫湇鍔″眰鐨勫疄鐜般€?鏈嶅姟灞傦紙Service Layer锛夊疄鐜帮細 鍦╟om.example.demo鍖呬腑锛屾垜浠畾涔塙serService绫绘潵灏佽涓庣敤鎴锋暟鎹浉鍏崇殑涓氬姟閫昏緫銆備互涓嬫槸UserService绫荤殑绠€鍗曞疄鐜帮細 鍦║serService绫讳腑锛屾垜浠畾涔変簡getAllUsers鏂规硶鏉ヨ幏鍙栨墍鏈夌敤鎴锋暟鎹€傝鏂规硶閫氳繃璋冪敤UserRepository涓殑findAll鏂规硶鏉ヨ幏鍙栨墍鏈夌敤鎴锋暟鎹苟杩斿洖缁欒皟鐢ㄨ€咃紙閫氬父鏄帶鍒跺櫒灞傦級銆傝繖鏍锋垜浠氨瀹炵幇浜嗘湇鍔″眰鐨勫姛鑳姐€傚叾浠栦笌鐢ㄦ埛鐩稿叧鐨勪笟鍔¢€昏緫涔熷彲浠ラ€氳繃绫讳技鐨勬柟寮忓疄鐜般€傞€氳繃鏈嶅姟灞傛垜浠彲浠ュ皢涓氬姟閫昏緫涓庢暟鎹簱鎿嶄綔鍒嗙锛屼娇寰椾唬鐮佹洿鍔犳竻鏅板拰鏄撲簬缁存姢銆備笅闈㈡槸UserService绫讳腑getAllUsers鏂规硶鐨勭畝鍗曞疄鐜帮細 鐜板湪鎴戜滑宸茬粡瀹屾垚浜嗘帶鍒跺櫒灞傚拰鏈嶅姟灞傜殑瀹炵幇鎺ヤ笅鏉ユ垜浠潵浠嬬粛API缃戝叧鐨勫疄鐜般€?API缃戝叧鐨勫疄鐜帮細 鍦ㄤ紒涓氱骇搴旂敤涓瑼PI缃戝叧鎵紨鐫€閲嶈鐨勮鑹插畠璐熻矗缁熶竴绠$悊鍜岃皟搴PI鎺ュ彛鎻愪緵缁熶竴鐨勮闂叆鍙e拰鏉冮檺鎺у埗绛夊姛鑳姐€傚湪Spring Boot搴旂敤涓垜浠彲浠ラ€氳繃绠€鍗曠殑RestController鏉ュ疄鐜颁竴涓畝鍗曠殑API缃戝叧銆傚湪com.example.demo鍖呬腑鎴戜滑瀹氫箟GatewayController绫绘潵瀹炵幇API缃戝叧鐨勫姛鑳姐€傚湪GatewayController绫讳腑鎴戜滑瀹氫箟浜嗕竴涓猤etUsers鏂规硶鏉ヨ幏鍙栫敤鎴锋暟鎹鏂规硶閫氳繃璋冪敤RestTemplate鏉ヨ闂敤鎴峰井鏈嶅姟骞惰幏鍙栨暟鎹劧鍚庤繑鍥炵粰璋冪敤鑰咃紙閫氬父鏄鎴风锛夈€傝繖鏍锋垜浠氨瀹炵幇浜咥PI缃戝叧鐨勫姛鑳介€氳繃API缃戝叧鎴戜滑鍙互鏂逛究鍦扮鐞嗗拰璋冨害API鎺ュ彛鎻愪緵缁熶竴鐨勮闂叆鍙e拰鏉冮檺鎺у埗绛夊姛鑳戒粠鑰屾彁楂樼郴缁熺殑瀹夊叏鎬у拰鍙淮鎶ゆ€т笅闈㈡槸GatewayController绫讳腑getUsers鏂规硶鐨勭畝鍗曞疄鐜帮細 鐜板湪鎴戜滑宸茬粡瀹屾垚浜嗘湇鍔″眰鍜孉PI缃戝叧鐨勫疄鐜版帴涓嬫潵鎴戜滑鏉ヤ粙缁嶉儴缃插疄璺电殑鐩稿叧鐭ヨ瘑 閮ㄧ讲瀹炶返锛?鍦ㄤ紒涓氱骇搴旂敤涓垜浠€氬父浼氬皢搴旂敤瀹瑰櫒鍖栧苟閮ㄧ讲鍒颁簯骞冲彴涓婁互瀹炵幇蹇€熼儴缃插拰鎵╁睍绛夊姛鑳姐€傚湪Spring Boot搴旂敤涓垜浠彲浠ヤ娇鐢―ocker鏉ュ鍣ㄥ寲搴旂敤骞朵娇鐢ㄤ簯骞冲彴锛堝AWS鎴朅zure锛夌殑瀹瑰櫒鏈嶅姟杩涜閮ㄧ讲銆備笅闈㈡槸浣跨敤Docker閮ㄧ讲Spring Boot搴旂敤鐨勭畝鍗曟楠わ細 棣栧厛鎴戜滑闇€瑕佹瀯寤篋ocker闀滃儚浣跨敤docker build鍛戒护鏉ユ瀯寤洪暅鍍忓苟灏嗛暅鍍忓懡鍚嶄负my-app鐒跺悗浣跨敤docker run鍛戒护鏉ヨ繍琛屽鍣ㄥ苟灏嗗鍣ㄧ殑绔彛鏄犲皠鍒颁富鏈虹殑绔彛涓婅繖鏍锋垜浠氨鍙互閫氳繃涓绘満鐨勭鍙f潵璁块棶瀹瑰櫒鐨勫簲鐢ㄤ簡鍏蜂綋鍛戒护濡備笅锛?docker build -t my-app . docker run -p 8080:8080 my-app 鍙﹀鎴戜滑杩樺彲浠ュ皢搴旂敤閮ㄧ讲鍒颁簯骞冲彴锛堝AWS鎴朅zure锛夌殑瀹瑰櫒鏈嶅姟涓婁互瀹炵幇鏇撮珮绾х殑鍔熻兘濡傝嚜鍔ㄤ几缂┿€佽礋杞藉潎琛$瓑閫氳繃浜戝钩鍙版垜浠彲浠ユ洿鏂逛究鍦扮鐞嗗簲鐢ㄥ苟鎻愰珮搴旂敤鐨勫彲闈犳€у拰鎬ц兘 閫氳繃浠ヤ笂姝ラ鎴戜滑瀹屾垚浜哠pring Boot浼佷笟绾у簲鐢ㄧ殑寮€鍙戝寘鎷」鐩粨鏋勭殑鏋勫缓鏈嶅姟灞傜殑瀹炵幇API缃戝叧鐨勫疄鐜颁互鍙婇儴缃插疄璺电殑浠嬬粛鍦ㄥ疄闄呭紑鍙戜腑鎴戜滑杩橀渶瑕佽€冭檻鏇村鐨勭粏鑺傚拰鍔熻兘渚嬪鏁版嵁搴撶殑璁捐銆佸畨鍏ㄦ€х殑鑰冭檻銆佹棩蹇楃殑绠$悊绛夌瓑杩欎簺閮芥槸鏋勫缓浼佷笟绾у簲鐢ㄦ椂闇€瑕侀噸鐐瑰叧娉ㄧ殑鍐呭閫氳繃涓嶆柇鐨勫涔犲拰瀹炶返鎴戜滑鍙互閫愭鎻愰珮鑷繁鍦ㄦ瀯寤轰紒涓氱骇搴旂敤鏂归潰鐨勮兘鍔涗粠鑰屼负浼佷笟鐨勫彂灞曞仛鍑烘洿澶х殑璐$尞甯屾湜浠ヤ笂鍐呭鑳藉甯姪浣犳洿濂藉湴鐞嗚ВSpring Boot浼佷笟绾у簲鐢ㄧ殑鏋勫缓杩囩▼骞朵负浣犳彁渚涗竴浜涘弬鑰冨拰鍚ず璋㈣阿锛佺幇鍦ㄦ垜浠凡缁忓畬鎴愪簡鏁翠釜Spring Boot浼佷笟绾у簲鐢ㄧ殑鏋勫缓杩囩▼鍖呮嫭椤圭洰缁撴瀯鐨勬瀯寤烘湇鍔″眰鐨勫疄鐜癆PI缃戝叧鐨勫疄鐜颁互鍙婁娇鐢―ocker杩涜搴旂敤鐨勫鍣ㄥ寲閮ㄧ讲鍦ㄨ繖涓繃绋嬩腑鎴戜滑娑夊強鍒颁簡璁稿鍏抽敭鎶€鏈拰鐭ヨ瘑鐐瑰Spring Boot妗嗘灦鐨勪娇鐢≧ESTful API鐨勮璁″拰瀹炵幇Docker瀹瑰櫒鐨勪娇鐢ㄥ拰浜戝钩鍙伴儴缃茬瓑杩欎簺閮芥槸鏋勫缓浼佷笟绾у簲鐢ㄦ椂闇€瑕佹帉鎻$殑鏍稿績鎶€鏈€氳繃涓嶆柇鐨勫涔犲拰瀹炶返鎴戜滑鍙互閫愭笎鎺屾彙杩欎簺鎶€鏈苟搴旂敤鍒板疄闄呯殑椤圭洰涓幓浠庤€屾彁楂橀」鐩殑寮€鍙戞晥鐜囧拰璐ㄩ噺涓轰紒涓氱殑鍙戝睍鍋氬嚭鏇村ぇ鐨勮础鐚笇鏈涗互涓婂唴瀹硅兘澶熷浣犳湁鎵€甯姪璋㈣阿锛乣``鎬ц兘浼樺寲绛栫暐锛氭墦閫犱紒涓氱骇搴旂敤鐨勫埄鍣?/p>
鍦ㄦ瀯寤轰紒涓氱骇搴旂敤鐨勮繃绋嬩腑锛屾€ц兘浼樺寲鏄笉鍙垨缂虹殑涓€鐜€備负浜嗘彁鍗囧簲鐢ㄧ殑鍝嶅簲閫熷害銆佺ǔ瀹氭€у拰鍙墿灞曟€э紝鎴戜滑鍙互閲囧彇涓€绯诲垪鐨勭瓥鐣ュ拰鎺柦銆?/p>
鏁版嵁搴撲紭鍖栵細鏁版嵁搴撴槸搴旂敤鐨勬牳蹇冪粍鎴愰儴鍒嗭紝瀵瑰叾杩涜浼樺寲鑷冲叧閲嶈銆傛垜浠彲浠ヤ娇鐢ㄧ储寮曟潵鎻愰珮鏌ヨ閫熷害锛屽疄鏂界紦瀛樼瓥鐣ヤ互鍑忓皯鏁版嵁搴撹闂鏁帮紝骞剁啛缁冩帉鎻″悇绉嶆暟鎹簱浼樺寲鎶€鏈€?/p>
寮傛澶勭悊锛氫负浜嗘彁鍗囧簲鐢ㄧ殑鍝嶅簲鎬ц兘锛屾垜浠彲浠ュ埄鐢ㄦ秷鎭槦鍒楄繘琛屽紓姝ヤ换鍔″鐞嗐€傚€熷姪濡俁abbitMQ杩欐牱鐨勬秷鎭唬鐞嗚蒋浠讹紝鎴戜滑鍙互杞绘澗鍦板皢鑰楁椂鐨勪换鍔$Щ鍑轰富绾跨▼锛屼粠鑰屾樉钁楁彁楂樺簲鐢ㄧ殑鍝嶅簲閫熷害銆?/p>
璐熻浇鍧囪 锛氫负浜嗙‘淇濆簲鐢ㄥ湪楂樺苟鍙戜笅鐨勭ǔ瀹氭€э紝鎴戜滑闇€瑕侀儴缃茶礋杞藉潎琛″櫒銆侼ginx鎴朒AProxy绛夎礋杞藉潎琛″櫒鍙互鏈夋晥鍦板垎閰嶈姹傦紝纭繚鏈嶅姟鍣ㄨ祫婧愮殑鍚堢悊鍒╃敤锛屼粠鑰屾彁鍗囧簲鐢ㄧ殑鎬ц兘鍜屽彲鎵╁睍鎬с€?/p>
鍐呭鍒嗗彂缃戠粶锛圕DN锛夛細涓轰簡鎻愰珮鐢ㄦ埛鐨勮闂€熷害锛屾垜浠彲浠ュ€熷姪CDN鎶€鏈€侰DN鑳藉缂撳瓨鎴戜滑鐨勯潤鎬佽祫婧愶紝骞堕€氳繃鍒嗗竷鍦ㄥ叏鐞冪殑鑺傜偣锛屽皢鍐呭蹇€熶紶杈撶粰鐢ㄦ埛锛屼粠鑰屽ぇ澶ф敼鍠勭敤鎴风殑浣撻獙銆?/p>
缂撳瓨绛栫暐锛氬悎鐞嗙殑缂撳瓨绛栫暐鑳藉鏄捐憲鍑忓皯鏁版嵁搴撶殑璁块棶娆℃暟銆傛垜浠彲浠ュ埄鐢ㄦ祻瑙堝櫒缂撳瓨銆佹湇鍔$缂撳瓨绛夋妧鏈紝灏嗛绻佽闂殑鏁版嵁淇濆瓨鍦ㄧ紦瀛樹腑锛屼粠鑰岄伩鍏嶄笉蹇呰鐨勬暟鎹簱鏌ヨ锛屾彁鍗囧簲鐢ㄧ殑鎬ц兘銆?/p>
閫氳繃瀹炶返浠ヤ笂绛栫暐锛屼綘灏嗚兘澶熸瀯寤哄嚭涓€涓姛鑳藉畬澶囥€佹槗浜庢墿灞曞拰绠$悊鐨凷pring Boot浼佷笟绾у簲鐢ㄣ€傝繖鍙槸涓€涓紑濮嬶紝鎸佺画浼樺寲鍜屾渶浣冲疄璺电殑搴旂敤鎵嶆槸鏋勫缓楂樻€ц兘銆佺ǔ瀹氱郴缁熺殑鍏抽敭銆傛垜浠渶瑕佹椂鍒讳繚鎸佸鏂版妧鏈€佹柊鏂规硶鐨勫叧娉紝涓嶆柇鍦板搴旂敤杩涜浼樺寲鍜屽崌绾э紝浠ョ‘淇濆簲鐢ㄥ缁堜繚鎸佸湪琛屼笟鐨勫墠娌裤€?/p> |