Direct模式
引入依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置:
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
然后寫個(gè)配置類:
@Configuration
public class MqConfig {
@Bean
public Queue queue() {
return new Queue("queue");
}
}
然后生產(chǎn)者類:
@Component
public class HelloSender {
@Autowired
private AmqpTemplate template;
public void send() {
template.convertAndSend("queue","Hello,MQ");
}
}
然后再另一個(gè)應(yīng)用程序?qū)懴M(fèi)者類:
@Component
public class HelloReceive {
@RabbitListener(queues="queue") //監(jiān)聽器監(jiān)聽指定的Queue
public void receive(String str) {
System.out.println("Receive:"+str);
}
}
跑生產(chǎn)者程序的測試,消費(fèi)者程序就可以收到了:
@Autowired
private HelloSender helloSender;
@Test
public void testRabbit() {
helloSender.send();
}
默認(rèn)情況下,RabbitMq的虛擬機(jī)是 "/" ,它默認(rèn)有交換機(jī)和路由鍵,交換機(jī)配置類型就是Direct

image.png
而template.convertAndSend("queue","Hello,MQ"),默認(rèn)就是通過默認(rèn)的虛擬機(jī)處理消息,默認(rèn)的虛擬機(jī)會自動(dòng)綁定所有的隊(duì)列,默認(rèn)的隊(duì)列名就是路由鍵Key,可以看到:
/**
* Convert a Java object to an Amqp {@link Message} and send it to a default exchange
* with a specific routing key.
*
* @param routingKey the routing key
* @param message a message to send
* @throws AmqpException if there is a problem
*/
void convertAndSend(String routingKey, Object message) throws AmqpException;
自定義虛擬機(jī)
配置
@Bean
public Queue myQueue() {
return new Queue("myQueue");
}
@Bean
public CustomExchange customExchange(){
return new CustomExchange("CustomExchange",ExchangeTypes.DIRECT);
}
@Bean
Binding customExchangeBinding( Queue myQueue, CustomExchange customExchange) {
return BindingBuilder.bind(myQueue).to(customExchange).with("Apple").noargs();
}
發(fā)送者
@Component
public class HelloSender {
@Autowired
private AmqpTemplate template;
public void send() {
//template.convertAndSend("queue","Hello,MQ");
template.convertAndSend("CustomExchange", "Apple", "Hello,MQ");
}
}
接收者
@Component
public class HelloReceive {
@RabbitListener(queues="myQueue") //監(jiān)聽器監(jiān)聽指定的Queue
public void receive(String str) {
System.out.println("Receive:"+str);
}
}
Topic模式
同樣的首先配置Topic交換機(jī)并綁定隊(duì)列
@Bean
public Queue myTopicQueue() {
return new Queue("myTopicQueue");
}
@Bean
public TopicExchange topicExchange(){
return new TopicExchange("TopicExchange");
}
@Bean
Binding topicExchangeBinding( Queue myTopicQueue, TopicExchange topicExchange) {
return BindingBuilder.bind(myTopicQueue).to(topicExchange).with("Apple.#");
}
然后發(fā)生消息
@Component
public class HelloSender {
@Autowired
private AmqpTemplate template;
public void send() {
//template.convertAndSend("queue","Hello,MQ");
// template.convertAndSend("CustomExchange", "Apple", "Hello,MQ");
template.convertAndSend("TopicExchange", "Apple.666", "Hello,MQ");
}
}
接收消息:
@Component
public class HelloReceive {
@RabbitListener(queues="myTopicQueue") //監(jiān)聽器監(jiān)聽指定的Queue
public void receive(String str) {
System.out.println("Receive:"+str);
}
}
Fanout模式
同樣的套路
@Bean
public Queue myFanoutQueue() {
return new Queue("myFanoutQueue");
}
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("FanoutExchange");
}
@Bean
Binding fanoutExchangeBinding( Queue myFanoutQueue, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(myFanoutQueue).to(fanoutExchange);
}
@Component
public class HelloSender {
@Autowired
private AmqpTemplate template;
public void send() {
template.convertAndSend("FanoutExchange", "", "Hello,MQ");
}
}
@Component
public class HelloReceive {
@RabbitListener(queues="myFanoutQueue") //監(jiān)聽器監(jiān)聽指定的Queue
public void receive(String str) {
System.out.println("Receive:"+str);
}
}