Spring Boot AMQP-RabbitMQ

AMQP(advanced message queuing protocol)高級消息隊列協議是一個異步消息傳遞所使用的應用層協議規范。與JMS本質區別:AMQP是一種協議,更準確的說是一種binary wire-level protocol(鏈接協議)。JMS 是JAVA應用提供統一的消息操作(Java API)的;AMQP不從API 與語言平臺層進行限定,而是直接定義網絡交換的數據格式。這使得實現了AMQP的provider天然性就是跨平臺的。意味著我們可以使用Java的AMQP provider,同時使用一個Python的producer的consumer。從這一點看,AQMP可以用HTTP來進行類比,不關心實現的語言,只關心數據格式去發送報文請求。

本例演示由Java 消息生產者,Python消息消費者,實現了AMQP的跨平臺的特性,也只是演示了基礎的 queue,其它詳情請參考官網。

RabbitMQ 下載安裝:

  1. http://www.erlang.org/downloads (Erlang )
  2. http://www.rabbitmq.com/download.html (RabbitMQ 下載)
    請根據自身的系統環境下載對應的安裝包,安裝過程就不在詳細描述,如我本機下載后的: rabbitmq-server-3.6.8.exe 、otp_win64_17.5.exe
    瀏覽器輸入URL: http://localhost:15672/ ,輸入默認帳號/密碼:guest/guest,我本地增加帳號/密碼:admin/secret 與設置virtual-host=test
    RabbitMQ

application.properties加入配置

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=secret
spring.rabbitmq.virtual-host=test

AMQP JAVA消息生產者: RabbitMqProducter.java

package com.spring.boot.amqp.producter;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Description: AMQP  JAVA消息生產者
 * author: 慢慢來了
 * date:  2017/3/31 11:38
 */
@Component
public class RabbitMqProducter {

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendMsg(String content){
        amqpTemplate.convertAndSend("test" , content);
    }
}

JAVA 單元測試
package com.spring.boot.jms;
import com.spring.boot.amqp.producter.RabbitMqProducter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;



@RunWith(SpringRunner.class)
@SpringBootTest
public class SpirngBootJmsApplicationTests {

    @Autowired
    private RabbitMqProducter rabbitMqProducter;

    @Test
    public void contextLoads(){
        rabbitMqProducter.sendMsg("hello rabbtimq");
    }


}

運行后:

Queue test

直接通過 Get Message查看消息如下:


Get Message
Python 消息消費者

環境配置,安裝pika包

 pip install pika
Collecting pika
  Downloading pika-0.10.0-py2.py3-none-any.whl (92kB)
    100% |████████████████████████████████| 102kB 127kB/s
Installing collected packages: pika
Successfully installed pika-0.10.0

編寫Python 消息消費者: receive.py

#!/usr/bin/env python
# date:  2017/3/31 14:35
# auth : 慢慢來了
import pika

# ***********************消息費**************************
# 登錄帳戶名
credential = pika.PlainCredentials('admin' , 'secret')

# 連接至rabbitmq 服務器
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', port=5672, virtual_host='test',credentials = credential))

# 連接通道
channel = connection.channel()

#  聲明傳遞消息隊列
channel.queue_declare(queue='abc')

#告訴rabbitmq使用callback來接收信息  no_ack=True表示在回調函數中不需要發送確認標識
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)

channel.basic_consume(callback , queue='test' , no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
# 隊列里有信息才會調用callback進行處理。按ctrl+c退出
channel.start_consuming()

結果:

receive

RabbitMQ:

RabbitMQConsumers

參考資料:

  1. http://www.erlang.org/docs
  1. http://www.rabbitmq.com/documentation.html
  2. http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/
  3. https://www.rabbitmq.com/tutorials/tutorial-one-python.html
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容