鐮佸啘鎵嬭 | Spring WebFlux瀹炴垬浠ュ強鍘熺悊娴呮瀽
Posted 鏋侀摼绉戞妧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鐮佸啘鎵嬭 | Spring WebFlux瀹炴垬浠ュ強鍘熺悊娴呮瀽相关的知识,希望对你有一定的参考价值。
鏍忕洰馃摪锛?span>鐮佸啘鎵嬭 馃捇
鎾版枃鉁嶐煆伙細鏋侀摼绉戞妧瑙嗚仈缃戞妧鏈腑蹇?/span>寮犲枂闂?/span>
缂栬緫馃摎锛?/span>鏋滄灉
鍏抽敭璇?/span>馃搶锛?/span> WebFlux 銆?/strong> Spring 5 銆?/strong> WebMVC 銆?/strong> 鍙嶅簲寮忕紪绋?nbsp;
灏嗕細閭€璇?/span>
Spring 5鏄祦琛岀殑spring妗嗘灦鐨勪笅涓€涓噸澶х殑鐗堟湰鍗囩骇銆?/span>spring5 涓渶閲嶈鏀瑰姩鏄妸鍙嶅簲寮忕紪绋嬬殑鎬濇兂搴旂敤鍒颁簡妗嗘灦鐨勫悇涓柟闈紝spring 5鐨勫弽搴斿紡缂栫▼浠eactor搴撲负鍩虹銆?/span>
Spring 5涓璚ebFlux 妯″潡鐨勫悕绉版槸 spring-webflux锛屽悕绉颁腑鐨?Flux 鏉ユ簮浜?Reactor 涓殑绫?Flux銆?/span>璇ユā鍧椾腑鍖呭惈浜嗗鍙嶅簲寮?HTTP銆佹湇鍔″櫒鎺ㄩ€佷簨浠跺拰 WebSocket鐨勫鎴风鍜屾湇鍔″櫒绔殑鏀寔銆?/span>瀵逛簬寮€鍙戜汉鍛樻潵璇达紝姣旇緝閲嶈鐨勬槸鏈嶅姟鍣ㄧ鐨勫紑鍙戙€?/span>
鍦ㄦ湇鍔″櫒绔紝WebFlux 鏀寔涓ょ涓嶅悓鐨勭紪绋嬫ā鍨嬶細绗竴绉嶆槸 Spring MVC 涓娇鐢ㄧ殑鍩轰簬 Java 娉ㄨВ鐨勬柟寮忥紱绗簩绉嶆槸鍩轰簬 Java 8 鐨?lambda 琛ㄨ揪寮忕殑鍑芥暟寮忕紪绋嬫ā鍨嬨€?/span>杩欎袱绉嶇紪绋嬫ā鍨嬪彧鏄湪浠g爜缂栧啓鏂瑰紡涓婂瓨鍦ㄤ笉鍚屻€?/span>瀹冧滑杩愯鍦ㄥ悓鏍风殑鍙嶅簲寮忓簳灞傛灦鏋勪箣涓婏紝鍥犳鍦ㄨ繍琛屾椂鏄浉鍚岀殑銆?/span>
WebFlux 闇€瑕佸簳灞傛彁渚涜繍琛屾椂鐨勬敮鎸侊紝WebFlux 鍙互杩愯鍦ㄦ敮鎸?Servlet 3.1 闈為樆濉?IO API 鐨?Servlet 瀹瑰櫒涓?/strong>锛屾垨鏄叾浠栧紓姝ヨ繍琛屾椂鐜锛屽 Netty 鍜?Undertow銆?/span>
鍙嶅簲寮忕紪绋?/span>(Reactive Programming)杩欑鏂扮殑缂栫▼鑼冨紡瓒婃潵瓒婂彈鍒板紑鍙戜汉鍛樼殑娆㈣繋銆?/span>
# 鍙嶅簲寮忕紪绋嬩粙缁?nbsp;
鍙嶅簲寮忕紪绋嬫潵婧愪簬鏁版嵁娴佸拰鍙樺寲鐨勪紶鎾紝鎰忓懗鐫€鐢卞簳灞傜殑鎵ц妯″瀷璐熻矗閫氳繃鏁版嵁娴佹潵鑷姩浼犳挱鍙樺寲銆?/span>姣斿姹傚€间竴涓畝鍗曠殑琛ㄨ揪寮廲=a+b锛屽綋a鎴栬€卋鐨勫€煎彂鐢熷彉鍖栨椂锛屼紶缁熺殑缂栫▼鑼冨紡闇€瑕佸a+b杩涜閲嶆柊璁$畻鏉ュ緱鍒癱鐨勫€笺€?/span>
濡傛灉浣跨敤鍙嶅簲寮忕紪绋嬶紝褰揳鎴栬€卋鐨勫€煎彂鐢熷彉鍖栨椂锛宑鐨勫€间細鑷姩鏇存柊銆?/span>
鍦ㄧ粡鍘嗕簡涓€浜涘弽搴斿紡缂栫▼瀹炶返鐨勫熀纭€涓婁骇鐢熶簡鍚庢潵鐨?span>鍙嶅簲寮忔祦锛圧eactive Streams锛夎鑼?/strong>銆?/span>璇ヨ鑼冨畾涔変簡鍙嶅簲寮忔祦鐨勭浉鍏虫帴鍙o紝骞堕泦鎴愬埌浜唈ava 9涓€?/span>
鍦ㄤ紶缁熺殑缂栫▼鑼冨紡涓紝鎴戜滑涓€鑸?span>閫氳繃杩唬鍣?Iterator)妯″紡鏉ラ亶鍘嗕竴涓簭鍒?/strong>銆?/span>杩欑閬嶅巻鏂瑰紡鏄敱璋冪敤鑰呮潵鎺у埗鑺傚鐨勶紝閲囩敤鐨勬槸鎷夌殑鏂瑰紡銆?/span>姣忔鐢辫皟鐢ㄨ€呴€氳繃next()鏂规硶鏉ヨ幏鍙栧簭鍒椾腑鐨勪笅涓€涓€笺€?/span>浣跨敤鍙嶅簲寮忔祦鏃堕噰鐢ㄧ殑鍒欐槸鎺ㄧ殑鏂瑰紡锛屽嵆甯歌鐨勫彂甯冭€?璁㈤槄鑰呮ā寮忋€?/span>褰撳彂甯冭€呮湁鏂扮殑鏁版嵁浜х敓鏃讹紝杩欎簺鏁版嵁浼氳鎺ㄩ€佸埌璁㈤槄鑰呮潵杩涜澶勭悊銆?/span>鍦ㄥ弽搴斿紡娴佷笂鍙互娣诲姞鍚勭涓嶅悓鐨勬搷浣滄潵瀵规暟鎹繘琛屽鐞嗭紝褰㈡垚鏁版嵁閾?/span>銆?/span>杩欎釜浠ュ0鏄庡紡鐨勬柟寮忔坊鍔犵殑澶勭悊閾惧彧鍦ㄨ闃呰€呰繘琛岃闃呮搷浣滄椂鎵嶄細鐪熸鎵ц銆?/span>
鍙嶅簲寮忔祦涓涓€涓噸瑕佹蹇靛氨鏄?span>璐熷帇(backpressure)銆?/span>鍦ㄥ熀鏈殑娑堟伅鎺ㄩ€佹ā寮忎腑锛屽綋娑堟伅鍙戝竷鑰呬骇鐢熸暟鎹殑閫熷害杩囧揩鏃讹紝浼氫娇寰楁秷鎭闃呰€呯殑澶勭悊閫熷害鏃犳硶璺熶笂浜х敓鐨勯€熷害锛屼粠鑰岀粰璁㈤槄鑰呴€犳垚寰堝ぇ鐨勫帇鍔涖€?/span>褰撳帇鍔涜繃澶ф椂锛屾湁鍙兘閫犳垚璁㈤槄鑰呮湰韬殑濂旀簝锛屾墍浜х敓鐨勭骇鑱旀晥搴旂敋鑷冲彲鑳介€犳垚鏁翠釜绯荤粺鐨勭槴鐥€?/span>璐熷帇鐨勪綔鐢ㄥ湪浜庢彁渚涗竴绉嶄粠璁㈤槄鑰呭埌鐢熶骇鑰呯殑鍙嶉娓犻亾銆?/span>璁㈤槄鑰呭彲浠ラ€氳繃request()鏂规硶鏉ュ0鏄庡叾涓€娆℃墍鑳藉鐞嗙殑娑堟伅鏁伴噺锛岃€岀敓浜ц€呭氨鍙細浜х敓鐩稿簲鏁伴噺鐨勬秷鎭紝鐩村埌涓嬩竴娆equest()鏂规硶璋冪敤銆?/span>杩欏疄闄呬笂鍙樻垚浜嗘帹鎷夌粨鍚堢殑妯″紡銆?/span>
# Reactor 绠€浠?nbsp;
Reactor瀹屽叏鍩轰簬鍙嶅簲寮忔祦瑙勮寖璁捐鍜屽疄鐜扮殑搴撱€?/span>Reactor涔熸槸Spring 5涓弽搴斿紡缂栫▼鐨勫熀纭€銆?/span>瀛︿範鍜屾帉鎻eactor鍙互鏇村ソ浣庣悊瑙pring 5涓殑鐩稿叧姒傚康銆?/span>
鍦↗ava绋嬪簭涓娇鐢≧eactor搴撻潪甯哥殑绠€鍗曪紝鍙渶瑕侀€氳繃Maven鎴朑radle鏉ユ坊鍔爄o.projectreactor:reactor-core鐨勪緷璧栧嵆鍙€?/span>
# Flux 鍜?Mono
Flux鍜孧ono鏄疪eactor涓殑涓や釜鍩烘湰姒傚康銆?/span>Flux琛ㄧず鐨勬槸鍖呭惈0鍒癗涓厓绱犵殑寮傛搴忓垪銆?/span>鍦ㄨ搴忓垪涓彲浠ュ寘鍚笁绉嶄笉鍚岀被鍨嬬殑娑堟伅閫氱煡锛?/span>姝e父鐨勫寘鍚厓绱犵殑娑堟伅銆佸簭鍒楃粨鏉熺殑娑堟伅鍜屽簭鍒楀嚭閿欑殑娑堟伅銆?/span>褰撴秷鎭€氱煡浜х敓鏃讹紝璁㈤槄鑰呬腑瀵瑰簲鐨勬柟娉昽nNext(),onComplete()鍜宱nError()浼氳璋冪敤銆?/span>Mono琛ㄧず鐨勬槸鍖呭惈0鎴栬€?涓厓绱犵殑寮傛搴忓垪銆?/span>璇ュ簭鍒椾腑鍚屾牱鍙互鍖呭惈浜巉lux鐩稿悓鐨勪笁绉嶇被鍨嬬殑娑堟伅閫氱煡銆?/span>Flux鍜孧ono涔嬮棿鍙互杩涜杞崲銆?/span>瀵逛竴涓狥lux搴忓垪杩涜璁℃暟鎿嶄綔锛屽緱鍒扮殑缁撴灉鏄竴涓狹ono<Long>瀵硅薄銆?/span>鎶婁袱涓狹ono搴忓垪鍚堝苟鍒颁竴璧凤紝寰楀埌鐨勬槸涓€涓狥lux瀵硅薄銆?/span>
# 鍙嶅簲寮忕紪绋嬪皬缁?nbsp;
鍙嶅簲寮忕紪绋嬭寖寮忎负寮€鍙戦珮鎬ц兘web搴旂敤甯︽潵浜嗘柊鐨勬満浼氬拰鎸戞垬銆?/span>瀵逛簬澶嶆潅鐨勫簲鐢ㄦ潵璇达紝鍙嶅簲寮忕紪绋嬪拰璐熷帇鐨勪紭鍔夸細浣撶幇鍑烘潵锛屽彲浠ュ甫鏉ユ暣浣撶殑鎬ц兘鐨勬彁鍗?/span>銆?/span>spring5鐨剋ebflux妯″潡鍙互浣滀负寮€鍙戝弽搴斿紡web搴旂敤鐨勫熀纭€銆?/span>鐢变簬spring妗嗘灦鐨勬祦琛岋紝webflux浼氭垚涓哄紑鍙憌eb搴旂敤鐨勯噸瑕佽秼鍔夸箣涓€銆?/span>
webflux鍐呴儴浣跨敤鐨勬槸鍝嶅簲寮忕紪绋?Reactive Programming)锛屼互Reactor搴撲负鍩虹锛屽熀浜庡紓姝ュ拰浜嬩欢椹卞姩锛屽彲浠ヨ鎴戜滑鍦ㄤ笉鎵╁厖纭欢璧勬簮鐨勫墠鎻愪笅锛屾彁鍗囩郴缁熺殑鍚炲悙閲忓拰浼哥缉鎬с€?/span>
浣嗘槸锛?span>webflux骞朵笉鑳戒娇鎺ュ彛鐨勫搷搴旀椂闂寸缉鐭紝瀹冧粎浠呰兘澶熸彁鍗囧悶鍚愰噺鍜屼几缂╂€с€?/strong>
1锔忊儯浠嶥ao鍒癈ontroller锛屽叏閮ㄩ兘瑕佹槸Mono鍜孎lux銆?/span>
2锔忊儯鐩墠瀹樻柟鐨勬暟鎹眰Reactive妗嗘灦鍙敮鎸丷edis锛孧ongo绛夊嚑涓紝娌℃湁JDBC锛寃ebflux鏁翠釜涓€濂楅兘闇€瑕佹槸闈為樆濉炵殑銆?/span>
3锔忊儯缂栫▼闅惧害楂橈紝debug鍥伴毦銆?/span>
1锔忊儯鐗瑰埆閫傚悎搴旂敤鍦↖O瀵嗛泦鍨嬫湇鍔′腑锛屾瘮濡傚啓鏂囦欢鐨勬棩蹇楁敹闆嗐€佸井鏈嶅姟缃戝叧杩欐牱鐨勫簲鐢ㄤ腑銆?/span>锛圛O瀵嗛泦鍨嬪寘鎷細纾佺洏IO瀵嗛泦鍨嬶紝缃戠粶IO瀵嗛泦鍨嬶紝浣跨敤寮傛闈為樆濉炲紡缂栫▼妯″瀷锛岃兘澶熸樉钁楀湴鎻愬崌鍚炲悙閲忋€?/span>骞朵笖 瀹樻柟鐨勬帹鑽愪娇鐢∟etty璺慦ebFlux锛?/span>
3锔忊儯濡傛灉浣犵殑浠g爜涓湁浠讳綍闃诲鎿嶄綔锛岃璋ㄦ厧閫夋嫨WebFlux銆?/span>
4锔忊儯WebFlux骞朵笉淇濊瘉搴旂敤鑳借繍琛岀殑鏇村揩锛屼絾鏄畠涓绘墦鐨勬槸妯悜鎵╁睍鍜屼綆鍐呭瓨娑堣€?/strong>锛屽畠鐨勬€ц兘闇€瑕佸湪涓€浜涚壒瀹氱殑鍦烘櫙鎵嶈兘灞曠幇锛屾瘮濡傛參缃戠粶IO鐨勫満鏅€?/span>
# WebMVC v.s. WebFlux
棣栧厛闇€瑕佹寚鍑簑ebflux涓嶆槸spring mvc鐨勬浛浠f柟妗堬紒webflux寮鸿皟鐨勬槸寮傛闈為樆濉烇紝spring mvc寮鸿皟鐨勬槸鍚屾闃诲锛屽鏋滄柟妗堝ぇ閮ㄥ垎鍋忓悜浜庨潪鍚屾锛屽垯spring webflux鎵嶆槸棣栭€夈€?/span>鍙﹀锛屽鏋滀緷璧栦簡澶эゾ闃诲寮忔寔涔呭寲 API 鍜岀綉缁?API锛屽缓璁娇饨?Spring MVC銆?/span>
1锔忊儯宸茬粡浣库饯铴洪潪闃诲鎶€鏈爤锛屽彲浠ヨ€冭檻浣库饯WebFlux銆?/span>
2锔忊儯鎯宠浣库饯 Java 8 Lambda 缁撳悎杞婚噺绾у嚱鏁板紡妗嗘灦锛屽彲浠ヨ€冭檻 WebFlux銆?/span>
WebMVC锛?/span>springMvc鏋勫缓鍦╯ervlet api涔嬩笂锛屼娇鐢ㄧ殑鏄悓姝ラ樆濉炲紡i/o妯″瀷锛屾瘡涓€涓姹傚搴斾竴涓嚎绋嬪幓澶勭悊銆?/span>
WebFlux锛?/span>spring webflux鏄竴涓紓姝ラ潪闃诲寮忕殑web妗嗘灦锛屽畠鑳藉鍏呭垎鍒╃敤澶氭牳cpu鐨勭‖浠惰祫婧愬幓澶勭悊澶ч噺鐨勫苟鍙戣姹傘€?/span>
鐩稿悓鐐癸細
1锔忊儯閮藉彲浠ヤ娇鐢╯pringmvc娉ㄨВ锛屽@Controller锛屾柟渚挎垜浠湪涓や釜web妗嗘灦涓嚜鐢辫浆鎹紱
2锔忊儯閮藉彲浠ヤ娇鐢╰omcat锛宩etty锛寀ndertow servlet瀹瑰櫒锛坰ervlet 3.1+锛?/span>
# WebFlux鐩稿浜嶴pringMVC涓嬬殑浼樼偣
1锔忊儯鍚屾牱鐨勬€ц兘鍦烘櫙娑堣€楃殑璧勬簮鏇村皯
2锔忊儯閫傚悎妯悜鎵╁睍
娉ㄦ剰鐐癸細
1锔忊儯spring mvc鍥犱负鏄娇鐢ㄧ殑鍚屾闃诲寮忥紝鏇存柟渚垮紑鍙戜汉鍛樼紪鍐欏姛鑳斤紝debug娴嬭瘯绛夛紝涓€鑸潵璇达紝濡傛灉spring mvc鑳藉婊¤冻鐨勫満鏅紝灏卞敖閲忎笉瑕佺敤webflux锛?/span>
2锔忊儯webflux榛樿鎯呭喌涓嬩娇鐢╪etty浣滀负鏈嶅姟鍣紱
3锔忊儯webflux鐩墠涓嶆敮鎸乵ysql锛?/span>
4锔忊儯WebFlux闇€瑕?nbsp;闈為樆濉炵殑涓氬姟浠g爜锛屽鏋滈樆濉烇紝闇€瑕佽嚜宸卞紑绾跨▼姹犲幓杩愯锛?/span>
# WebFlux浠€涔堝満鏅笅鍙互鏇挎崲SpringMVC鍛紵
1锔忊儯鎯宠鍐呭瓨鍜岀嚎绋嬫暟杈冨皯鐨勫満鏅?- 鑺傜渷鏈哄櫒璧勬簮锛岀渷閽?/span>
2锔忊儯缃戠粶杈冩參鎴栬€匢O浼氱粡甯稿嚭鐜伴棶棰樼殑鍦烘櫙
浣跨敤杩?Spring MVC 鐨勫悓瀛︿滑锛屽簲璇ュ埌鐭ラ亾 Spring MVC 鐨勫墠绔帶鍒跺櫒鏄?DispatcherServlet, 鑰?WebFlux 鏄?DispatcherHandler锛屽畠瀹炵幇浜?WebHandler 鎺ュ彛锛?/span>
DispatcherHandler绫讳腑澶勭悊璇锋眰鐨?handle 鏂规硶锛?/span>
1锔忊儯ServerWebExchange 瀵硅薄涓斁缃瘡涓€娆?HTTP 璇锋眰鍝嶅簲淇℃伅锛屽寘鎷弬鏁扮瓑锛?/span>
2锔忊儯鍒ゆ柇鏁翠釜鎺ュ彛鏄犲皠 mappings 闆嗗悎鏄惁涓虹┖锛岀┖鍒欏垱寤轰竴涓?Not Found 鐨勯敊璇紱
4锔忊儯璋冪敤鍏蜂綋涓氬姟鏂规硶锛屼篃灏辨槸鎴戜滑瀹氫箟鐨勬帴鍙f柟娉曪紱
5锔忊儯澶勭悊杩斿洖鐨勭粨鏋滐紱
璇︾粏鍘熺悊锛屽鏋滃ぇ瀹舵劅鍏磋叮锛屽彲浠ヤ笅娆℃帴鐫€鍒嗕韩锝?/span>
馃殌
馃憞馃徎鐐瑰嚮銆岄槄璇诲師鏂囥€?/strong>鏌ョ湅鏇村娣卞害鎶€鏈ソ鏂?/span>
以上是关于鐮佸啘鎵嬭 | Spring WebFlux瀹炴垬浠ュ強鍘熺悊娴呮瀽的主要内容,如果未能解决你的问题,请参考以下文章