实现步骤:
添加依赖
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
org.springframework.boot
spring-boot-starter-amqp
org.springframework.boot
spring-boot-starter-test
配置整合
创建application.yml连接参数等配置文件;
spring:
rabbitmq:
host: 192.168.200.142
port: 5672
username: admin
password: 123456
virtual-host: /
创建配置类
package com.Zh.config;
import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author OZH
* @Description:
* @date 2022/1/28 14:11
*/
//配置类:声明交换机,队列,以及交换机和队列的绑定
@Configuration
public class RabbitMQConfig {
public static final String EXCHANGE_NAME = "boot_topic_exchange";//交换机名字
public static final String QUEUE_NAME = "boot_queue";//队列名字
//1.交换机
@Bean(value = "topicExchange")
public Exchange topicExchange() {
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}
//2.对列
@Bean(value = "bootQueue")
public Queue bootQueue() {
return QueueBuilder.durable(QUEUE_NAME).build();
}
//3.队列和交换机绑定关系 Binding
/*
1.知道哪个队列
2.知道哪个交换机
3.routing key
noargs():表示不指定参数
@Qualifier //自动装配
*/
@Bean
public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue,
@Qualifier("topicExchange") Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
}
}
创建入口类
@SpringBootApplication
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class);
}
}
发送消息
创建测试文件
package com.Zh;
import com.Zh.config.RabbitMQConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author OZH
* @Description:
* @date 2022/1/28 14:28
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ProducerTest {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 第一个参数:交换机名字
* 第二个参数:routingKey
* 第三个参数:发送的消息
*/
@Test
public void testSend(){
rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.haha","mq hello");
}
}
实现步骤
创建工程consumber-springboot
添加依赖
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
org.springframework.boot
spring-boot-starter-amqp
配置整合
创建application.yml连接参数等配置文件;
spring:
rabbitmq:
host: 192.168.200.142
port: 5672
username: admin
password: 123456
virtual-host: /
消息监听器
队列监听器
创建 RabbimtMQListener
package com.Zh.listener;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class RabbimtMQListener {
@RabbitListener(queues = "boot_queue")//要监听的队列
public void listenerQueue(Message message){
System.out.println(new String(message.getBody()));
}
}
创建入口类
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
运行applicaion:
小结: