<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-amqpartifactId>
dependency>
spring:
application:
name: rabbitmq-springboot
rabbitmq:
host: 192.168.153.180
username: /ems
password: 123
virtual-host: /ems
port: 5672
RabbitTemplate 用来简化操作 使用时候直接在项目中注入即可使用
//注入rabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
void testhello() {
rabbitTemplate.convertAndSend("hello","hello world");
}
package com.xizi.hello;
@Component
@RabbitListener(queuesToDeclare = @Queue(value = "hello"))
public class HelloCustomer {
@RabbitHandler
public void receive(String message){
System.out.println("message======= "+message);
}
}
//注入rabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
//公平消费 work
@Test
void testWork() {
for (int i = 0; i < 10; i++) {
rabbitTemplate.convertAndSend("work","work模型"+i);
}
}
package com.xizi.work;
@Component
public class WorkCustomer {
//一个消费者
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive1(String message){
System.out.println("message1========"+message);
}
//一个消费者
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive2(String message){
System.out.println("message2========"+message);
}
}
//注入rabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
//fanout 关播
@Test
void testfanout() {
rabbitTemplate.convertAndSend("logs_fanout","","fanout模型发送的消息");
}
package com.xizi.fanout;
@Component
public class Customer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,//创建临时队列
exchange = @Exchange(value = "logs_fanout",type = "fanout")
)
})
public void receive1(String message){
System.out.println("message1======="+message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,//创建临时队列
exchange = @Exchange(value = "logs_fanout",type = "fanout")
)
})
public void receive2(String message){
System.out.println("message2======="+message);
}
}
//注入rabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
//路由模式 route
@Test
public void testRoute(){
rabbitTemplate.convertAndSend("direct","info","发送info的路由信息");
}
package com.xizi.route;
@Component
public class RouteCustomer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,//创建临时队列
exchange = @Exchange(value = "direct",type = "direct"),
key={
"info","error","warn"}
)
})
public void receive1(String message){
System.out.println("message1=========="+message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,//创建临时队列
exchange = @Exchange(value = "direct",type = "direct"),
key={
"error"}
)
})
public void receive2(String message){
System.out.println("message2=========="+message);
}
}
//注入rabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
//topic 动态订阅 订阅模式
@Test
public void testTopic(){
rabbitTemplate.convertAndSend("topic","user.save","topic订阅模式");
}
package com.xizi.topic;
@Component
public class TopicCustomer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(type ="topic",value ="topic",name ="topic" ),
key = {
"user.save","user.*"}
)
})
public void receive1(String message ){
System.out.println("message1========"+message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(type ="topic",value ="topic",name ="topic" ),
key = {
"order.#","produce.#","user.*"}
)
})
public void receive2(String message ){
System.out.println("message2========"+message);
}
}