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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容