1,引包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.4.0</version>
</dependency>
2,添加配置
spring:
rabbitmq:
addresses: 10.211.55.7
port: 5672
username: guest
password: guest
virtual-host: /
3,添加RabbitConfig
package com.wayne.core.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;
import org.springframework.stereotype.Component;
@Configuration
public class RabbitConfig {
public static final String EXCHANGE_NAME = "boot_topic_exchange";
public static final String QUEUE_NAME = "boot_queue";
//1,交换机对象
@Bean("buildExchange")
public Exchange buildExchange(){
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}
//2,队列
@Bean("buildQueue")
public Queue buildQueue(){
return QueueBuilder.durable(QUEUE_NAME).build();
}
//3,绑定交换机和队列
@Bean
public Binding bindQueueExchange(@Qualifier("buildQueue") Queue queue,@Qualifier("buildExchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
}
}
4,添加生产者
@Autowired
private RabbitTemplate rabbitTemplate;
@PostMapping("/send")
public void sendMessage(){
rabbitTemplate.convertAndSend(EXCHANGE_NAME,"boot.hahah","hello spring boot rabbitMQ!~~~~~~~");
}
5,添加消费者
@RabbitListener(queues = QUEUE_NAME)
public void rabbitMQConsumerListener(Message message){
log.info("当前队列信息为"+message);
}
6,测试
2020-12-14 00:13:25,635 INFO (AbstractConnectionFactory.java:558)- Attempting to connect to: [10.211.55.7:5672]
2020-12-14 00:13:25,663 INFO (AbstractConnectionFactory.java:511)- Created new connection: rabbitConnectionFactory#46a488c2:0/SimpleConnection@1ecf0ac6 [delegate=amqp://guest@10.211.55.7:5672/, localPort= 55692]
2020-12-14 00:13:25,711 INFO (RabbitMQConsumerQueue.java:16)- 当前队列信息为(Body:'hello spring boot rabbitMQ!~~~~~~~' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=boot_topic_exchange, receivedRoutingKey=boot.hahah, deliveryTag=1, consumerTag=amq.ctag-p9F4ZgPsG4ECSHqMBmjRfA, consumerQueue=boot_queue])
2020-12-14 00:13:25,711 INFO (StartupInfoLogger.java:61)- Started SpringBootDemo in 2.389 seconds (JVM running for 2.974)
2020-12-14 00:13:37,691 INFO (DirectJDKLog.java:173)- Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-12-14 00:13:37,691 INFO (FrameworkServlet.java:525)- Initializing Servlet 'dispatcherServlet'
2020-12-14 00:13:37,697 INFO (FrameworkServlet.java:547)- Completed initialization in 6 ms
2020-12-14 00:13:37,718 INFO (RabbitMQConsumerQueue.java:16)- 当前队列信息为(Body:'hello spring boot rabbitMQ!~~~~~~~' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=boot_topic_exchange, receivedRoutingKey=boot.hahah, deliveryTag=2, consumerTag=amq.ctag-p9F4ZgPsG4ECSHqMBmjRfA, consumerQueue=boot_queue])