---
娣卞叆浜嗚ВSpringBoot妗嗘灦鍦ㄥ嵆鏃堕€氳寮€鍙戜腑鐨勫簲鐢?/p>
鏈枃鏃ㄥ湪寮曢寮€鍙戣€呰笍涓婂熀浜嶴pringBoot妗嗘灦鐨勫嵆鏃堕€氳绯荤粺寮€鍙戜箣鏃呫€傛垜浠皢浠庨」鐩惎鍔ㄥ紑濮嬶紝閫愭瀹炵幇涓€瀵逛竴銆佸瀵瑰娑堟伅鍙戦€佹帴鏀剁殑鏍稿績鍔熻兘锛屾兜鐩栫敤鎴疯璇併€佹秷鎭寔涔呭寲涓庡瓨鍌ㄣ€佸畨鍏ㄤ笌鎬ц兘浼樺寲绛夊叧閿幆鑺傦紝涓哄紑鍙戣€呮彁渚涜灏界殑寮€鍙戞寚鍗椼€傝鎴戜滑涓€鍚屾瀯寤洪珮鏁堛€佸畨鍏ㄧ殑瀹炴椂閫氳浣撻獙鍚э紒
姒傝堪涓庣洰鏍囪瀹?/p>
鍗虫椂閫氳锛圛nstant Messaging, IM锛夐€氳繃浜掕仈缃戞垨鎵嬫満缃戠粶鎻愪緵娑堟伅浼犺緭鏈嶅姟锛屽寘鎷枃瀛椼€佽闊炽€佸浘鐗囥€佽棰戠瓑澶氭牱鍖栧唴瀹广€傚湪鏋勫缓鍗虫椂閫氳绯荤粺鏃讹紝鏍稿績缁勪欢濡傜敤鎴疯璇佷笌鎺堟潈銆佹秷鎭紶閫掋€佺姸鎬佺鐞嗕互鍙婃寔涔呭寲瀛樺偍绛夎嚦鍏抽噸瑕併€傝€孲pringBoot妗嗘灦鍦ㄥ嵆鏃堕€氳寮€鍙戜腑鐨勫簲鐢紝涓烘垜浠彁渚涗簡蹇€熸瀯寤哄拰閮ㄧ讲搴旂敤鐨勪究鍒┿€傚€熷姪Spring鎻愪緵鐨勪緷璧栵紝濡係pring Web銆丼pring Security鍜孲pring Data绛夛紝鎴戜滑鑳藉鏈夋晥鍦版瀯寤鸿璇佺郴缁熴€丠TTP鏈嶅姟鍜屾寔涔呭寲灞傘€?/p>
椤圭洰鍚姩涓庣幆澧冨噯澶?/p>
纭繚寮€鍙戠幆澧冩弧瓒充互涓嬫潯浠讹細瀹夎浜咼ava寮€鍙戝伐鍏峰寘锛圝DK锛夛紝浣跨敤IntelliJ IDEA鎴朎clipse绛塈DE锛岃缃ソ鏈嶅姟鍣ㄧ幆澧冿紙鏈湴鎴栦簯鏈嶅姟鍣級锛屽苟鍑嗗濂組aven鎴朑radle鏋勫缓宸ュ叿銆傛帴涓嬫潵锛屽垱寤轰竴涓柊鐨凷pringBoot椤圭洰锛屼娇鐢∕aven鎴朑radle鐨勬ā鏉垮揩閫熺敓鎴愬熀纭€鐨勯」鐩粨鏋勩€?/p>
浣跨敤Maven鍒涘缓骞跺垵濮嬪寲SpringBoot椤圭洰
1. 鐢熸垚鏂扮殑SpringBoot椤圭洰锛歚mvn archetype:generate -DgroupId=com.example -DartifactId=chat-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false`銆?/p>
2. 杩涘叆椤圭洰鐩綍锛歚cd chat-app`銆?/p>
娣诲姞蹇呰渚濊禆
鍦╬om.xml鏂囦欢涓坊鍔犱互涓嬩緷璧栦互鏀寔椤圭洰鍔熻兘锛歋pring Boot Starter Web鐢ㄤ簬Web寮€鍙戯紱Spring Security鐢ㄤ簬澧炲己搴旂敤瀹夊叏鎬э紱Redis鐢ㄤ簬娑堟伅闃熷垪鍜岀紦瀛樸€?/p>
```xml
spring-boot-starter-web
spring-boot-starter-security
spring-boot-starter-data-redis
```
椤圭洰鍒濆鍖栦笌杩愯
瀹屾垚渚濊禆閰嶇疆鍚庯紝鎵ц浠ヤ笅鍛戒护鍒濆鍖栧苟杩愯椤圭洰锛?/p>
1. 鎵撳寘椤圭洰锛歚mvn clean install`銆?/p>
2. 杩愯椤圭洰锛歚nohup java -jar target/chat-app-1.0-SNAPSHOT.jar > output.log 2>&1 &`銆?/p>
鍩虹鏋舵瀯鎼缓
鍦ㄩ厤缃暟鎹簮鏃讹紝鎴戜滑棣栧厛闇€瑕佽缃暟鎹簱鐨刄RL銆佺敤鎴峰悕銆佸瘑鐮佷互鍙婇┍鍔ㄧ被鍚嶃€傚浜庢垜浠殑鑱婂ぉ搴旂敤锛屾暟鎹簱閲囩敤鐨勬槸MySQL锛屽叾閰嶇疆淇℃伅濡備笅锛?/p>
```makefile
spring.datasource.url=jdbc:mysql://localhost:3306/chatdb?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
```
鎴戜滑鐨勫簲鐢ㄨ繕闆嗘垚浜哛edis锛岀敤浜庡疄鐜版秷鎭槦鍒楀姛鑳姐€俁edis鏈嶅姟鍣ㄤ綅浜庢湰鍦颁富鏈轰笂锛岀鍙e彿涓洪粯璁ょ殑6379銆傚叧浜嶳edis鐨勯厤缃俊鎭涓嬶細
```makefile
spring.redis.host=localhost
spring.redis.port=6379
```
涓轰簡纭繚搴旂敤鐨勫畨鍏ㄦ€э紝鎴戜滑杩橀渶瑕佽缃畨鍏ㄧ敤鎴风殑淇℃伅銆傝繖閲岋紝鎴戜滑灏嗙敤鎴峰悕璁剧疆涓衡€渁dmin鈥濓紝骞朵负鍏跺垎閰嶄簡涓€涓瘑鐮併€傚叧浜庡畨鍏ㄩ厤缃殑淇℃伅濡備笅锛?/p>
```makefile
spring.security.user.name=admin
spring.security.user.password=admin瀵嗙爜锛堟澶勫缓璁缃负鏇村畨鍏ㄧ殑瀵嗙爜锛?/p>
```
鎺ヤ笅鏉ワ紝鎴戜滑闇€瑕佸湪椤圭洰鐨刞src/main/java`鐩綍涓嬪垱寤轰竴涓悕涓篳Application`鐨勭被锛岃繖鏄垜浠殑SpringBoot搴旂敤鐨勫惎鍔ㄧ偣銆備互涓嬫槸鐩稿簲鐨凧ava浠g爜锛?/p>
```java
package com.example.chatapp; // 瀵煎叆蹇呰鐨勫寘鍜岀被瀹氫箟锛堝凡鐪佺暐锛?/p>
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ChatAppApplication {
public static void main(String[] args) {
SpringApplication.run(ChatAppApplication.class, args);
}
---
Redis娑堟伅闃熷垪鎺ュ彛涓庡疄鐜扮殑鑹烘湳
瀹氫箟娑堟伅闃熷垪鎺ュ彛 鈥?MessageQueueService
鍦ㄥ瘜鏈夌幇浠f劅鐨勬灦鏋勮璁′腑锛屾垜浠瀯寤轰竴涓悕涓篗essageQueueService鐨勬帴鍙o紝瀹冧负Redis娑堟伅闃熷垪鎼缓涓€涓紭缇庣殑妗ユ锛?/p>
鍖呭悕锛?com.example.chatapp.service
鎺ュ彛瀹氫箟锛?/p>
`public interface MessageQueueService {
boolean sendMessage(String queueName, String message);
String receiveMessage(String queueName);
}`
鍛堢幇Redis娑堟伅闃熷垪鐨勫疄闄呮搷浣?鈥?MessageQueueServiceImpl绫?/p>
瀹炵幇涓婅堪鎺ュ彛锛屾垜浠皢浼橀泤鍦版搷鎺edis娑堟伅闃熷垪锛屽氨鍍忔搷绾佃嚜宸辩殑闊充箰鐩掍竴鏍疯交鏉捐嚜濡傘€備互涓嬫槸鍏蜂綋瀹炵幇缁嗚妭锛?/p>
鍖呭悕锛?com.example.chatapp.service.impl
浠g爜瀹炵幇锛?/p>
浣跨敤Spring妗嗘灦涓殑RedisTemplate浣滀负鏍稿績宸ュ叿锛屾垜浠敞鍏edisTemplate鍒癕essageQueueServiceImpl绫讳腑銆傞€氳繃姝ゆ柟娉曪紝鎴戜滑鍙互鍒╃敤RedisTemplate寮哄ぇ鐨勬搷浣滆兘鍔涳紝瀹炵幇娑堟伅鐨勫彂閫佸拰鎺ユ敹銆備緥濡傦紝浣跨敤`opsForList().rightPush()`鏂规硶鍙戦€佹秷鎭埌鎸囧畾闃熷垪锛屽苟浣跨敤`opsForList().leftPop()`鏂规硶浠庨槦鍒椾腑鑾峰彇娑堟伅銆傚疄鐜拌繃绋嬩腑鍏呮弧浜嗗绾跨▼瀹夊叏鐨勪紭闆呭鐞嗗拰瀵规椂闂寸殑绮惧噯鎺у埗銆傝繑鍥炵殑娑堟伅绫诲瀷鏄庣‘鍦板0鏄庝负String绫诲瀷锛屾柟渚垮悗缁鐞嗐€傛瘡娆℃垚鍔熷彂閫佹秷鎭悗閮戒細杩斿洖true锛屼互纭娑堟伅鐨勫彂閫佺姸鎬併€傚綋鎺ユ敹娑堟伅鏃讹紝濡傛灉闃熷垪涓虹┖鍒欎笉浼氶樆濉炵瓑寰呮秷鎭殑鍒版潵銆傛暣涓疄鐜拌繃绋嬪氨鍍忔槸鍦ㄦ紨濂忎竴鏇蹭紭缇庣殑浜ゅ搷涔愶紝姣忎釜闊崇閮芥伆鍒板ソ澶勩€?/p>
鏈嶅姟绫讳腑闆嗘垚娑堟伅闃熷垪鏈嶅姟 鈥?NotificationService绫?/p>
---
瑙i攣Spring Security鐨勯厤缃绫?/p>
鍦╝pplication.properties鏂囦欢涓紝鎴戜滑璁剧疆Spring Security鐨勫熀纭€閰嶇疆锛?/p>
`spring.security.user.name=admin`
`spring.security.user.password=admin`
绠€鍗曠殑涓よ浠g爜锛屽凡缁忎负鎴戜滑鍦⊿pring搴旂敤涓紑鍚簡瀹夊叏鐨勫ぇ闂ㄣ€?/p>
鎺ヤ笅鏉ワ紝鎴戜滑娣卞叆鍒癝pring Security鐨勮繃婊ゅ櫒閰嶇疆涓紝纭繚鍙湁缁忚繃璁よ瘉鐨勭敤鎴锋墠鑳借闂壒瀹氱殑璧勬簮銆?/p>
鍦╜com.example.chatapp.config`鍖呬笅锛屾垜浠湁涓€涓悕涓篳SecurityConfig`鐨勭被锛屽畠缁ф壙浜哷WebSecurityConfigurerAdapter`銆傝繖涓被涓烘垜浠彁渚涗簡寮哄ぇ鐨勫畨鍏ㄩ厤缃兘鍔涖€傞€氳繃娉ㄥ叆`AuthenticationManagerBuilder`鐨勫疄渚嬶紝鎴戜滑鍙互瀹氫箟鍏ㄥ眬鐨勫畨鍏ㄧ瓥鐣ャ€備緥濡傦細
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin")
.password("admin")
.roles("USER");
}
// ... 鍏朵粬閰嶇疆 ...
}
```
鍦ㄨ繖涓緥瀛愪腑锛屾垜浠畾涔変簡涓€涓悕涓衡€渁dmin鈥濈殑鐢ㄦ埛锛屽瘑鐮佷篃鏄€渁dmin鈥濓紝骞惰祴浜堝叾鈥淯SER鈥濊鑹层€傝繖鎰忓懗鐫€鍙湁鎷ユ湁姝よ鑹叉垨鏇撮珮鏉冮檺鐨勭敤鎴锋墠鑳芥墽琛屽彈淇濇姢鐨勬搷浣溿€?/p>
JWT Token鏈哄埗鐨勬繁搴﹁В鏋愪笌瀹炴柦
JWT锛圝SON Web Token锛夋槸涓€涓紑鏀剧殑璁块棶浠ょ墝鏍囧噯锛屽畠涓鸿璇佸拰鎺堟潈鎻愪緵浜嗗己澶х殑鏀寔銆傚湪JWT鐨勪笘鐣岄噷锛屽鎴风鍜屾湇鍔$涔嬮棿閫氳繃鍏变韩鐨勫瘑閽ヨ繘琛屼护鐗岀殑绛惧悕鍜岄獙璇併€?/p>
杩欎釜鏈哄埗鐨勬牳蹇冨湪浜巘oken锛屽畠鏄竴涓敱涓夐儴鍒嗙粍鎴愮殑瀛楃涓诧紝鍚勯儴鍒嗕箣闂寸敤`.`鍒嗛殧銆傛瘡涓€閮ㄥ垎閮芥湁鍏剁壒瀹氱殑鍚箟鍜屽姛鑳斤紝渚嬪澶撮儴鍙互鍖呭惈浠ょ墝鐨勭被鍨嬪拰閲囩敤鐨勫姞瀵嗙畻娉曠瓑淇℃伅銆傛湇鍔$鍦ㄥ彂鏀総oken鏃朵細瀵瑰叾杩涜绛惧悕锛屼互纭繚鍏跺畬鏁存€у拰鍙俊搴︺€傚鎴风鍦ㄦ帴鏀跺埌token鍚庯紝鍙互鍦ㄥ悗缁殑璇锋眰涓檮甯︽token锛屼互璇佹槑鑷繁鐨勮韩浠藉拰鏉冮檺銆?/p>
瀹炴柦JWT Token鏈哄埗鏃讹紝闇€瑕佹敞鎰忛€夋嫨鍚堥€傜殑瀵嗛挜绠$悊绛栫暐銆佺‘淇漷oken鐨勫畨鍏ㄥ瓨鍌ㄤ笌浼犺緭銆佸悎鐞嗚鍒抰oken鐨勬湁鏁堟湡绛夈€備负浜嗗寮哄畨鍏ㄦ€э紝杩樺彲浠ョ粨鍚堝叾浠栨妧鏈HTTPS銆丱Auth绛変竴璧蜂娇鐢ㄣ€?/p>
鍦ㄥ疄鏂借繃绋嬩腑锛岃纭繚姣忎竴姝ラ兘閬靛惊鏈€浣冲疄璺靛拰瀹夊叏鏍囧噯锛屼互纭繚绯荤粺鐨勫畨鍏ㄦ€с€?/p>
甯屾湜閫氳繃浠ヤ笂鐨勮В璇讳笌瀹炴柦鏂规鑳藉府鍔╀綘鏇村ソ鍦扮悊瑙e拰杩愮敤JWT Token鏈哄埗銆?馃攼鉁?/p>
---
浠ヤ笂鍐呭浠呬緵鍙傝€冿紝鍙牴鎹疄闄呴渶姹傝繘涓€姝ヨ皟鏁翠紭鍖栥€?鍒涘缓JWT Token绫讳笌宸ュ叿搴?/p>
鍦╜com.example.chatapp.util`鍖呬腑锛屾垜浠畾涔変簡涓€涓猔JwtUtil`绫伙紝涓撻棬鐢ㄤ簬澶勭悊JWT Token鐨勭敓鎴愩€侀獙璇佸拰瑙f瀽銆備互涓嬫槸璇ョ被鐨勪富瑕佸姛鑳斤細
JwtUtil 绫?/p>
鎴戜滑鍦ㄨ绫讳腑瀹氫箟浜嗕竴浜涘父閲忥紝濡俙SECRET_KEY`鐢ㄤ簬鐢熸垚JWT鐨勫瘑閽ワ紝浠ュ強`EXPIRATION_TIME`琛ㄧずToken鐨勮繃鏈熸椂闂淬€傛帴鐫€锛屾垜浠彁渚涗簡涓変釜鏂规硶锛?/p>
`generateToken(String subject)`锛氱敓鎴愪竴涓柊鐨凧WT Token锛屽叾涓寘鎷富棰樸€佺鍙戞椂闂村拰杩囨湡鏃堕棿绛変俊鎭紝骞朵娇鐢℉S256绠楁硶杩涜绛惧悕銆?/p>
`validateToken(String token, String subject)`锛氶獙璇佺粰瀹氱殑Token鏄惁鏈夋晥锛屽寘鎷鏌ュ叾绛惧悕鍜屾槸鍚﹁繃鏈熴€?/p>
`getClaimsFromToken(String token)`锛氫粠缁欏畾鐨凾oken涓彁鍙栧嚭鍏朵腑鐨勫0鏄庝俊鎭€?/p>
搴旂敤JWT Token杩涜鐢ㄦ埛璁よ瘉
鍦╜com.example.chatapp.config.security`鍖呬腑锛屾垜浠畾涔変簡`JwtAuthenticationFilter`绫伙紝杩欐槸涓€涓熀浜庤姹傜殑杩囨护鍣紝鐢ㄤ簬澶勭悊鐢ㄦ埛璁よ瘉銆傝绫诲湪姣忔璇锋眰鏃堕兘浼氭鏌ヨ姹傚ご涓殑Authorization淇℃伅銆傚鏋滄壘鍒颁簡鏈夋晥鐨凚earer Token骞朵笖鍏朵富棰樹负鈥渁dmin鈥濓紝鍒欐彁鍙朤oken涓殑澹版槑淇℃伅锛屽苟璁剧疆鐩稿簲鐨勭敤鎴疯璇佷俊鎭€?/p>
杩涢樁鍔熻兘锛氬疄鐜颁竴瀵逛竴娑堟伅鍙戦€佹帴鏀?/p>
瀵逛簬鍗虫椂娑堟伅鐨勬牳蹇冨姛鑳斤紝鐗瑰埆鏄疄鐜颁竴瀵逛竴鐨勬秷鎭彂閫佹帴鏀讹紝鎴戜滑鍙互浣跨敤WebSocket鎴朒TTP闀胯疆璇㈡妧鏈€傚湪鏈嶅姟绔紝鎴戜滑鍙互寤虹珛涓€涓秷鎭槦鍒楁垨绫讳技鐨勬暟鎹粨鏋勬潵绠$悊姣忎釜鐢ㄦ埛鐨勬秷鎭繛鎺ャ€傚綋鏈夋柊鐨勬秷鎭彂閫佹椂锛屾湇鍔″櫒浼氭煡鎵惧搴旂殑杩炴帴骞跺彂閫佹秷鎭粰鎸囧畾鐨勭敤鎴枫€傝繖绉嶆柟寮忕殑浼樼偣鏄彲浠ュ疄鐜板疄鏃剁殑閫氫俊锛岄€傜敤浜庨渶瑕佸嵆鏃跺弽棣堢殑搴旂敤鍦烘櫙銆?/p>
WebSocket 瀹炵幇鏂瑰紡
浣跨敤WebSocket锛屾垜浠彲浠ヤ负姣忎釜鐢ㄦ埛寤虹珛涓€涓寔涔呯殑杩炴帴銆傚綋鏈夋柊鐨勬秷鎭椂锛屾湇鍔″櫒鍙互鐩存帴閫氳繃璇ヨ繛鎺ュ皢娑堟伅鎺ㄩ€佺粰鐢ㄦ埛銆傝繖绉嶆柟寮忛渶瑕佹湇鍔″櫒绔殑WebSocket鏀寔浠ュ強瀹㈡埛绔殑WebSocket搴撱€傝繕闇€瑕佸鐞嗚繛鎺ュ缓绔嬨€佹秷鎭鐞嗐€佽繛鎺ュ叧闂瓑鐢熷懡鍛ㄦ湡浜嬩欢銆?/p>
HTTP闀胯疆璇㈠疄鐜版柟寮?/p>
鍙︿竴绉嶆柟寮忔槸浣跨敤HTTP闀胯疆璇€傚鎴风鍙戣捣璇锋眰鍚庯紝鏈嶅姟鍣ㄤ細淇濇寔杩炴帴鎵撳紑鐘舵€侊紝绛夊緟鏈夋秷鎭椂杩斿洖缁欏鎴风銆傚鏋滄病鏈夋秷鎭紝鏈嶅姟鍣ㄥ彲浠ヤ繚鎸佽繛鎺ヤ竴娈垫椂闂达紙渚嬪鍑犵锛夊悗鍏抽棴锛屽鎴风鍐嶆鍙戣捣璇锋眰銆傝繖绉嶆柟寮忕浉瀵圭畝鍗曪紝浣嗛渶瑕佹洿澶氱殑璇锋眰-鍝嶅簲寰幆锛屽彲鑳戒笉閫傜敤浜庨珮骞跺彂鎴栧疄鏃舵€ц姹傝緝楂樼殑鍦烘櫙銆?/p>
瀹炵幇涓€瀵逛竴鐨勬秷鎭彂閫佹帴鏀堕渶瑕佹湇鍔$鍜屽鎴风鐨勯厤鍚堬紝閫夋嫨鍚堥€傜殑閫氫俊鏂瑰紡骞舵牴鎹叿浣撻渶姹傝繘琛屽疄鐜般€侸WT Token浣滀负鐢ㄦ埛璁よ瘉鐨勬墜娈碉紝鍙互鏈夋晥鍦颁繚璇侀€氫俊鐨勫畨鍏ㄦ€с€俉ebSocket锛氬埄鐢⊿pring Websocket鏋勫缓瀹炴椂閫氫俊妗ユ
WebSocket宸茬粡鎴愪负鐜颁唬Web搴旂敤涓嶅彲鎴栫己鐨勪竴閮ㄥ垎锛屽畠涓哄疄鏃跺弻鍚戦€氫俊鎻愪緵浜嗗己澶х殑鏀寔銆傚€熷姪Spring Websocket锛屾垜浠彲浠ヨ交鏉惧湴瀹炵幇WebSocket鐨勯泦鎴愪笌杩愮敤锛屼负搴旂敤绋嬪簭甯︽潵涓板瘜鐨勫疄鏃朵氦浜掍綋楠屻€傛湰鏂囧皢浠嬬粛濡備綍浣跨敤Spring Websocket鏉ュ疄鐜癢ebSocket鐨勫己澶у姛鑳姐€?/p>
涓€銆佺悊瑙ebSocket涓嶴pring Websocket
WebSocket鏄竴绉嶇綉缁滈€氫俊鍗忚锛屽厑璁告湇鍔″櫒涓庡鎴风涔嬮棿杩涜鍏ㄥ弻宸ラ€氫俊銆備笌浼犵粺鐨凥TTP璇锋眰-鍝嶅簲妯″紡涓嶅悓锛學ebSocket寤虹珛杩炴帴鍚庯紝鏈嶅姟鍣ㄥ彲浠ヤ富鍔ㄥ悜瀹㈡埛绔彂閫佹秷鎭紝瀹炵幇瀹炴椂鏁版嵁浼犺緭銆傝€孲pring Websocket鏄熀浜嶫ava璇█鐨刉ebSocket瀹炵幇妗嗘灦锛屽畠涓哄紑鍙戣€呮彁渚涗簡渚挎嵎鐨勫伐鍏峰拰缁勪欢锛屼互绠€鍖朩ebSocket鐨勫紑鍙戣繃绋嬨€?/p>浜屻€丼pring Websocket鐨勫揩閫熷叆闂?/h3>
瑕佷娇鐢⊿pring Websocket锛岄鍏堥渶瑕佸湪椤圭洰涓紩鍏pring Websocket鐨勪緷璧栥€傛帴鐫€锛屾垜浠彲浠ュ垱寤轰竴涓猈ebSocket鐨勯厤缃被锛岀敤浜庤缃甒ebSocket鐨勭鐐广€傚湪閰嶇疆绫讳腑锛屾垜浠彲浠ュ畾涔夋秷鎭唬鐞嗐€佺瓑鐩稿叧閰嶇疆銆傝繕闇€瑕佸垱寤轰竴涓鐞哤ebSocket娑堟伅鐨勫鐞嗗櫒绫伙紝鐢ㄤ簬澶勭悊瀹㈡埛绔彂閫佺殑娑堟伅浠ュ強鏈嶅姟鍣ㄧ鐨勫搷搴旈€昏緫銆傚鐞嗗櫒绫讳腑鐨勬柟娉曞彲浠ユ牴鎹渶瑕佽繘琛岃嚜瀹氫箟锛屼互瀹炵幇鐗瑰畾鐨勪笟鍔¢€昏緫銆?/p>涓夈€佸疄鐜板疄鏃堕€氫俊鍔熻兘
閫氳繃Spring Websocket锛屾垜浠彲浠ヨ交鏉惧湴瀹炵幇瀹炴椂閫氫俊鍔熻兘銆傛垜浠渶瑕佸垱寤轰竴涓猈ebSocket鐨勫鎴风杩炴帴锛岀敤浜庝笌鏈嶅姟鍣ㄥ缓绔嬭繛鎺ュ苟鍙戦€佹秷鎭€傚湪瀹㈡埛绔繛鎺ュ缓绔嬪悗锛屾垜浠彲浠ヤ娇鐢ㄥ彂閫佹秷鎭殑鏂规硶鍚戞湇鍔″櫒鍙戦€佹暟鎹€傛湇鍔″櫒鎺ユ敹鍒版秷鎭悗锛屼細璋冪敤澶勭悊鍣ㄧ被涓殑鐩稿簲鏂规硶杩涜澶勭悊锛屽苟灏嗙粨鏋滃彂閫佸洖瀹㈡埛绔€傝繖鏍凤紝瀹㈡埛绔拰鏈嶅姟鍣ㄤ箣闂村氨鍙互杩涜瀹炴椂鐨勫弻鍚戦€氫俊浜嗐€傛垜浠繕鍙互鍒╃敤娑堟伅浠g悊鍔熻兘瀹炵幇娑堟伅鐨勫箍鎾姛鑳斤紝浣夸竴涓秷鎭彲浠ュ彂閫佺粰澶氫釜瀹㈡埛绔€傝繖绉嶅箍鎾満鍒跺彲浠ュ箍娉涘簲鐢ㄤ簬瀹炴椂鑱婂ぉ銆佸疄鏃堕€氱煡绛夊満鏅€?/p>鍥涖€佷紭鍖栦笌鎵╁睍
---
WebSocket閰嶇疆涓庡疄鐜?/p>涓€銆乄ebSocket閰嶇疆姒傝
鍦╜ChatWebSocket.java`鏂囦欢涓紝鎴戜滑绮惧績缂栫粐浜哤ebSocket杩炴帴鐨勯厤缃€傞€氳繃浠ヤ笅鍏抽敭鍖呭拰娉ㄨВ锛屾垜浠紑濮嬩簡杩欏満WebSocket鐨勯瓟娉曚箣鏃呫€?/p>
浠g爜鐗囨锛?/p>
```java
package com.example.chatapp.websocket;
import ... // 瀵煎叆蹇呰鐨勫簱鍜屾敞瑙?/p>
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
// ... 瀹炵幇鏂规硶锛岄厤缃甒ebSocket杩炴帴
}
```
浜屻€乄ebSocket鏈嶅姟绮惧僵瀹炵幇鍦╜ChatWebSocket.java`涓紝鎴戜滑瀹炵幇浜哤ebSocket鏈嶅姟鐨勫績鑴忛儴鍒嗐€傝繖鏄竴涓秷鎭鐞嗕腑蹇冿紝璐熻矗澶勭悊鍚勭娑堟伅绫诲瀷銆?/p>
浠g爜鐗囨锛?/p>
```java
package com.example.chatapp.websocket;
import ... // 瀵煎叆蹇呰鐨勫簱鍜岀被
@Component
public class ChatWebSocket implements MessageHandler {
// ... 瀹氫箟闈欐€丮ap鏉ュ瓨鍌ㄤ細璇濅俊鎭紝骞跺疄鐜板悇涓秷鎭鐞嗘柟娉曘€?/p>
}
```
褰撳簲鐢ㄥ惎鍔ㄦ椂锛岀‘淇漙ChatWebSocket`涓嶹ebSocket杩炴帴琚纭厤缃苟娉ㄥ唽銆傝繖涓€鐜妭鑷冲叧閲嶈锛屼负鏁翠釜鑱婂ぉ搴旂敤鐨勭ǔ瀹氳繍琛屽瀹氬熀纭€銆?/p>涓夈€佸瀵瑰娑堟伅鎺ㄩ€佸師鐞嗕笌瀹炵幇
璁╂垜浠彮寮€澶氬澶氭秷鎭帹閫佺殑绁炵闈㈢罕銆傝繖涓€鍔熻兘鍩轰簬娑堟伅闃熷垪鐨勫垎鍙戞満鍒讹紝褰撲换浣曠敤鎴峰彂璧锋秷鎭椂锛岃娑堟伅浼氳鍙戦€佸埌娑堟伅闃熷垪涓€傞殢鍚庯紝绯荤粺浼氬皢杩欐潯娑堟伅鍒嗗彂缁欐墍鏈夎繛鎺ョ殑瀹㈡埛绔紝瀹炵幇澶氬澶氱殑瀹炴椂閫氫俊銆傚湪`ChatWebSocket`绫讳腑锛屾垜浠娇鐢ㄤ簡闈欐€佹柟娉昤sendTextMessageAll`鍜宍sendBinaryMessageAll`鏉ュ箍鎾秷鎭粰鎵€鏈夊鎴风銆傝繖浜涙柟娉曠殑鍐呴儴閫昏緫鏄亶鍘嗗瓨鍌ㄦ墍鏈変細璇濈殑Map锛屽苟灏嗘秷鎭彂閫佺粰姣忎釜浼氳瘽銆傝繖鏍凤紝浠讳綍鐢ㄦ埛鐨勫彂鍑虹殑娑堟伅閮戒細琚帹閫佺粰鍏朵粬鎵€鏈夊湪绾跨敤鎴枫€?/p>
---
瀵逛簬涓€瀵逛竴鐨勬秷鎭紶閫掓ā寮忥紝鎴戜滑宸茬粡閫氳繃Redis鎴愬姛瀹炵幇浜嗗叾鏈哄埗銆傚綋闈复澶氬澶氱殑娑堟伅鎺ㄩ€侀渶姹傛椂锛屾垜浠彲浠ヨ繘涓€姝ユ瀯寤轰竴涓洿涓哄鏉傜殑娑堟伅涓績绯荤粺锛岃繖涓郴缁熶笉浠呮帴鏀跺苟瀛樺偍鎵€鏈夌殑娑堟伅锛岃繕鑳芥牴鎹帴鏀惰€呯殑璁㈤槄鍏崇郴锛屾櫤鑳藉湴灏嗘秷鎭簿鍑嗘帹閫佸埌姣忎竴涓搴旂殑鎺ユ敹鑰呴槦鍒椾腑銆?/p>
杩欎釜绯荤粺鐨勫伐浣滄祦绋嬪彲浠ョ粏鍒嗕负鍑犱釜鍏抽敭姝ラ锛?/p>
1. 娑堟伅鎺ユ敹锛氭墍鏈夌殑娑堟伅棣栧厛琚彂閫佸埌娑堟伅涓績锛岃繖閲屽儚鏄竴涓箒蹇欑殑浜ら€氭灑绾斤紝璐熻矗澶勭悊淇℃伅鐨勬祦鍏ュ拰鍒嗛厤銆?/p>
2. 璁㈤槄鍏崇郴鍖归厤锛氭秷鎭腑蹇冧細鏍规嵁娑堟伅鐨勫睘鎬у拰鎺ユ敹鑰呯殑璁㈤槄鍋忓ソ杩涜鍖归厤銆傝繖灏卞儚鏄湪搴炲ぇ鐨勪俊鎭簱涓煡鎵剧洰鏍囧彈浼楋紝纭繚姣忎竴鏉℃秷鎭兘鑳芥壘鍒板畠鐨勭洰鏍囥€?/p>
3. 娑堟伅鍒嗗彂锛氫竴鏃﹀尮閰嶆垚鍔燂紝娑堟伅灏嗚鎺ㄩ€佸埌鐩稿簲鎺ユ敹鑰呯殑闃熷垪涓€傝繖涓繃绋嬮渶瑕侀珮搴︾殑鍑嗙‘鎬у拰鏁堢巼锛岀‘淇濅俊鎭殑鍙婃椂鎬у拰鍑嗙‘鎬с€?/p>
4. 闃熷垪绠$悊锛氭瘡涓帴鏀惰€呴兘鏈夎嚜宸辩殑娑堟伅闃熷垪锛岃繖鏍峰彲浠ョ‘淇濅俊鎭殑椤哄簭鎬у拰瀹屾暣鎬с€傜郴缁熻繕闇€瑕佸闃熷垪杩涜鏈夋晥鐨勭鐞嗭紝濡傛秷鎭殑鎸佷箙鍖栧瓨鍌ㄣ€侀槦鍒楃殑璐熻浇鍧囪 绛夈€?/p>
娑堟伅涓績鐨勫崕涓藉疄鐜?/p>
鍦∕essageCenter绫荤殑蹇冭剰鍦板甫锛屾垜浠阀濡欏疄鐜颁簡娑堟伅涓績鍔熻兘锛?/p>
com.example.chatapp.messagecenter鍖呭唴涓€鐬ワ紝鎴戜滑鐪嬪埌浜哅essageCenter绫伙紝杩欎釜绫荤姽濡傛秷鎭笘鐣岀殑浜ら€氭灑绾姐€傝鎴戜滑娣卞叆浜嗚В鍏朵腑鐨勫ゥ绉樸€傚€熷姪com.example.chatapp.websocket.ChatWebSocket鐨勬敮鎸侊紝鎴戜滑涓篧ebSocket浼氳瘽鍒涘缓浜嗕竴涓鐞嗗钩鍙般€備娇鐢–opyOnWriteArrayList骞跺彂瀹瑰櫒鏉ョ鐞嗚繖浜涗細璇濓紝纭繚鍦ㄥ绾跨▼鐜涓嬬殑鏁版嵁瀹夊叏鎬с€傞€氳繃@Component娉ㄨВ锛岃繖涓被琚玈pring妗嗘灦鑷姩璇嗗埆骞舵敞鍏ュ埌搴旂敤绋嬪簭鐨勪笂涓嬫枃涓€?/p>
娑堟伅鐨勭敓鍛藉懆鏈熶笌瀛樺偍涔嬫梾
鎯宠薄涓€涓嬫秷鎭殑鏃呯▼锛氫粠浜х敓鍒板瓨鍌紝瀹冧滑闇€瑕佷竴涓畨鍏ㄧ殑鏍栨伅鍦般€傛鍒伙紝鎴戜滑杩堝悜鏁版嵁搴撶殑璁捐涓庢ā鍨嬪疄鐜伴樁娈点€傚湪com.example.chatapp.model鍖呭唴锛屾垜浠璁′簡涓€涓悕涓篗essage鐨勫疄浣撶被銆傚畠鎷ユ湁鏍囪瘑韬唤鐨勫敮涓€ID銆佸彂閫佽€呭拰鎺ユ敹鑰呯殑鐢ㄦ埛瀵硅薄銆佹秷鎭唴瀹逛互鍙婃椂闂存埑绛夊睘鎬с€傞€氳繃JPA娉ㄨВ锛岃繖浜涘睘鎬т笌鏁版嵁搴撲腑鐨勮〃缁撴瀯绱у瘑鍏宠仈銆?/p>
寮傛浠诲姟璋冨害涓庢秷鎭殑鎸佷箙鍖栭瓟娉?/p>
涓轰簡璁╂秷鎭殑瀛樺偍杩囩▼鏇村姞娴佺晠锛屾垜浠紩鍏ヤ簡寮傛浠诲姟澶勭悊鏈哄埗銆傚湪com.example.chatapp.service.impl鍖呬腑锛孧essagePersistenceServiceImpl绫婚粯榛樻壙鎷呯潃娑堟伅鎸佷箙鍖栫殑閲嶄换銆傞€氳繃@Async娉ㄨВ锛屾瘡褰搒aveMessages鏂规硶琚皟鐢ㄦ椂锛屽畠灏嗗湪鍚庡彴绾跨▼涓紭闆呭湴澶勭悊娑堟伅鐨勫瓨鍌ㄦ搷浣溿€傚畠涓嶮essageRepository鍗忎綔锛岀‘淇濇秷鎭畨鍏ㄥ湴瀛樺偍鍦ㄦ暟鎹簱涓€?/p>
杩芥函鍘嗗彶锛氭秷鎭煡璇笌灞曠ず
鍥炴兂杩囧幓锛屾垜浠兂瑕佽拷婧亰澶╃殑鍘嗗彶璁板綍鎬庝箞鍔烇紵鏁版嵁搴撲腑鐨勬秷鎭ā鍨嬩负鎴戜滑鎻愪緵浜嗙瓟妗堛€傚湪com.example.chatapp.service鍖呭唴锛屾垜浠畾涔変簡MessageHistoryService鎺ュ彛锛屼笓闂ㄧ敤浜庢煡璇㈢敤鎴风殑娑堟伅鍘嗗彶銆傝€屽湪鍏跺疄鐜扮被MessageHistoryServiceImpl涓紝閫氳繃璋冪敤MessageRepository鐨勬柟娉曪紝鎴戜滑鍙互鏂逛究鍦拌幏鍙栧埌鐢ㄦ埛鐨勬秷鎭褰曘€傜畝鍗曞嚑琛屼唬鐮侊紝杞绘澗瀹炵幇鍘嗗彶娑堟伅鐨勬煡璇笌灞曠ず銆?/p>
瀹堟姢鏁版嵁瀹夊叏锛氶壌鏉冩満鍒剁殑娣卞叆鎺㈢储
鍦ㄥ嵆鏃堕€氳绯荤粺涓紝閴存潈鏈哄埗鏄畧鎶ゆ暟鎹畨鍏ㄧ殑鍧氬浐鍫″瀿銆傚畠娑电洊浜嗙敤鎴疯璇佸拰鎺堟潈涓ゅぇ鏍稿績閮ㄥ垎銆傜敤鎴疯璇佹槸杩欏骇鍫″瀿鐨勭涓€閬撳叧鍗°€傚畠瑕佹眰鎴戜滑楠岃瘉鐢ㄦ埛鐨勮韩浠斤紝閫氬父鍩轰簬鐢ㄦ埛鍚嶅拰瀵嗙爜杩涜楠岃瘉銆傚彧鏈夌粡杩囬獙璇佺殑鐢ㄦ埛鎵嶈兘杩涘叆閫氳绯荤粺鐨勫ぇ闂紝浠庤€岀‘淇濋€氫俊鐨勫畨鍏ㄦ€у拰鏁版嵁鐨勫畬鏁存€с€傝繖浠呬粎鏄壌鏉冩満鍒剁殑鍐板北涓€瑙掞紝鏇村娣卞叆鐨勫畨鍏ㄦ帾鏂藉拰绛栫暐绛夊緟鐫€鎴戜滑鍘绘帰绱㈠拰瀹炶返銆傛巿鏉冿細鐣屽畾鏉冮檺锛岃В閿佺壒瀹氳祫婧愪箣鏃?/p>
鍦ㄨ繖涓暟瀛楀寲鏃朵唬锛屾巿鏉冩垚涓轰簡鎴戜滑鐢熸椿涓笉鍙垨缂虹殑涓€鐜€傚畠濡傚悓涓€涓绉樼殑閽ュ寵锛屾帉鎻″湪鎴戜滑鎵嬩腑锛屽彧鏈夋嫢鏈夋纭殑鏉冮檺锛屾垜浠墠鑳芥墦寮€閭f墖閫氬線鐗瑰畾璧勬簮鐨勫ぇ闂ㄣ€傞偅涔堬紝鎺堟潈绌剁珶鎰忓懗鐫€浠€涔堝憿锛?/p>
鎺堟潈锛岃繖涓€杩囩▼瀹炶川涓婃槸瀵圭敤鎴锋潈闄愮殑鍒ゆ柇銆傚畠娑夊強鍒板鐢ㄦ埛鐨勮韩浠借繘琛岄獙璇侊紝纭鍏惰韩浠藉悗锛屾墠浼氳祴浜堝叾璁块棶鐗瑰畾璧勬簮鐨勬潈鍒┿€傝繖灏卞儚鏄竴鍦虹簿蹇冭璁$殑鎺㈤櫓涔嬫梾锛屽彧鏈夋嫢鏈夋纭殑閫氳璇侊紝鎵嶈兘缁х画鍓嶈锛屾帰绱㈤偅鏈煡鐨勫疂钘忋€?/p>
鍦ㄨ繖涓梾绋嬩腑锛屾瘡涓€涓敤鎴烽兘鏄竴涓媷鏁㈢殑鎺㈤櫓瀹躲€備粬浠€€鎻fⅵ鎯筹紝娓存湜鎺㈢储閭d簺绁炵鐨勮祫婧愩€傝繖浜涜祫婧愬苟闈為殢鎰忓彲寰楃殑锛屽畠浠濡ュ杽鍦颁繚鎶よ捣鏉ワ紝鍙负閭d簺鐪熸鎷ユ湁鏉冮檺鐨勪汉寮€鏀俱€傛巿鏉冿紝姝f槸杩欏満鎺㈤櫓涔嬫梾鐨勫畧闂ㄤ汉锛屽畠涓ユ牸鍦扮瓫閫夋瘡涓€浣嶆潵璁胯€咃紝纭繚鍙湁鐪熸鐨勫媷澹墠鑳界户缁墠琛屻€?/p>
鎺堟潈鐨勮繃绋嬪苟涓嶅鏉傘€傚畠濡傚悓涓€鍦轰弗璋ㄨ€屾湁搴忕殑浠紡锛岄渶瑕佺敤鎴锋彁渚涚浉搴旂殑鍑瘉锛屼互璇佹槑鑷繁鐨勮韩浠藉拰鏉冮檺銆備竴鏃﹂€氳繃楠岃瘉锛岀敤鎴峰氨浼氳璧嬩簣鐩稿簲鐨勬潈鍒╋紝鑳藉鑷敱鍦拌闂偅浜涚壒瀹氱殑璧勬簮銆傝繖浜涜祫婧愭垨璁告槸涓€娈电弽璐电殑璧勬枡锛屾垨璁告槸涓€椤归噸瑕佺殑鏈嶅姟锛屽彧鏈夋嫢鏈夋纭殑鏉冮檺锛岀敤鎴锋墠鑳戒韩鍙楀埌杩欎簺璧勬簮甯︽潵鐨勪究鍒╁拰涔愯叮銆?/p>
鍦ㄨ繖涓厖婊℃寫鎴樺拰鏈洪亣鐨勬椂浠o紝鎺堟潈鎴愪负浜嗘垜浠敓娲讳腑涓嶅彲鎴栫己鐨勪竴閮ㄥ垎銆傚畠淇濇姢鐫€鎴戜滑鐨勯殣绉佸拰瀹夊叏锛岀‘淇濆彧鏈夌湡姝f嫢鏈夋潈闄愮殑浜烘墠鑳借闂壒瀹氱殑璧勬簮銆傛垜浠簲璇ョ弽鎯滃苟灏婇噸鎺堟潈杩欎釜杩囩▼锛屽洜涓哄畠鏄垜浠€氬線鐗瑰畾璧勬簮鐨勫繀缁忎箣璺€傚彧鏈夐€氳繃涓ユ牸鐨勬巿鏉冮獙璇侊紝鎴戜滑鎵嶈兘鏀惧績鍦颁韩鍙楅偅浜涚弽璐电殑璧勬簮锛岃鎴戜滑鐨勭敓娲绘洿鍔犱赴瀵屽褰┿€?閴存潈鏈哄埗鐨勫疄鐜?/p>
鍦⊿pring Security妗嗘灦涓紝鎴戜滑濡備綍閰嶇疆閴存潈鏈哄埗鍛紵浠ヤ笅鏄垜浠疄鐜伴壌鏉冪殑涓€涓熀鏈厤缃ず渚嬨€?/p>
浣嶄簬 `com.example.chatapp.config` 鍖呬笅鐨?`SecurityConfig` 绫荤户鎵夸簡 `WebSecurityConfigurerAdapter`銆傛垜浠娇鐢ㄤ簡 `@Configuration` 鍜?`@EnableWebSecurity` 娉ㄨВ鏉ュ惎鐢⊿pring Security鐨勯厤缃€?/p>
鍦ㄨ繖涓被涓紝鎴戜滑棣栧厛娉ㄥ叆浜?`PasswordEncoder` 瀵硅薄鏉ョ紪鐮佺敤鎴风殑瀵嗙爜銆傚湪 `configureGlobal` 鏂规硶涓紝鎴戜滑閫氳繃 `AuthenticationManagerBuilder` 瀵硅薄閰嶇疆鍏ㄥ眬瀹夊叏绛栫暐锛岃繖閲屾垜浠娇鐢ㄤ簡鍐呭瓨璁よ瘉骞舵坊鍔犱簡涓€涓敤鎴峰悕涓?"admin"锛屽瘑鐮佺粡杩囩紪鐮佸悗鐨勭敤鎴凤紝骞惰祴浜堝叾 "USER" 瑙掕壊銆?/p>
鍦?`configure` 鏂规硶涓紝鎴戜滑閰嶇疆浜咹TTP瀹夊叏绛栫暐锛屽厑璁告煇浜涜矾寰勭殑璁块棶鑰屼笉闇€瑕佽璇侊紝鑰屽叾浠栬矾寰勫垯闇€瑕佽璇佹墠鑳借闂€傛垜浠繕閰嶇疆浜嗗熀浜庤〃鍗曠殑鐧诲綍椤甸潰浠ュ強鐧诲嚭绛栫暐銆?/p>
鎴戜滑杩橀€氳繃 `configure` 鏂规硶涓殑 `web.ignoring().antMatchers("/jws/")` 蹇界暐浜嗘煇浜涜矾寰勭殑璁よ瘉闇€姹傘€傝繖閮ㄥ垎鏄负浜嗙壒瀹氬満鏅殑闇€瑕侊紝濡傛煇浜涘叕鍏辫祫婧愪笉闇€瑕佽繘琛岃璇佽闂€?/p>
SSL鍔犲瘑閫氫俊鐨勫疄鐜?/p>
涓轰簡淇濋殰鍗虫椂閫氳鏁版嵁浼犺緭鐨勫畨鍏紝鎴戜滑閲囩敤SSL/TLS鍔犲瘑閫氫俊銆傚湪 `com.example.chatapp.websocket` 鍖呬笅鐨?`CustomWebSocketConfig` 绫讳腑锛屾垜浠垱寤轰簡涓€涓猈ebSocket鐨勬湇鍔″櫒绔偣閰嶇疆銆傚湪杩欎釜绫讳腑锛屾垜浠娇鐢ㄤ簡 `SSLEngine` 瀵硅薄鏉ュ鐞哠SL/TLS鍔犲瘑閫氫俊銆傚叿浣撳疄鐜版槸鍒涘缓涓€涓猈ebSocket浼氳瘽鍚庯紝浣跨敤Keystore鍒涘缓涓€涓猄SLEngine瀵硅薄锛屽苟灏嗗叾缁戝畾鍒颁細璇濋厤缃笂銆傝繖鏍凤紝鎴戜滑鐨刉ebSocket閫氫俊灏变細閫氳繃SSL/TLS鍔犲瘑鏉ヤ繚璇佹暟鎹紶杈撶殑瀹夊叏鎬с€?/p>
娴侀噺鎺у埗涓庨檺閫熺瓥鐣?/p>
涓轰簡闃叉鏈嶅姟杩囪浇锛屾垜浠渶瑕佸疄鐜版祦鎺у拰闄愰€熺瓥鐣ャ€傚湪 `com.example.chatapp.util` 鍖呬笅鐨?`RateLimiterConfig` 绫讳腑锛屾垜浠娇鐢≧esilience4j搴撲腑鐨凴ateLimiter鏉ュ垱寤洪€熺巼闄愬埗鍣ㄣ€傛垜浠畾涔変簡涓€涓悕涓?"chat-rate-limiter" 鐨勯€熺巼闄愬埗鍣紝骞惰缃簡姣忓垎閽熸渶澶氬彧鑳芥湁5涓姹傜殑闄愬埗锛屽鏋滆揪鍒伴檺鍒跺垯闇€瑕佺瓑寰呬竴绉掑悗鎵嶈兘鍐嶆灏濊瘯銆傝繖涓瓥鐣ュ彲浠ヤ繚鎶ゆ垜浠殑鏈嶅姟涓嶄細鍥犱负杩囧鐨勮姹傝€屽穿婧冦€?/p>
鎬ц兘鐩戞帶涓庝紭鍖栧疄璺?/p>
鎬ц兘鐩戞帶鍜屼紭鍖栨槸纭繚绯荤粺绋冲畾杩愯鐨勫叧閿幆鑺傘€傚湪杩欎竴閮ㄥ垎锛屾垜浠皢浠嬬粛濡備綍浣跨敤鐩戞帶宸ュ叿鏉ョ洃鎺х郴缁熺殑鎬ц兘骞惰繘琛屼紭鍖栥€傛垜浠帹鑽愪娇鐢⊿pring Boot Actuator銆丳rometheus鍜孏rafana绛夊伐鍏疯繘琛岀洃鎺с€傝繖浜涘伐鍏峰彲浠ュ府鍔╂垜浠敹闆嗙郴缁熺殑鍚勯」鎸囨爣鏁版嵁锛屽CPU浣跨敤鐜囥€佸唴瀛樹娇鐢ㄦ儏鍐点€佽姹傚搷搴旀椂闂寸瓑锛屼粠鑰屽府鍔╂垜浠強鏃跺彂鐜扮郴缁熺殑鐡堕骞惰繘琛屼紭鍖栥€傞€氳繃鎸佺画鐨勭洃鎺у拰浼樺寲锛屾垜浠彲浠ョ‘淇濈郴缁熺殑绋冲畾鎬у拰鎬ц兘涓嶆柇鎻愬崌銆備紭鍖栨帾鏂斤細閲嶅鎶€鏈灦鏋勶紝鎻愬崌鎬ц兘涓庣敤鎴蜂綋楠屻€傚寘鎷敼杩涗唬鐮佺粨鏋勩€佷紭鍖栫紦瀛樻満鍒躲€佹暟鎹垎鐗囧鐞嗕互鍙婂疄鐜板紓姝ュ鐞嗐€?/p>
瀹炴垬妗堜緥涓庨儴缃叉寚鍗楋細
1.璁块棶娴嬭瘯搴旂敤鍔熻兘锛氳璁块棶[localhost:8080/login](localhost:8080/login)杩涘叆鐧诲綍椤甸潰銆傛垚鍔熺櫥褰曞悗锛屾偍鍙互閫氳繃璁块棶ws://localhost:8080/ws/chat鎴栧叾浠朩ebSocket URL鏉ュ彂閫佸拰鎺ユ敹娑堟伅锛屼綋楠屽嵆鏃堕€氳鐨勪究鎹锋€с€?/p>
闆嗘垚瀹炰緥浠g爜婕旂ず锛?/p>
鎴戜滑鐨勫簲鐢ㄥ凡缁忛泦鎴愪簡璇稿瀹炵敤鍔熻兘锛屽叿浣撶殑浠g爜瀹炵幇灏嗗湪鐩稿叧鏂囨。鍜屽紑婧愮ぞ鍖轰腑璇︾粏灞曠ず銆傛杩庢偍娣卞叆鎺㈢储锛屽叡鍚屼负浼樺寲浜у搧鍑哄姏銆?/p>
浜戜笂閮ㄧ讲涓庨厤缃細
閮ㄧ讲鍒颁簯鏈嶅姟锛氬€熷姪Docker瀹瑰櫒鍖栨妧鏈垨涓绘祦浜戞湇鍔℃彁渚涘晢鐨勫钩鍙帮紝杞绘澗灏嗗簲鐢ㄩ儴缃插埌浜戠锛岀‘淇濆簲鐢ㄧ殑楂樻晥杩愯鍜屾墿灞曟€с€?/p>
閰嶇疆涓庝紭鍖栵細
鐜鍙橀噺锛氫娇鐢ㄧ幆澧冨彉閲忕鐞嗘晱鎰熶俊鎭紝澧炲己搴旂敤鐨勫畨鍏ㄦ€с€?/p>
璐熻浇鍧囪 锛氬埄鐢ㄤ簯鏈嶅姟鐨勮礋杞藉潎琛″姛鑳斤紝纭繚搴旂敤鍦ㄩ珮骞跺彂涓嬬殑绋冲畾杩愯銆?/p>
鑷姩缂╂斁锛氭牴鎹笟鍔¢渶姹傚姩鎬佽皟鏁磋祫婧愶紝瀹炵幇搴旂敤鐨勮嚜閫傚簲鎵╁睍銆?/p>
鎸佺画闆嗘垚涓庢寔缁儴缃诧紙CI/CD锛夛細
涓哄疄鐜板紑鍙戙€佹祴璇曚笌鐢熶骇鐨勬棤缂濊鎺ワ紝鎴戜滑閲囩敤浜咰I/CD娴佺▼銆傝嚜鍔ㄥ寲鏋勫缓銆佹祴璇曞拰閮ㄧ讲鐨勬瘡涓樁娈碉紝濡備笅锛?/p>
1. build闃舵锛氭墽琛宍mvn clean install -DskipTests`杩涜鏋勫缓锛岀敓鎴愮殑jar鏂囦欢灏嗗湪target鐩綍涓壘鍒帮紝骞惰缃叾鏈夋晥鏈熶负24灏忔椂銆?/p>
2. deploy闃舵锛氬湪姝ら樁娈碉紝鎮ㄥ皢杩愯鎮ㄧ殑閮ㄧ讲鑴氭湰锛屽苟閫氳繃docker鍛戒护鍚姩鎮ㄧ殑搴旂敤銆備緥濡傦紝浣跨敤鍛戒护`docker run -e PORT=8080 -p 8080:8080 your-image-name`锛屽苟纭繚浠呭湪master鍒嗘敮杩涜鎿嶄綔銆?/p>
閬靛惊鏈寚鍗楀拰浠g爜绀轰緥锛屾偍灏嗘垚鍔熸瀯寤哄苟閮ㄧ讲涓€涓畨鍏ㄣ€侀珮鏁堛€佸彲鎵╁睍鐨勫疄鏃堕€氫俊绯荤粺锛屼负鐢ㄦ埛鎻愪緵浼樿川鐨勯€氳浣撻獙銆?/p>
文章来自《钓虾网小编|www.jnqjk.cn》整理于网络,文章内容不代表本站立场,转载请注明出处。