1. مقدمة
هناك مفاهيمان مهمان في الوسيطة الوسيطة: وسيط الرسائل والوجهة. عندما يرسل مرسل الرسالة الرسالة ، يتم الاستيلاء على الرسالة بواسطة وسيط الرسائل ، مما يضمن تسليم الرسالة إلى الوجهة المحددة.
تشمل وسطاء الرسائل الشائعة الاستخدام مواصفات JMS و AMQP. في المقابل ، فإن تطبيقاتها المشتركة هي ActiveMQ و RabbitMQ.
2. دمج ActiveMq
2.1 إضافة تبعيات
<Rependency> <roupeD> org.springframework.boot </rougiD> <intifactid> Spring-boot-starter-activemq </tenfactid> </sependency> <!-إذا كنت بحاجة إلى تكوين تجمعات الاتصال ، أضف التبعيات التالية-> <preadency> </التبعية>
2.2 إضافة التكوين
# activeMq تكوين spring.activemq.broker-url = tcp: //192.168.2.12: 61616spring.activemq.user = adminspring.activemq.password = adminspring.activemq.enabled.endabl ليتم ضبطها على truespring.jms.pub-sub-domain = false
هنا spring.activemq.pool.enabled = خطأ يعني إغلاق تجمع الاتصال.
2.3 الترميز
فئة التكوين:
ConfigurationPublic class jmsconfirguration {public static final string queue_name = "ActiveMq_queue" ؛ Static Final String Topic_Name = "ActiveMq_topic" ؛ Queue public queue () {return new ActiveMqQueue (queue_name) ؛ } Bean Public Topic () {return new ActiveMqTopic (topic_name) ؛ }}مسؤولة عن إنشاء قوائم الانتظار والمواضيع.
منتج الرسائل:
componentpublic class jmssender {Autowired private queue queue ؛ موضوع موضوع خاص Autowired ؛ Autowired private jMsMessagingTemplate jMstemplate ؛ public void sendbyqueue (string message) {this.jmstemplate.convertandsend (Queue ، Message) ؛ } public void sendbytopic (string message) {this.jmstemplate.convertandsend (topic ، message) ؛ }}مستهلك الرسائل:
componentpublic class jmsReceiver {jmsListener (الوجهة = jmsconfirguration.queue_name) public void receptbyqueue (رسالة سلسلة) {system.out.println ("استلام رسالة قائمة الانتظار:" + رسالة) ؛ } jmslistener (الوجهة = jmsconfirguration.topic_name) public void receptbytopic (string message) {system.out.println ("استلام رسالة الموضوع:" + رسالة) ؛ }}يستمع مستهلكو الرسائل إلى الرسائل باستخدام jmslistener repantation.
2.4 اختبار
Runwith (SpringRunner.Class) springboottestpublic class jmstest {autowired private jmssender sender ؛ test public void testSendByqueue () {for (int i = 1 ؛ i <6 ؛ i ++) {this.sender.sendbyqueue ("Hello ActiveMq Queue"+i) ؛ }} test public void testSendByTopic () {for (int i = 1 ؛ i <6 ؛ i ++) {this.sender.sendbytopic ("Hello ActiveMq Topic"+i) ؛ }}}نتيجة الطباعة:
تلقي رسالة قائمة الانتظار: Hello ActiveMq قائمة الانتظار 1
تلقي رسالة قائمة الانتظار: Hello ActiveMq Queue 2
تلقي رسالة قائمة الانتظار: Hello ActiveMq Queue 3
تلقي رسالة قائمة الانتظار: Hello ActiveMQ قائمة الانتظار 4
تلقي رسائل قائمة الانتظار: Hello ActiveMq Queue 5
set spring.jms.pub-sub-domain = true عند اختبار وضع النشر/الاشتراك
تلقي رسالة الموضوع: Hello ActiveMq Topic 1
تلقي رسالة الموضوع: Hello ActiveMq Topic 2
تلقي رسائل الموضوع: Hello ActiveMq Topic 3
تلقي رسائل الموضوع: Hello ActiveMq Topic 4
تلقي رسائل الموضوع: Hello ActiveMq Topic 5
3. دمج الأرنب
3.1 إضافة تبعيات
<Rependency> <roupiD> org.springframework.boot </rougiD> <StifactId> Spring-Boot-Starter-AMQP </stifactid> </sependency>
3.2 إضافة التكوين
spring.RabbitMq.host = 192.168.2.30Spring.RabbitMq.port = 5672Spring.RabbitMq.Username = lightspring.RabbitMq.Password = lightpring.RabbitMq.Virtual-host =/test
3.3 الترميز
فئة التكوين:
ConfigurationPublic Class AMQPConfirguration {// =================================================== static Final String Simple_queue = "Simple_queue" ؛ Queue public queue () {return new Queue (simple_queue ، true) ؛ } // ================== static static Final String ps_queue_1 = "ps_queue_1" ؛ السلسلة النهائية الثابتة العامة ps_queue_2 = "ps_queue_2" ؛ Static Final Final Fanout_exchange = "Fanout_exchange" ؛ @bean public queue psqueue1 () {return new Queue (ps_queue_1 ، true) ؛ } @bean public queue psqueue2 () {return new Queue (ps_queue_2 ، true) ؛ } bean public fanoutexchange fanoutexchange () {return new fanoutexchange (fanout_exchange) ؛ } bean public binding fanoutBinding1 () {return bindingbuilder.bind (psqueue1 ()). إلى (fanoutexchange ()) ؛ } bean public binding fanoutBinding2 () {return bindingbuilder.bind (psqueue2 ()). إلى (fanoutexchange ()) ؛ } // ========================== static Final String Routing_Queue_1 = "Routing_queue_1" ؛ السلسلة النهائية الثابتة العامة ROTING_QUEUE_2 = "Routing_queue_2" ؛ Static Final String Direct_Exchange = "Direct_exchange" ؛ @bean public queue routingqueue1 () {return new Queue (Routing_queue_1 ، true) ؛ } @bean public queue routingqueue2 () {return new Queue (Routing_queue_2 ، true) ؛ } bean public directExchange DirectExchange () {return new DirectExchange (direct_exchange) ؛ } bean public binding directBinding1 () {return bindingbuilder.bind (RoutingQueue1 ()). to (directExchange ()). with ("user") ؛ } bean public binding directBinding2 () {return bindingbuilder.bind (RoutingQueue2 ()). إلى (directExchange ()). مع ("الطلب") ؛ } // =============== MODE ===================== STATIC Final String Topic_Queue_1 = "Topic_queue_1" ؛ Static Final String Topic_Queue_2 = "Topic_queue_2" ؛ Static Final String Topic_Exchange = "Topic_exchange" ؛ @bean public queue topicqueue1 () {return new Queue (topic_queue_1 ، true) ؛ } @bean public queue topicqueue2 () {return new Queue (topic_queue_2 ، true) ؛ } bean public TopicexChange TopicexChange () {return new TopicexChange (topic_exchange) ؛ } bean public binding TopicBinding1 () {return bindingbuilder.bind (topicqueue1 ()). to (topicexChange ()). with ("user.add") ؛ } bean public binding TopicBinding2 () {return bindingbuilder.bind (topicqueue2 ()). to (topicexchange ()). with ("user.#") ؛ }}يحتوي RabbitMQ على أوضاع عمل متعددة ، لذلك هناك العديد من التكوينات. يمكن للقراء الذين يرغبون في معرفة المحتوى ذي الصلة مراجعة "مقدمة في وضع عمل RabbitMQ" أو المعلومات المتعلقة بـ BAIDU بمفردهم.
منتج الرسائل:
componentpublic class amqpsender {autowired private amqptemplate amqptemplate ؛ / ** * وضع بسيط إرسال * * param message */ public void SimplesEnd (رسالة سلسلة) {this.amqptemplate.convertandsend (amqpconfirguration.simple_queue ، message) ؛ }/** * publish/اشتراك وضع إرسال * * param message */public void pssend (string message) {this.amqptemplate.convertandsend (amqpconfirguration.fanout_exchange ، "،" ، message) ؛ } / ** * أرسل في وضع التوجيه * * param رسالة * / public void trougensend (سلسلة RoutingKey ، رسالة السلسلة) {this.amqptemplate.convertandsend (AMQPConfirguration.direct_exchange ، RoutingKey ، Message) ؛ } / ** * إرسال في وضع السمة * * param RoutingKey * param message * / public void touthEd (string routingKey ، string message) {this.amqptemplate.convertandsend (amqpconfirguration.topic_exchange ، routingkey ، message) ؛ }}مستهلك الرسائل:
COMPONENTPublic Class AMQPRECEIVER { / ** * استقبال الوضع البسيط * * param message * / rabbitListener (queues = amqpconfirguration.simple_queue) public void simplereceive (string message) {system.out.println ("استلام الرسالة:" + رسالة) ؛ }/** * استقبال وضع النشر/الاشتراك * * param message */rabbitListener (queues = amqpconfirgurgion.ps_queue_1) psreceive1 (رسالة سلسلة) } @rabbitListener (queues = amqpconfirguration.ps_queue_2) psereceive2 (رسالة السلسلة) {system.out.println (amqpconfirguration.ps_queue_2 + "تلقي رسالة:" + رسالة) ؛ } / ** * استقبال وضع التوجيه * * param message * / rabbitListener (queues = amqpconfirgurgion.routing_queue_1) public void routingreceive1 (سلسلة الرسائل) } @rabbitListener (queues = amqpconfirguration.routing_queue_2) public void RoutingReceive2 (message string) {system.out.println (amqpconfirguration.routing_queue_2 + "تلقي الرسالة:" + رسالة) ؛ } / ** * استقبال وضع الموضوع * * param message * / rabbitListener (queues = amqpconfirguration.topic_queue_1) public void topicreceive1 (سلسلة رسالة) {system.out.println (amqpconfirguration.topic_queue_1 + } @rabbitListener (queues = amqpconfirguration.topic_queue_2) public void topicreceive2 (رسالة سلسلة) {system.out.println (amqpconfirguration.topic_queue_2 + "تلقي رسالة:" + رسالة) ؛ }}يستمع مستهلكو الرسائل إلى الرسائل باستخدام @rabbitlistener تعليق.
3.4 اختبار
Runwith (SpringRunner.Class) springboottestpublic class amqptest {autowired private amqpsender sender ؛ Test public void testSimplesEnd () {for (int i = 1 ؛ i <6 ؛ i ++) {this.sender.simplesend ("test simplesend"+i) ؛ } }Test public void testpssend () {for (int i = 1 ؛ i <6 ؛ i ++) {this.sender.pssend ("test pssend"+i) ؛ } }Test public void testpssend () {for (int i = 1 ؛ i <6 ؛ i ++) {this.sender.pssend ("test pssend"+i) ؛ }} test public void testRoutingSend () {for (int i = 1 ؛ i <6 ؛ i ++) {this.sender.RoutingsEnd ("order" ، "test forming"+i) ؛ }} test public void testTopicsEnd () {for (int i = 1 ؛ i <6 ؛ i ++) {this.sender.topicsend ("user.add" ، "test tebicsend"+i) ؛ }}}يتم تخطي نتائج الاختبار. . .
تذكير 1: تم رفض تسجيل الدخول إلى access_refused باستخدام آلية المصادقة السهل
حل:
1) يرجى التأكد من صحة اسم المستخدم وكلمة المرور. من المهم أن نلاحظ ما إذا كانت قيم اسم المستخدم وكلمة المرور تحتوي على مسافات أو علامات تبويب (قام المؤلف باختبارها لأن كلمة المرور لها حرف علامة تبويب أخرى ، والتي تسببت في فشل المصادقة).
2) إذا كان حساب الاختبار يستخدم الضيف ، فأنت بحاجة إلى تعديل ملف RabbitMQ.Conf. أضف تكوين "loopback_users = none" إلى الملف.
لا يمكن إعداد قائمة انتظار للمستمع. إما أن قائمة الانتظار غير موجودة أو أن الوسيط لن يسمح لنا باستخدامه
حل:
يمكننا تسجيل الدخول إلى واجهة إدارة RabbitMQ وإضافة قائمة الانتظار المقابلة يدويًا في خيار قائمة الانتظار.
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.