参考博客:
http://www.cnblogs.com/leocook/p/mq_rabbitmq_0.html (第一篇到第七篇)
http://www.cnblogs.com/ityouknow/p/6120544.html
http://www.cnblogs.com/boshen-hzb/p/6841982.html
第一节:理论
1.1 消息队列
概念:消息传送时的容器
解释:A服务器要给B服务器一条消息,A先将这一条消息放到消息服务器上,B在去消息服务器上读取或者订阅这条消息
1.2 使用场景
异步:发送者不关心消息是否发送成功,只负责发送消息,不获取消息发送的状态(成功或者失败)
同步:发送者关心消息发送的状态,发送完之后等待接收方返回状态码
1.3 角色概念
Broker:即消息队列服务器实体
Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列。
Queue:消息队列载体,每个消息都会被投入到一个或多个队列。
Binding:绑定,它的作用就是把exchange和queue按照路由规则绑定起来。
Routing Key:路由关键字,exchange根据这个关键字进行消息投递。
vhost:虚拟主机,一个broker里可以开设多个vhost,用作不同用户的权限分离。
producer:消息生产者,就是投递消息的程序。
consumer:消息消费者,就是接受消息的程序。
channel:消息通道,在客户端的每个连接里,可建立多个channel,每个channel代表一个会话任务。
1.4 工作过程
生产者
生产者连接到RabbitMQ服务器上,打开一个消息通道(channel);
生产者声明一个消息交换机(exchange),并设置相关属性。
生产者声明一个消息队列(queue),并设置相关属性。
生产者使用routing key在消息交换机(exchange)和消息队列(queue)中建立好绑定关系。
生产者投递消息都消息交换机(exchange)上
生产者关闭消息通道(channel)以及和服务器的连接。
消费者:
exchange接收到消息后,根据消息的key以及设置的binding,进行消息路由,将消息投递到一个或多个消息队列中。
exchange的几种类型
(1). Direct交换机:完全根据key进行投递。
例如,绑定时设置了routing key为abc,客户端提交信息提交信息时只有设置了key为abc的才会投递到队列;
(2).Topic交换机:在key进行模式匹配后进行投递。
例如:符号”#”匹配一个或多个字符,例如"abc.#"可以匹配abc.def.hig
符号”*”匹配一串连续的字母字符,例如”abc.*”只可以匹配”abc.def”。
(3).Fanout交换机:它采取广播模式,消息进来时,将会被投递到与改交换机绑定的所有队列中。
注意:如果消息交换机(exchange)和消息队列(queue)都是持久化的话,那么他们之间的绑定(Binding)也是持久化的。
如果消息交换机和消息队列之间一个持久化、一个非持久化,那么就不允许绑定
1.5 应答机制: 在下边的demo中,你会发现消息只能被消费一次,如果某个消费者在消费消息时,突然奔溃了,这 条消息无法继续被消费,而消息服务器中也没有了这条消息,那么也就意味着这条消息和这个消 费者无法继续完成这条消息的内容. 为保证消息永不丢失,rabbitMQ支持消息应答机制,即消费之接收到消息并完成对应的消息内 容,然后想消息服务器发送一条确认命令,消息服务器才会把这条消息删除掉,如果这个消费者同 消息服务器断开连接,name消息服务器会把发送给这个消费者的消息并且没有返回状态的消 息发送给其他的消费者, 注意:消费者退出后消息将会被重发,但是由于一些未能被确认消息不能被释放,RabbitMQ 将会消耗掉越来越多的内存。我们需要调用一下basicAck方法. 1.6 消息持久化 为保证消息服务器重启或者挂掉了,消息不丢失,我们需要序列化消息 1.声明持久化存储,注意:不能修改已存在的队列的属性 2.
第二节 spring boot 整合rabbitmq简单入门
2.1 概念
生产者:生产消息的客户端
消费者:消费消息或者使用消息客户单
消息队列:你可以理解为排队
2.2 pom文件以及配置文件
pom文件
<!-- Spring Boot 启动父依赖 核心模块,包括自动配置支持、日志和YAML-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependencies>
<!---->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--rabbitMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
application.properties配置文件
server.port=8080
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.virtual-host=/
2.3 简单模式 2.3.1 一对一模式(一个消费者对应一个生产者) 生产者代码
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 一个生产者对应一个消费者:生产者
*/
@Component
public class OneProducer {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
String sendMsg = "hello one2one " + new Date();
System.out.println("one2one 生产者 sendMessage : " + sendMsg);
this.rabbitTemplate.convertAndSend("one2one", sendMsg);
}
}
消费者代码
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 一个生产者对应一个消费者:消费者
*/
@Component
@RabbitListener(queues = "one2one")
public class OneCustomer {
@RabbitHandler
public void process(String hello) {
System.out.println("one2one 消息消费者消费消息 : " + hello);
}
}
配置类代码
/* one2one */
final static String ONE2ONE = "one2one";
@Bean
public Queue queueOne2One() {
return new Queue(ONE2ONE);
}
Controller层代码
import cn.zfs.amqp.simple.one2one.role.OneProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class One2OneController {
@Autowired
private OneProducer oneProducer;
@RequestMapping("/one2one")
public String one2OneSend(){
oneProducer.send();
return "ok";
}
}
2.3.2 一对多模式(一个生产者多个消费者) (后边的代码不在标注是那一层的内容,统一按照配置类,生产者,消费者,Controller层的模式给出)
/* one2many */
final static String ONE2MANY = "one2many";
@Bean
public Queue queueOne2Many(){
return new Queue(ONE2MANY);
}
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 一对多模式:单个消息生产者
*/
@Component
public class One2ManyProducer {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send(String msg) {
String sendMsg = msg + new Date();
System.out.println("one2many 生产者产生的消息 : " + sendMsg);
this.rabbitTemplate.convertAndSend("one2many", sendMsg);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 一对多模式:多个消费者
*/
@Component
@RabbitListener(queues = "one2many")
public class One2ManyCustomer1 {
@RabbitHandler
public void process(String msg) {
System.out.println("one2may 消费者1号 : " + msg);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 一对多模式:多个消费者
*/
@Component
@RabbitListener(queues = "one2many")
public class One2ManyCustomer2 {
@RabbitHandler
public void process(String msg) {
System.out.println("one2may 消费者2号 : " + msg);
}
}
import cn.zfs.amqp.simple.one2many.role.One2ManyProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 一对多模式,一个生产者多个消费者的Controller层
*/
@RestController
public class One2ManyController {
@Autowired
private One2ManyProducer one2ManyProducer;
@RequestMapping("/one2many")
public String one2Many() {
for (int i = 0; i < 10; i++) {
one2ManyProducer.send("one2many 消费者你好 ,----" + i);
}
return "ok";
}
}
注意:在这里运行代码后,你会发现每个消息只能被消费一次 2.3.3 多对多(多个生产者对多个消费者)
/* many2many */
final static String MANY2MANY = "many2many";
@Bean
public Queue queueMany2Many(){
return new Queue(MANY2MANY);
}
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 一对多模式:单个消息生产者
*/
@Component
public class Mnay2ManyProducer2 {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send(String msg) {
String sendMsg = msg + new Date();
System.out.println("one2many 二号生产者产生的消息 : " + sendMsg);
this.rabbitTemplate.convertAndSend("many2many", sendMsg);
}
}
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 一对多模式:单个消息生产者
*/
@Component
public class Mnay2ManyProducer1 {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send(String msg) {
String sendMsg = msg + new Date();
System.out.println("one2many 一号生产者产生的消息 : " + sendMsg);
this.rabbitTemplate.convertAndSend("many2many", sendMsg);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 一对多模式:多个消费者
*/
@Component
@RabbitListener(queues = "many2many")
public class Mnay2ManyCustomer1 {
@RabbitHandler
public void process(String msg) {
System.out.println("many2many 消费者1号 : " + msg);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 一对多模式:多个消费者
*/
@Component
@RabbitListener(queues = "many2many")
public class Mnay2ManyCustomer2 {
@RabbitHandler
public void process(String msg) {
System.out.println("many2many 消费者2号 : " + msg);
}
}
import cn.zfs.amqp.simple.many2many.role.Mnay2ManyProducer1;
import cn.zfs.amqp.simple.many2many.role.Mnay2ManyProducer2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 一对多模式,一个生产者多个消费者的Controller层
*/
@RestController
public class Many2ManyController {
@Autowired
private Mnay2ManyProducer1 mnay2ManyProducer1;
@Autowired
private Mnay2ManyProducer2 mnay2ManyProducer2;
@RequestMapping("/many2many")
public String one2Many() {
for (int i = 0; i < 10; i++) {
mnay2ManyProducer1.send("many2many 消费者你好(一号生产者说) ,----" + i);
mnay2ManyProducer2.send("many2many 消费者你好(二号生产者说) ,----" + i);
}
return "ok";
}
}
2.4 高级一点的东西 2.4.1 传递对象信息(传递的既然是对象,就需要创建一个对象了,这里我就不给出了,自己随便写一个就行(User))
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ObjConfig {
final static String OBJ = "obj";
@Bean
public Queue queue(){
return new Queue(OBJ);
}
}
import cn.zfs.amqp.senior.obj.obj.User;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 对象传输,生产者
*/
@Component
public class ObjProducter {
@Autowired
private RabbitTemplate rabbitTemplate;
public void send() {
User user = new User("张三", "24");
rabbitTemplate.convertAndSend("obj", user);
System.out.println("发送对象数据: " + user.toString());
}
}
import cn.zfs.amqp.senior.obj.obj.User;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 对象的传输,消费者
*/
@Component
@RabbitListener(queues = "obj")
public class ObjCustomer {
@RabbitHandler
public void process(User user) {
System.out.println("消费者 : " + user.toString());
}
}
import cn.zfs.amqp.senior.obj.role.ObjProducter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 对象的发送
*/
@RestController
public class ObjController {
@Autowired
private ObjProducter objProducter;
@RequestMapping("/sendobj")
public String sendobj() {
objProducter.send();
return "ok";
}
}
2.4.2 topic exchange
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TopicEcchangeConfig {
final static String MESSAGE = "topic.message";
final static String MESSAGES = "topic.messages";
@Bean
public Queue queueMessage() {
return new Queue(MESSAGE);
}
@Bean
public Queue queueMessages() {
return new Queue(MESSAGES);
}
@Bean
TopicExchange exchange() {
return new TopicExchange("exchange");
}
/**
* 将队列topic.message与exchange绑定,binding_key为topic.message,就是完全匹配
* @param queueMessage
* @param exchange
* @return
*/
@Bean
Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
}
/**
* 将队列topic.messages与exchange绑定,binding_key为topic.#,模糊匹配
* @param queueMessages
* @param exchange
* @return
*/
@Bean
Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
}
}
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TopicExchangeProducer {
@Autowired
private RabbitTemplate rabbitTemplate;
public void send() {
rabbitTemplate.convertAndSend("exchange", "topic.message", "topic.message");
rabbitTemplate.convertAndSend("exchange", "topic.messages", "topic.messages");
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 消费者message
*/
@Component
@RabbitListener(queues = "topic.message")
public class MessageCustomer {
@RabbitHandler
public void process(String msg){
System.out.println("topicexchange message 消费者 : " +msg);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 消费者messages
*/
@Component
@RabbitListener(queues = "topic.messages")
public class MessagesCustomer {
@RabbitHandler
public void process(String msg){
System.out.println("topicexchange messages 消费者 : " +msg);
}
}
import cn.zfs.amqp.senior.topicexchange.role.TopicExchangeProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TopicExchangeController {
@Autowired
private TopicExchangeProducer topicExchangeProducer;
@RequestMapping("/topicexchange")
public String topicExchange(){
topicExchangeProducer.send();
return "ok";
}
}
2.4.3 Fanout Exchange (广播模式)
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FanoutRabbitConfig {
@Bean
public Queue AMessage() {
return new Queue("fanout.A");
}
@Bean
public Queue BMessage() {
return new Queue("fanout.B");
}
@Bean
public Queue CMessage() {
return new Queue("fanout.C");
}
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
@Bean
Binding bindingExchangeA(Queue AMessage, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(AMessage).to(fanoutExchange);
}
@Bean
Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(BMessage).to(fanoutExchange);
}
@Bean
Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(CMessage).to(fanoutExchange);
}
}
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Fanout Exchange 生产者
*/
@Component
public class FanoutProducer {
@Autowired
private RabbitTemplate rabbitTemplate;
public void send(){
//论证广播模式和routingkey参数无关
rabbitTemplate.convertAndSend("fanoutExchange","abcd.ee","--------------");
rabbitTemplate.convertAndSend("fanoutExchange","abcd.aa","==============");
rabbitTemplate.convertAndSend("fanoutExchange","***************");
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* Fanout Exchange 消费者
*/
@Component
@RabbitListener(queues = "fanout.A")
public class FanoutCustomerA {
@RabbitHandler
public void process(String msg){
System.out.println("FanoutReceiverA : " + msg);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* Fanout Exchange 消费者
*/
@Component
@RabbitListener(queues = "fanout.B")
public class FanoutCustomerB {
@RabbitHandler
public void process(String msg){
System.out.println("FanoutReceiverB : " + msg);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* Fanout Exchange 消费者
*/
@Component
@RabbitListener(queues = "fanout.C")
public class FanoutCustomerC {
@RabbitHandler
public void process(String msg){
System.out.println("FanoutReceiverC : " + msg);
}
}
import cn.zfs.amqp.senior.fanoutexchange.role.FanoutProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FanoutExchangeController {
@Autowired
private FanoutProducer fanoutProducer;
@RequestMapping("/fanoutsend")
public void fanoutSend(){
fanoutProducer.send();
}
}
2.4.4 回调函数
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
/**
* 回调函数配置文件
*/
@Configuration
public class CallbackConfig {
final static String CALLBACK = "callback";
@Bean
public Queue callBackQueue() {
return new Queue(CALLBACK);
}
@Autowired
private ConnectionFactory connectionFactory;
@Bean
/** 因为要设置回调类,所以应是prototype类型,如果是singleton类型,则回调类为最后一次设置 */
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public RabbitTemplate rabbitTemplatenew() {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
return template;
}
}
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 执行回调的生产者
*/
@Component
public class CallBackProducer implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {
@Autowired
private RabbitTemplate rabbitTemplate;
public void send() {
rabbitTemplate.setReturnCallback(this);
rabbitTemplate.setConfirmCallback(this);
rabbitTemplate.convertAndSend("callback", "回调函数:生产者发出的消息");
}
/**
* 发送后的回调函数
*
* @param correlationData
* @param b
* @param s
*/
@Override
public void confirm(CorrelationData correlationData, boolean b, String s) {
System.out.println("回调函数:" + "b=" + b);
}
/**
* 消息发送失败的回调函数(未测试)
*
* @param message
* @param replyCode
* @param replyText
* @param exchange
* @param routingKey
*/
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
System.out.println("发送消息失败");
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "callback")
public class CallBackCustomer {
@RabbitHandler
public void process(String msg){
System.out.println("回调函数-消费者:"+msg);
}
}
import cn.zfs.amqp.senior.callback.role.CallBackProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CallBackController {
@Autowired
private CallBackProducer callBackProducer;
@RequestMapping("/callback")
public void send(){
callBackProducer.send();
}
}
2.4.5 消息持久化 消息持久化,我没有做测试demo,我看了一下Queue的源码,在他的构造方法中有一个参 数durable,为true就可以持久化消息,这个未做验证