SpringBoot即时通讯开发教程:从零开始的实践指南

当前位置: 钓虾网 > 圈子 > SpringBoot即时通讯开发教程:从零开始的实践指南

SpringBoot即时通讯开发教程:从零开始的实践指南

2024-11-19 作者:钓虾网 1

---

SpringBoot即时通讯开发教程:从零开始的实践指南

娣卞叆浜嗚ВSpringBoot妗嗘灦鍦ㄥ嵆鏃堕€氳寮€鍙戜腑鐨勫簲鐢?/p>

SpringBoot即时通讯开发教程:从零开始的实践指南

鏈枃鏃ㄥ湪寮曢寮€鍙戣€呰笍涓婂熀浜嶴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

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-security

org.springframework.boot

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》整理于网络,文章内容不代表本站立场,转载请注明出处。

本文链接:https://www.jnqjk.cn/quanzi/161527.html

AI推荐

Copyright 2024 © 钓虾网 XML 币安app官网

蜀ICP备2022021333号-1