一 创建springboot项目
不选择依赖的话,也可以直接创建好项目后 再在pom.xml文件中导入依赖
<!-- rabbitmq集成依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
二 配置yml
spring:
rabbitmq:
host: 127.0.0.1 #主机ip
port: 5672 #端口号
username: root #登陆账号
password: root123 #登陆密码
virtual-host: /zz #虚拟主机
三 测试hello模型
创建消息生产者
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;
@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
public class Tests {
@Autowired
//注入rabbitTemplate对象
private RabbitTemplate rabbitTemplate;
// 测试hello模型
@Test
public void test(){
//第一个参数:队列名称
//第二个参数:发送消息的内容
rabbitTemplate.convertAndSend("hello","hello wor1d");
}
}
运行Test方法 发现rabbitmq web端并没有生成消息
这里是因为mq没有监听到消费者
创建消费者
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 消费者
*/
@Component
// rabbit消费者监听 指定接受那个队列的消息
// 若要设置队列其他属性 可以在@Queue注解后添加参数 默认生成的队列是持久化,非独占,不是自动删除的队列
//@Queue(value = "hello",durable = "true",autoDelete = "true") 所有参数都是String类型
@RabbitListener(queuesToDeclare =@Queue(value = "hello"))
public class HelloCustomer {
//@RabbitHandler代表接收到消息后的回调方法
@RabbitHandler
public void getMessage(String message){
System.out.println("message = "+message);
}
}
运行测试hello模型的test方法
发现控制台打印出了消息
web端也出现了hello这个队列
这里注意: 因为我是新创建的springboot项目, 也是加载的新的依赖,导致我在测试的时候一直接收不到消息,试了很多次,也不确定具体是不是这个问题,但是我clear 和 install maven之后,程序就OK了。
四 测试其他模型
消息生产者都在一个类里面
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;
@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
public class Tests {
@Autowired
//注入rabbitTemplate对象
private RabbitTemplate rabbitTemplate;
// 测试hello模型
@Test
public void test(){
//第一个参数:队列名称
//第二个参数:发送消息的内容
rabbitTemplate.convertAndSend("hello","hello wor1d");
}
// 测试work模型
@Test
public void testWork(){
for (int i = 0; i <10 ; i++) {
rabbitTemplate.convertAndSend("work","work test"+i);
}
}
// 测试fanout模型
@Test
public void testFanout(){
rabbitTemplate.convertAndSend("logs","","fanout模型发送的消息");
}
// route模型
@Test
public void testRoute(){
rabbitTemplate.convertAndSend("directs","info","发送info路由消息");
}
// topic模型
@Test
public void testTopic(){
rabbitTemplate.convertAndSend("topics","user.save","发送topic路由消息");
}
}
测试work模型
消费者
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class WorkCustomer {
@RabbitListener(queuesToDeclare = @Queue(value = "work"))
public void test(String message){
System.out.println("message1 :"+message);
}
@RabbitListener(queuesToDeclare = @Queue(value = "work"))
public void test2(String message){
System.out.println("message2 :"+message);
}
}
运行
测试fanout模型
消费者
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class FanoutCustomer {
// 绑定队列和交换机
@RabbitListener(bindings = {
@QueueBinding(value = @Queue, //不指定value 就是创建一个临时队列
exchange =@Exchange(value = "logs",type = "fanout"))
}) //绑定交换机)
public void test(String message){
System.out.println("message1 = "+message);
}
// 绑定队列和交换机
@RabbitListener(bindings = {
@QueueBinding(value = @Queue, //不指定value 就是创建一个临时队列
exchange =@Exchange(value = "logs",type = "fanout"))
}) //绑定交换机)
public void test2(String message){
System.out.println("message2 = "+message);
}
}
运行
测试route模型
消费者
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class RouteCustomer {
@RabbitListener@RabbitListener(bindings = {
@QueueBinding(value = @Queue, //创建一个临时队列
exchange =@Exchange(value = "directs", type = "direct"),//指定交换机名称
key = {"info","warn"}) //指定路由key
})
public void test(String message){
System.out.println("message1 = "+message);
}
@RabbitListener@RabbitListener(bindings = {
@QueueBinding(value = @Queue, //创建一个临时队列
exchange =@Exchange(value = "directs", type = "direct"),//指定交换机名称和类型 默认为direct类型
key = {"info"}) //指定路由key
})
public void test1(String message){
System.out.println("message2 = "+message);
}
}
运行
测试topic模型
消费者
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class TopicCustomer {
@RabbitListener
@RabbitListener(bindings = {
@QueueBinding(value = @Queue, //创建一个临时队列
exchange =@Exchange( name = "topics",type = "topic"),//指定交换机名称
key = {"user.save","user.*"}) //指定路由key
})
public void test(String message){
System.out.println("message1 = "+message);
}
@RabbitListener
@RabbitListener(bindings = {
@QueueBinding(value = @Queue, //创建一个临时队列
exchange =@Exchange( name = "topics",type = "topic"),//指定交换机名称
key = {"user.update","user.#"}) //指定路由key
})
public void test1(String message){
System.out.println("message2 = "+message);
}
@RabbitListener
@RabbitListener(bindings = {
@QueueBinding(value = @Queue, //创建一个临时队列
exchange =@Exchange( name = "topics",type = "topic"),//指定交换机名称
key = {"ceshi.update","ceshi.#"}) //指定路由key
})
public void test2(String message){
System.out.println("message3 = "+message);
}
}
运行
更多:
RabbitMQ学习笔记一:了解及在Linux下安装RabbitMQ