如果你没有使用springboot快速构建的方式启动项目,而是使用maven的方式进行构建,需要引入依赖
org.springframework.boot
spring-boot-starter-amqp
spring:
# 应用名称[非必须]
application:
name: springboot_rabbitmq
# rabbitmq 连接配置
rabbitmq:
host: 192.168.77.138
port: 5672 # tcp端口
username: ems # 用户名
password: 123 # 用户密码
virtual-host: /ems # 虚拟主机
springboot提供的操作rabbitmq的对象为:RabbitTemplate
使用时候直接在项目中注入即可使用
直接发送给队列,不使用交换机
@Autowired
private RabbitTemplate rabbitTemplate; //注入rabbitmq对象
//helloworld 直连模式
@Test
public void testHello() {
//发送消息[注意此时并不会立即创建队列,因为没有消费者存在]
//参数1:队列名字
rabbitTemplate.convertAndSend("hello", "hello 模型");
}
@Component //注入容器
//监听rabbitmq,参数1:队列名称,参数2:是否持久化,参数3:是否独占,参数4:是否自动删除
@RabbitListener(queuesToDeclare = @Queue(value = "hello", durable = "true", exclusive = "false", autoDelete = "false"))
public class HelloConsumer {
//消费消息
@RabbitHandler //加上此注解后,会自动回调
public void receive(String message) {
System.out.println("消费者:===>" + message);
}
}
@Autowired
private RabbitTemplate rabbitTemplate; //注入rabbitmq对象
//work 任务模型
@Test
public void testWork() {
for (int i = 1; i < 10; i++) {
rabbitTemplate.convertAndSend("work", "work 模型");
}
}
@Component //注入容器
public class WorkConsumer {
//消费者1号
@RabbitListener(queuesToDeclare = @Queue("work")) //@RabbitListener 也可以直接写在方法上进行回调
public void consumer1(String message) {
System.out.println("消费者1号:===>" + message);
}
//消费者2号
@RabbitListener(queuesToDeclare = @Queue("work")) //@RabbitListener 也可以直接写在方法上进行回调
public void consumer2(String message) {
System.out.println("消费者2号:===>" + message);
}
}
说明: 默认在Spring AMQP实现中Work这种方式就是公平调度,如果需要实现能者多劳需要额外配置
只要临时队列绑定到了交换机,一发送消息,所有消费者都可以接收
@Autowired
private RabbitTemplate rabbitTemplate; //注入rabbitmq对象
//fanout 广播模型
@Test
public void testFanout() {
//不指定队列
rabbitTemplate.convertAndSend("logs", "", "fanout模型");
}
@Component
public class FanoutConsumer {
//绑定临时队列与交换机
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue(),//临时队列
exchange = @Exchange(name = "logs", type = "fanout")//交换机名字与类型
)
})
public void consumer1(String message) {
System.out.println("消费者1号:===>" + message);
}
//绑定临时队列与交换机
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue(),//临时队列
exchange = @Exchange(name = "logs", type = "fanout")//交换机名字与类型
)
})
public void consumer2(String message) {
System.out.println("消费者2号:===>" + message);
}
}
指定路由key,只有拥有相匹配路由的队列才能接收到消息
@Autowired
private RabbitTemplate rabbitTemplate; //注入rabbitmq对象
//route 路由模型
@Test
public void testRoute() {
//需指定路由key
rabbitTemplate.convertAndSend("directs", "error", "direct模型");
}
@Component
public class DirectConsumer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue(),//临时队列
exchange = @Exchange(name = "directs", type = "direct"),//交换机名字和类型
key = {"info", "error"}//路由key
)
})
public void consumer1(String message) {
System.out.println("消费者1号:===>" + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue(),//临时队列
exchange = @Exchange(name = "directs", type = "direct"),//交换机名字和类型
key = {"error"}//路由key
)
})
public void consumer2(String message) {
System.out.println("消费者2号:===>" + message);
}
}
与route类型,只是需要指定动态的路由key
@Autowired
private RabbitTemplate rabbitTemplate; //注入rabbitmq对象
//topic 订阅模型
@Test
public void testTopic() {
//需指定路由key
rabbitTemplate.convertAndSend("topics", "order.add", "指定的路由key为:user.add[topic模型]");
}
@Component
public class TopicConsumer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue(),//临时队列
exchange = @Exchange(name = "topics",type = "topic"),//交换机名称和类型
key = {"user.*"}
)
})
public void consumer1(String message) {
System.out.println("消费者1号:===>" + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue(),//临时队列
exchange = @Exchange(name = "topics",type = "topic"),//交换机名称和类型
key = {"user.*","produce.#","order.*"}
)
})
public void consumer2(String message) {
System.out.println("消费者2号:===>" + message);
}
}