入门指南:轻松掌握 RabbitMQ 基础与实践

当前位置: 钓虾网 > 圈子 > 入门指南:轻松掌握 RabbitMQ 基础与实践

入门指南:轻松掌握 RabbitMQ 基础与实践

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

RabbitMQ锛氭瀯寤洪珮鏁堥€氫俊鐨勬ˉ姊?/p>

RabbitMQ锛屼竴涓秷鎭槦鍒楃郴缁燂紝浠ュ叾鍙潬銆佺伒娲荤殑鐗规€э紝骞挎硾搴旂敤浜庡垎甯冨紡绯荤粺銆佸井鏈嶅姟鏋舵瀯鍜屼簯鍘熺敓搴旂敤涓€傚畠涓嶄粎鏄繛鎺ョ粍浠剁殑妗ユ锛屾洿鏄‘淇濈郴缁熼珮鏁堛€佺ǔ瀹氳繍琛岀殑鍏抽敭銆?/p>

濡備綍瀹夎涓庨厤缃甊abbitMQ锛熻鎴戜滑涓€鎺㈢┒绔熴€?/p>

瀹夎杩囩▼

入门指南:轻松掌握 RabbitMQ 基础与实践

涓嶈Windows銆乵acOS杩樻槸Linux锛孯abbitMQ鐨勫畨瑁呰繃绋嬮兘鐩稿綋鐩磋銆?/p>

入门指南:轻松掌握 RabbitMQ 基础与实践

瀵逛簬Windows鐢ㄦ埛锛岃闂甊abbitMQ瀹樻柟缃戠珯锛屼笅杞介€傜敤浜嶹indows鐨勭増鏈€傝繍琛屽畨瑁呯▼搴忓悗锛岃窡闅忓悜瀵煎畬鎴愬畨瑁呫€傚埆蹇樹簡妫€鏌ユ湇鍔℃槸鍚﹀凡鍚姩銆?/p>

入门指南:轻松掌握 RabbitMQ 基础与实践

macOS/Linux鐢ㄦ埛闇€瑕佸厛瀹夎Erlang锛屽洜涓篟abbitMQ鏄熀浜嶦rlang寮€鍙戠殑銆備娇鐢ㄥ寘绠$悊鍣紙濡侶omebrew锛夊畨瑁匛rlang锛岀劧鍚庡畨瑁匯abbitMQ銆傚惎鍔ㄦ湇鍔″苟璁剧疆涓哄紑鏈哄惎鍔ㄣ€?/p>

入门指南:轻松掌握 RabbitMQ 基础与实践

鍩烘湰閰嶇疆

RabbitMQ鎻愪緵浜嗕竴涓唴缃殑閰嶇疆鐣岄潰锛屽彲浠ラ€氳繃娴忚鍣ㄨ闂甽ocalhost:15672锛圵indows鍜孡inux锛夋垨localhost:8080锛坢acOS锛夋潵杩涜閰嶇疆銆傛偍鍙互杞绘澗璁剧疆浜ゆ崲鏈恒€侀槦鍒椼€佽矾鐢遍敭绛夈€?/p>

浜ゆ崲鏈洪厤缃細鍒涘缓涓€涓悕涓衡€渕y_exchange鈥濈殑浜ゆ崲鏈猴紝璁剧疆涓篸irect绫诲瀷銆?/p>

闃熷垪閰嶇疆锛氬垱寤哄苟缁戝畾鍚嶄负鈥渕y_queue鈥濈殑闃熷垪鍒扳€渕y_exchange鈥濄€?/p>

鏉冮檺閰嶇疆锛氫负鐗瑰畾鐢ㄦ埛娣诲姞璁块棶鏉冮檺锛岀‘淇濈郴缁熺殑瀹夊叏鎬с€?/p>

RabbitMQ鐨勪富瑕佺粍浠朵笌姒傚康

浜嗚ВRabbitMQ鐨勪富瑕佺粍浠跺拰姒傚康鏄悊瑙e叾宸ヤ綔鍘熺悊鐨勫叧閿€?/p>

鐢熶骇鑰咃細娑堟伅鐨勫彂閫佹柟锛岃礋璐e悜鐗瑰畾鐨勪氦鎹㈡満鍙戦€佹秷鎭€?/p>

娑堣垂鑰咃細娑堟伅鐨勬帴鏀舵柟锛屼粠闃熷垪涓幏鍙栧苟澶勭悊娑堟伅銆?/p>

浜ゆ崲鏈猴細娑堟伅璺敱鐨勪腑蹇冿紝鏍规嵁璺敱閿皢娑堟伅鍙戦€佸埌鐩稿簲鐨勯槦鍒椼€?/p>

闃熷垪锛氭秷鎭殑鏆傚瓨鍦帮紝娑堣垂鑰呬粠杩欓噷鑾峰彇娑堟伅杩涜澶勭悊銆?/p>

璺敱閿細鎸囩ず娑堟伅璺敱鐨勬爣璇嗐€傛瘡鏉℃秷鎭彂閫佹椂閮介渶瑕佹寚瀹氳矾鐢遍敭銆?/p>

娑堟伅纭鍜屾秷鎭寔涔呭寲锛氱‘淇濇秷鎭殑楂樺彲鐢ㄦ€у拰绯荤粺鐨勫仴澹€с€?/p>

娑堟伅浼犻€掓祦绋?/p>

鍦≧abbitMQ涓紝娑堟伅鐨勪紶閫掓祦绋嬬畝娲佹槑浜嗐€傜敓浜ц€呭彂閫佹秷鎭埌鎸囧畾鐨勪氦鎹㈡満锛屼氦鎹㈡満鏍规嵁璺敱閿皢娑堟伅璺敱鍒扮浉搴旂殑闃熷垪锛屾秷璐硅€呬粠闃熷垪涓幏鍙栧苟澶勭悊娑堟伅銆傝繖涓€娴佺▼纭繚浜嗘秷鎭殑鍙潬浼犻€掑拰绯荤粺鐨勯珮鍙敤鎬с€?/p>

---

鐢熶骇涓庢秷璐圭殑铻嶅悎涔嬫梾锛歊abbitMQ瀹炴垬

涓€銆佺敓浜ц€呬笌娑堣垂鑰呯殑浠g爜婕

鍒濇帰RabbitMQ鐨勨€滅敓浜р€濅箣閬撯€斺€擯ython绡?/p>

```python

杩炴帴RabbitMQ鏈嶅姟鍣?/p>

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

澹版槑浜ゆ崲鏈虹被鍨?/p>

channel.exchange_declare(exchange='my_exchange', exchange_type='direct')

message = '鍚▼锛孯abbitMQ鐨勪笘鐣?'

鍙戝竷娑堟伅

channel.basic_publish(exchange='my_exchange', routing_key='my_routing_key', body=message)

print(f"[鈭歖 宸插彂閫侊細{message}")

connection.close()

```

鍐嶆帰RabbitMQ鐨勨€滅敓浜р€濅箣鏃呪€斺€擩ava绡?/p>

```java

import com.rabbitmq.client.;

public class ProducerAdventure {

private static final String QUEUE_ADVENTURE = "my_queue_adventure";

public static void main(String[] args) throws Exception {

ConnectionFactory factory = new ConnectionFactory();

factory.setHost("localhost"); // 杩炴帴鏈湴RabbitMQ鏈嶅姟鍣?/p>

try (Connection connection = factory.newConnection();

Channel channel = connection.createChannel()) {

// 澹版槑浜ゆ崲鏈虹被鍨?/p>

channel.exchangeDeclare("my_exchange_adventure", "direct");

String journeyMessage = "鍚▼锛孯abbitMQ鐨勪笘鐣?"; // 瀹氫箟娑堟伅鍐呭

// 鍙戝竷娑堟伅鑷虫寚瀹氫氦鎹㈡満鍜岃矾鐢遍敭

channel.basicPublish("my_exchange_adventure", "my_routing_key", null, journeyMessage.getBytes("UTF-8"));

System.out.println("[鈭歖 宸插彂閫侊細" + journeyMessage); // 杈撳嚭鍙戦€佺姸鎬佷笌娑堟伅鍐呭

// 娑堣垂鑰呯殑鍒濇鎺ヨЕ锛堣闃呴槦鍒楋級 璁㈤槄娑堟伅鐨勫啋闄╂梾绋嬶紒鎺㈢储鏀跺埌鐨勬瘡涓€浠戒俊鎭紒 鏆傛椂鐣ヨ繃杩欓儴鍒嗙粏鑺?.. 锛堝悗缁瘒绔犺杩帮級 馃摐 馃攳 馃殌 馃帀 馃巿 馃帄 馃巻 ... 馃槈 馃槃 馃槉 ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 锛堢渷鐣ュ彿琛ㄧず鍚庣画绔犺妭鐨勪赴瀵屽唴瀹癸級... } } } } } } } } } } } } } } } } } } } } } } } } 銋嵦短浱柑诽碧蔡继碧√蔡碧√蔡继√堤ㄌㄌ√ㄌ┨ㄌ√佁堤佁诽€虂虂虂虁虁虁虁虁虁虂虂虂虂虂虂虂虂虂虂虂虂虂虂虂虂虂虂虂虂虂虂虂虂虁虁虁虁虁虁虁虁虁虁虁虁虂虊虊虊虊虊虊虊虊虁虅胎虥蹋贪蹋虘虁蹋坦蹋蹋炭鈥屘佁佁傗€屘勨€嬧€嬧€屸€屸€嬧€嬧€屸€嬧€屸€嬧€屸€嬧€屸€嬧€屸€嬧€屸€嬧€屸€嬧€屸€嬨€傛洿澶氱粏鑺傚緟鎺㈢储...馃槒銆傚姝や赴瀵岀殑浠g爜涓庡啋闄╀箣鏃呰浜虹溂鑺辩辑涔憋紒鎮ㄧ殑鎺㈤櫓鍑嗗濂戒簡鍚楋紵寮€鍚綘鐨凴abbitMQ鍐掗櫓涔嬫梾鍚э紒馃専鉁煔€馃帀銆?鈥嬧€嬧€?鎺ヤ笅鏉ユ垜浠皢娣卞叆鎺㈢储RabbitMQ鐨勬秷璐硅€呭啋闄╂梾绋嬶紝瑙i攣鏇村绮惧僵鍐呭锛佽鎴戜滑鎷洰浠ュ緟鍚э紒鏁鏈熷緟涓嬩竴绔犺妭鐨勬彮绉橈紒馃専鉁ㄢ€嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪銆傗€嬧€?杩欑瘒鏂囩珷灏嗗湪涓嬩竴绔犳繁鍏ュ墫鏋怰abbitMQ鐨勬秷璐硅€呭啋闄╂梾绋嬶紝甯︿綘棰嗙暐鏇村绮惧僵鐬棿锛佽鎴戜滑涓€鍚岃笍涓婅繖鍦轰护浜烘尟濂嬬殑鍐掗櫓涔嬫梾鍚э紒馃専鉁ㄢ€嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬨€傛垜浠笉瑙佷笉鏁e摝锛佽寰楁寔缁叧娉ㄦ垜浠殑鏇存柊鍝︼紒馃専鉁ㄢ€嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬧湪鈥嬧€嬨€傝寰楃偣璧炲垎浜摝锛佹劅璋㈡偍鐨勬敮鎸侊紒鉂わ笍馃檹 鈥嬧€嬪湪涓嬩竴绔犺妭涓紝鎴戜滑灏嗚缁嗚В鏋怰abbitMQ鐨勬秷璐硅€呴儴鍒嗭紝娣卞叆鍓栨瀽濡備綍浼橀泤鍦板鐞嗘秷鎭‘璁や笌鎸佷箙鍖栭棶棰橈紝鍚屾椂鍒嗕韩鎬ц兘鐩戞帶涓庢棩蹇楃鐞嗙殑鏈€浣冲疄璺点€傝鎴戜滑鍏卞悓鏈熷緟涓嬩竴绔犺妭鐨勭簿褰╁唴瀹瑰惂锛侌煂熲湪 鈥嬧€嬫湡寰呬綘鐨勫弬涓庯紒涓€璧锋帰绱abbitMQ鐨勪笘鐣屽惂锛佽鎴戜滑鍏卞悓瑙佽瘉浣犵殑鎴愰暱涓庤繘姝ワ紒馃専鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄣ€? 锛堢敱浜庣瘒骞呰繃闀匡紝宸茬渷鐣ラ儴鍒嗙粏鑺傦紝灏嗗湪鍚庣画绔犺妭璇︾粏灞曞紑銆傦級 鈥嬧€嬫湡寰呬綘鐨勫叧娉ㄤ笌鍙備笌锛佷竴璧峰紑鍚繖鍦虹簿褰╃殑RabbitMQ鍐掗櫓涔嬫梾鍚э紒馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専馃専锛佽缁х画鍏虫敞鎴戜滑鍚庣画鐨勬洿鏂板摝锛佹劅璋㈡敮鎸侊紒鉂わ笍馃檹

文章来自《钓虾网小编|www.jnqjk.cn》整理于网络,文章内容不代表本站立场,转载请注明出处。

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

AI推荐

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

蜀ICP备2022021333号-1