本文简单的介绍了,在SpringBoot中如何整合RabbitMQ,以及一些常用的方法代码。详细原理等具体内容请阅读👇👇👇“消息队列-RabbitMQ学习笔记(四)”
消息队列-RabbitMQ学习笔记(四)-CSDN博客
1. SpringBoot整合RabbitMQ
1.1. 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
1.2. 配置参数
server:
port: 8132
spring:
rabbitmq:
host: localhost
port: 5672
username: admin
password: 123456
virtual-host: /boot
1.3. Fanout
@Configuration
public class ConsumerFanout {
/**
* 注册交换机
*/
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("fanout_exchange", true, false);
}
/**
* 注册队列
*
* @return Queue
*/
@Bean
public Queue fanoutQueue1() {
return new Queue("fanout_queue1", true, false, false);
}
@Bean
public Queue fanoutQueue2() {
return new Queue("fanout_queue2", true, false, false);
}
/**
* 绑定队列到交换机
*/
@Bean
public Binding fanoutBind1() {
return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
}
@Bean
public Binding fanoutBind2() {
return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange());
}
/**
* 消费消息
*/
@RabbitListener(queues = "fanout_queue1")
public void process1(String msg) {
System.out.println("fanout_queue1 接收到消息:" + msg);
}
@RabbitListener(queues = "fanout_queue2")
public void process2(String msg) {
System.out.println("fanout_queue2 接收到消息:" + msg);
}
}
@Configuration
public class ProducerFanout {
@Resource
private RabbitTemplate rabbitTemplate;
public void send() {
String msg = "fanout message";
rabbitTemplate.convertAndSend("fanout_exchange", "",msg);
}
}
1.4. Direct
@Configuration
public class ConsumerDirect {
/**
* 注册交换机
*/
@Bean
public DirectExchange directExchange() {
return new DirectExchange("directExchange", true, false);
}
/**
* 注册队列
*/
@Bean
public Queue directQueue() {
return new Queue("directQueue", true, false, false);
}
/**
* 绑定交换机和队列
*/
@Bean
public Binding bind() {
return BindingBuilder.bind(directQueue()).to(directExchange()).with("direct");
}
/**
* 消费消息
*/
@RabbitListener(queues = "directQueue")
public void process(String msg) {
System.out.println("directQueue接收到消息:" + msg);
}
}
@Configuration
public class ProducerDirect {
@Resource
private RabbitTemplate rabbitTemplate;
/**
* 发送消息
*/
public void send() {
String msg = "hello lz";
rabbitTemplate.convertAndSend("directExchange", "direct", msg);
}
}
1.5. Topic
@Configuration
public class ConsumerTopic {
/**
* 注册交换机
*/
@Bean
public TopicExchange topicExchange() {
return ExchangeBuilder.topicExchange("lz_boot_topic_exchange").build();
}
/**
* 注册队列
*/
@Bean
public Queue topicQueue1() {
return QueueBuilder.durable("lz_boot_topic_queue1").build();
}
@Bean
public Queue topicQueue2() {
return QueueBuilder.durable("lz_boot_topic_queue2").build();
}
@Bean
public Queue topicQueue3() {
return QueueBuilder.durable("lz_boot_topic_queue3").build();
}
@Bean
public Binding topicBind1() {
return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("lz.#");
}
@Bean
public Binding topicBind2() {
return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("lz.*");
}
@Bean
public Binding topicBind3() {
return BindingBuilder.bind(topicQueue3()).to(topicExchange()).with("#");
}
@RabbitListener(queues = "lz_boot_topic_queue1")
public void topicProcess1(String msg) {
System.out.println("lz_boot_topic_queue1 接收到消息:" + msg);
}
@RabbitListener(queues = "lz_boot_topic_queue2")
public void topicProcess2(String msg) {
System.out.println("lz_boot_topic_queue2 接收到消息:" + msg);
}
@RabbitListener(queues = "lz_boot_topic_queue3")
public void topicProcess3(String msg) {
System.out.println("lz_boot_topic_queue3 接收到消息:" + msg);
}
}
@Configuration
public class ProducerTopic {
@Resource
private RabbitTemplate rabbitTemplate;
public void send() {
String msg = "topic 主题路由器";
rabbitTemplate.convertAndSend("lz_boot_topic_exchange", "lz.lq.666", msg + "1");
}
}