SpringBoot中使用RabbitMQ
<!-- 引入 rabbitmq 集成依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
server:
port: 9090
spring:
application:
# 微服务系统有意义, 养成好习惯, 先写出来
name: rabbitmq-02-springboot
rabbitmq:
host: 127.0.0.1
port: 5672
username: codingce
password: 123456
virtual-host: /codingce
// 注入 rabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 直连
*/
@Test
void contextLoads() {
rabbitTemplate.convertAndSend("hello", "hello word");
}
/**
* @author mxz
*/
@Component
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloCustomer {
/**
* @param message
*/
@RabbitHandler
public void receivel(String message) {
System.out.println("message: " + message);
}
}
/**
* work
*/
@Test
void testWork() {
for (int i = 0; i < 10; i++) {
rabbitTemplate.convertAndSend("work", "work模型");
}
}
/**
* 第二种模型 work 模型
*
* @author mxz
*/
@Component
public class WorkCustomer {
/**
* 第1个消费者
*
* @param message
*/
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receivel(String message) {
System.out.println("message1 = " + message);
}
/**
* 第2个消费者
*
* @param message
*/
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receivel2(String message) {
System.out.println("message2 = " + message);
}
}
/**
* fanout 广播
*/
@Test
void testFanout() {
rabbitTemplate.convertAndSend("logs", "", "Fanout模型发送的消息");
}
/**
* fanout
*
* @author mxz
*/
@Component
public class FanoutCustomer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, // 创建临时队列
exchange = @Exchange(value = "logs", type = "fanout") // 绑定的交换机
)
})
public void receivel(String message) {
System.out.println("message1 = " + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, // 创建临时队列
exchange = @Exchange(value = "logs", type = "fanout") // 绑定的交换机
)
})
public void receive2(String message) {
System.out.println("message2 = " + message);
}
}
/**
* route 路由
*/
@Test
void testRoute() {
// exchange 交换机名称
rabbitTemplate.convertAndSend("directs", "info", "info的key的路由消息");
}
/**
* @author mxz
*/
@Component
public class RouteCustomer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, // 绑定临时队列
exchange = @Exchange(value = "directs", type = "direct"), // 自定义交换机名称和类型
key = {"info", "error", "warn"}
)
})
public void receivel(String message) {
System.out.println("message1 = " + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, // 绑定临时队列
exchange = @Exchange(value = "directs", type = "direct"), // 自定义交换机名称和类型
key = {"error"}
)
})
public void receivel2(String message) {
System.out.println("message1 = " + message);
}
}
/**
* topic 订阅模式 动态路由
*/
@Test
void testTopic() {
rabbitTemplate.convertAndSend("topics", "user.save", "user.save 路由消息");
}
/**
* 订阅模型
*
* @author mxz
*/
@Component
public class TopicCustomer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(type = "topic", name = "topics"),
key = {"user.save", "user.*"}
)
}
)
public void receivel(String message) {
System.out.println("message1" + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(type = "topic", name = "topics"),
key = {"produce.#", "order.#"}
)
}
)
public void receivel2(String message) {
System.out.println("message2" + message);
}
}
文章已上传gitee https://gitee.com/codingce/hexo-blog 项目地址github: https://github.com/xzMhehe/codingce-java