pom.xml
提供者工程:RabbitMQ_provider4.0.0 org.example RabbitMQ pom 1.0-SNAPSHOT RabbitMQ_provider RabbitMQ_consumer org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE 8 8 org.springframework.cloud spring-cloud-dependencies Greenwich.SR1 pom import
pom.xml
RabbitMQ org.example 1.0-SNAPSHOT 4.0.0 RabbitMQ_provider 8 8 org.springframework.boot spring-boot-starter-amqp org.springframework.boot spring-boot-starter-test org.springframework.amqp spring-rabbit-test test
application.yml
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /
username: chocoMoss
password: 123456
启动类
package com.example;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class RabbitMQProviderApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitMQProviderApplication.class,args);
}
}
配置类
package com.example.Config;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
//声明交换机
@Bean("itemTopicExchange")
public Exchange topicExchange() {
return ExchangeBuilder.topicExchange("item_topic_exchange").durable(true).build();
}
//声明队列
@Bean("itemQueue")
public Queue itemQueue() {
return QueueBuilder.durable("item_queue").build();
}
//将队列绑定到交换机上
@Bean
public Binding itemQueueExchange(@Qualifier("itemQueue") Queue queue,
@Qualifier("itemTopicExchange") Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("item.#").noargs();
}
}
测试类
注意: 测试类的目录要和启动类的目录一致
package com.example;
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.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMQTest {
//用于发送MQ消息
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testCreateMessage(){
rabbitTemplate.convertAndSend("item_topic_exchange", "item.insert", "商品新增,routing key 为item.insert");
rabbitTemplate.convertAndSend("item_topic_exchange", "item.update", "商品修改,routing key 为item.update");
rabbitTemplate.convertAndSend("item_topic_exchange", "item.delete", "商品删除,routing key 为item.delete");
}
}
消费者工程:RabbitMQ_consumer
pom.xml
RabbitMQ org.example 1.0-SNAPSHOT 4.0.0 RabbitMQ_consumer 8 8 org.springframework.boot spring-boot-starter-amqp
application.yml
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /
username: chocoMoss
password: 123456
监听类
package com.example.listener;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class MessageListener {
@RabbitListener(queues = "item_queue")
public void myListener(String message){
System.out.println("消费者收到的消息为:"+message);
}
}
启动类
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RabbitMQConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitMQConsumerApplication.class,args);
}
}
测试结果



