参看连接:java
springboot(八):RabbitMQ详解--Author@纯洁的微笑spring
队列服务有三个概念: 发消息者、队列、收消息者。springboot
比较重要的概念有 4 个:虚拟主机,交换机,队列和绑定。bash
交换机的功能主要是接收消息而且转发到绑定的队列,交换机不存储消息,在启用ack模式后,交换机找不到队列会返回错误。交换机有四种类型:Direct, topic, Headers and Fanout服务器
Direct Exchange是RabbitMQ默认的交换机模式,也是最简单的模式,根据key全文匹配去寻找队列。app
第一个 X - Q1 就有一个 binding key,名字为 orange; X - Q2 就有 2 个 binding key,名字为 black 和 green。当消息中的 路由键 和 这个 binding key 对应上的时候,那么就知道了该消息去到哪个队列中。 spring-boot
Topic Exchange 转发消息主要是根据通配符。 在这种交换机下,队列和交换机的绑定会定义一种路由模式,那么,通配符就要在这种路由模式和路由键之间匹配后交换机才能转发消息。 在这种交换机模式下:测试
具体代码发送的时候仍是同样,第一个参数表示交换机,第二个参数表示routing key,第三个参数即消息。以下:this
rabbitTemplate.convertAndSend("testTopicExchange","key1.a.c.key2", " this is RabbitMQ!");
topic 和 direct 相似, 只是匹配上支持了"模式", 在"点分"的 routing_key 形式中, 可使用两个通配符:spa
headers 也是根据规则匹配, 相较于 direct 和 topic 固定地使用 routing_key , headers 则是一个自定义匹配规则的类型. 在队列与交换器绑定时, 会设定一组键值对规则, 消息中也包括一组键值对( headers 属性), 当这些键值对有一对, 或所有匹配时, 消息被投送到对应队列.
Fanout Exchange 消息广播的模式,无论路由键或者是路由模式,会把消息发给绑定给它的所有队列,若是配置了routing_key会被忽略。
经过start.spring.io生成含有spring-boot-starter-amqp依赖的项目,结构以下:
application.properties
spring.application.name=DdoubleRabbit debug=true spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=ddouble spring.rabbitmq.password=Tong2014
RabbitConfig.java
package com.ddouble.DdoubleRabbit.rabbit; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitConfig { @Bean public Queue helloQueue() { return new Queue("hello"); } }
HelloSender.java
package com.ddouble.DdoubleRabbit.rabbit.hello; 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 HelloSender { @Autowired private AmqpTemplate rabbitTemplate; public void send() { String context = "hello" + new Date(); System.out.println("Sender : " + context); this.rabbitTemplate.convertAndSend("hello", context); } }
HelloReceiver.java
package com.ddouble.DdoubleRabbit.rabbit.hello; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues = "hello") public class HelloReceiver { @RabbitHandler public void process(String str) { System.out.println("Receiver : " + str); } }
测试类
package com.ddouble.DdoubleRabbit; import com.ddouble.DdoubleRabbit.rabbit.hello.HelloSender; 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 DdoubleRabbitApplicationTests { @Autowired private HelloSender helloSender; @Test public void contextLoads() { helloSender.send(); } }
控制台打印:
RabbitMQ能够看到一个“hello”的Queue