1. 自动应答模式
- 第1步,编写application.properties
spring.rabbitmq.host = 127.0.0.1
spring.rabbitmq.port = 5672
spring.rabbitmq.password = test
spring.rabbitmq.username = test
spring.rabbitmq.virtual-host = /test
-第2步,编写RabbitMQ配置
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.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* RabbitMQ配置
*/
@Configuration
public class SpringBootRabbitMQConfiguration {
// ======== 生产者配置 ========
/**
* 声明了一个TopicExchange
*
* @return
*/
@Bean
public TopicExchange testTopicExchange() {
return new TopicExchange("testTopicExchange");
}
// ======== 消费者配置 ========
/**
* 声明了一个Queue
*
* @return
*/
@Bean
public Queue testQueue() {
return new Queue("testQueue");
}
/**
* 声明了另一个Queue
*
* @return
*/
@Bean
public Queue testQueue2() {
return new Queue("testQueue2");
}
// ======== 实际项目中为了解耦,绑定队列是放在RabbitMQ的Web端操作========
/**
* 队列1绑定TopicExchange并关联到RoutingKey
*
* @param testQueue
* @param testTopicExchange
* @return
*/
@Bean
public Binding bindingTestQueueToTestTopicExchange(@Qualifier("testQueue") Queue testQueue, @Qualifier("testTopicExchange") TopicExchange testTopicExchange) {
return BindingBuilder.bind(testQueue).to(testTopicExchange).with("foo.*");
}
/**
* 队列2绑定TopicExchange并关联到RoutingKey
*/
@Bean
public Binding bindingTestQueue2ToTestTopicExchange(@Qualifier("testQueue2") Queue testQueue, @Qualifier("testTopicExchange") TopicExchange testTopicExchange) {
return BindingBuilder.bind(testQueue).to(testTopicExchange).with("foo.bar");
}
}
- 第3步,编写消费者类
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class Consumer {
@RabbitListener(queues = "testQueue")
public void receiveTestQueue(String msg) {
System.out.println("消费者1:" + msg);
}
@RabbitListener(queues = "testQueue2")
public void receiveTestQueue2(String msg) {
System.out.println("消费者2:" + msg);
}
}
- 第4步,编写生产者
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Producer {
@Autowired
private AmqpTemplate amqpTemplate;
/**
* 直接发送消息到Queue
*
* @param msg
*/
public void sendToQueue(String routingKey, String msg) {
// 如果没有经过交换机,routingKey即queue
this.amqpTemplate.convertAndSend(routingKey, msg);
}
/**
* 发送消息到TopicExchange
*
* @param msg
*/
public void sendToTopicExchange(String exchange, String routingKey,String msg) {
this.amqpTemplate.convertAndSend(exchange, routingKey, msg);
}
}
- 第5步,编写测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootRabbitMQTest {
@Autowired
private Producer producer;
/**
* 直接发送消息到Queue
*/
@Test
public void sendToQueue() {
producer.sendToQueue("testQueue", "'hello world' through queue");
}
/**
* 发送消息到TopicExchange
*/
@Test
public void sendToTopicExchange() {
// TopicExchange绑定了2个队列
// - 如果routingKey为foo.bar,两个队列(两个消费者)都会收到消息
// - 如果routingKey为foo.bar2(其它任意标识),只有一个队列(一个消费者)才会收到消息
producer.sendToTopicExchange("testTopicExchange", "foo.bar2", "hello world!!!");
}
}