整合SpringBoot
1 快速构建一个SpringBoot项目
2,勾选web依赖和Rabbitmq依赖,或者手动在pom.xml文件中导入依赖
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
编写yml配置文件
spring:
application:
name: rabbitmq-springboot
rabbitmq:
host: 192.168.0.42
port: 5672
username: shop
password: 123456
virtual-host: /shop
Spring提供了一个RabbitTemplate模板,直接注入就可以使用了,以下为RabbitMQ各种模型的代码
@SpringBootTest(classes = SpringbootRabbitmqApplication.class)
@RunWith(SpringRunner.class)
public class TestRabbitMQ {
//注入RabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
//hello world
@Test
public void test(){
rabbitTemplate.convertAndSend("hello","hello world");
}
//work模型
@Test
public void testWork(){
for (int i = 0; i < 10; i++) {
rabbitTemplate.convertAndSend("work","work模型");
}
}
//fanout广播模型
@Test
public void testFanout(){
rabbitTemplate.convertAndSend("logs","","fanout发布的消息");
}
//router路由模式
@Test
public void testRouter(){
rabbitTemplate.convertAndSend("directs","error","发送了info的key的路由信息");
}
//动态路由
@Test
public void testTopics(){
rabbitTemplate.convertAndSend("topics","order.save","user.save的消息");
}
}
消费者之HelloWord模型
**/
//组件,被工厂监听
@Component
//消费者监听这个类 多个参数,对应业务 默认是持久化,非独占,不是自动删除的队列
@RabbitListener(queuesToDeclare = @Queue(value = "hello"/*,declare = "true",autoDelete = "true"*/))
public class HelloCustomer {
@RabbitHandler
public void receivel(String message){
System.out.println("消息是"+message);
}
}
消费者之Work模型
@Component
public class WorkCustomer {
//一个消费者
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receivel1(String message){
System.out.println("message1"+message);
}
//二个消费者
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receivel2(String message){
System.out.println("message2"+message);
}
}
fanout扇出模型
@Component
public class FanoutCustomer {
@RabbitListener(bindings = {
@QueueBinding(value = @Queue,//创建临时队列
exchange = @Exchange(value = "logs",type = "fanout")//绑定的交换机
)
})
public void receivel1(String message){
System.out.println("message1"+message);
}
@RabbitListener(bindings = {
@QueueBinding(value = @Queue,//创建临时队列
exchange = @Exchange(value = "logs",type = "fanout")//绑定的交换机
)
})
public void receivel2(String message){
System.out.println("message2"+message);
}
}
路由模型
@Component
public class RouterCustomer {
@RabbitListener(bindings = {
@QueueBinding(value = @Queue,exchange = @Exchange(value = "directs",type = "direct"),
key = {"info","error","warn"}
)//指定交换机名称和类型
})
public void receivel1(String message){
System.out.println("message1"+message);
}
@RabbitListener(bindings = {
@QueueBinding(value = @Queue,exchange = @Exchange(value = "directs",type = "direct"),
key = {"error"}//路由Key
)//指定交换机名称和类型
})
public void receivel2(String message){
System.out.println("message2"+message);
}
}
动态路由模型
@Component
public class topicCustomer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(type="topic",name="topics"),
key = {"user.save","user.*"}
)
})
public void receivel1(String message){
System.out.println("message1"+message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(type="topic",name="topics"),
key = {"order.#","user.*","produce.*"}
)
})
public void receivel2(String message){
System.out.println("message2"+message);
}
}
这就是SpringBoot整合RabbitMQ以及基本用法,结合代码和注释学习吧