RabbitMQ(十二)——springboot整合RabbitMQ
2021/8/4 23:08:18
本文主要是介绍RabbitMQ(十二)——springboot整合RabbitMQ,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
环境搭建
一、创建一个Springboot项目
二、导入相关依赖
或者不勾选Spring for RabbitMQ
,自己导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
三、编写配置文件
# 应用名称 spring.application.name=rabbitmq_springboot # 应用服务 WEB 访问端口 server.port=8080 spring.rabbitmq.port=5672 spring.rabbitmq.host=192.168.137.5 spring.rabbitmq.username=admin spring.rabbitmq.password=123 #虚拟主机 spring.rabbitmq.virtual-host=/
helloworld模型
一、编写消费者
package com.study.hello; 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 // 生产端没有指定交换机只有routingKey和Object。 //消费方产生hello队列,放在默认的交换机(AMQP default)上。 //而默认的交换机有一个特点,只要你的routerKey的名字与这个 //交换机的队列有相同的名字,他就会自动路由上。 //生产端routingKey 叫hello ,消费端生产hello队列。 //他们就路由上了 @RabbitListener(queuesToDeclare = @Queue(value = "hello")) //表示RabbitMQ消费者,声明一个队列 public class HelloConsumer { @RabbitHandler //当消费者从队列取出消息时的回调方法 public void receive(String message){ System.out.println("message = " + message); } }
二、编写生产者测试类
package com.study; 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 TestRabbitMQ { //注入rabbitTemplate @Autowired private RabbitTemplate rabbitTemplate; //hello world @Test public void testHelloWorld(){ //转换和发送 1.routingKey 2.消息 rabbitTemplate.convertAndSend("hello","hello world"); } }
三、运行生产者测试类
work工作模型
一、消费者
package com.study.work; import org.springframework.amqp.rabbit.annotation.Queue; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class WorkConsumer { //第一个消费者 @RabbitListener(queuesToDeclare = @Queue("work")) //@RabbitListener在方法上代表它监听这个方法作为队列消费回调 public void receive1(String message){ System.out.println("message1 = " + message); } //第二个消费者 @RabbitListener(queuesToDeclare = @Queue("work")) //@RabbitListener在方法上代表它监听这个方法作为队列消费回调 public void receive2(String message){ System.out.println("message2 = " + message); } }
二、生产者
@Test public void testWork(){ for (int i = 0; i < 10; i++) { rabbitTemplate.convertAndSend("work","work模型"); } }
三、运行测试
fanout广播模型
一、消费者
package com.study.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 FanoutConsumer { @RabbitListener(bindings = { @QueueBinding( value = @Queue, //创建临时队列 exchange =@Exchange(value = "logs",type = "fanout") //绑定的交换机 ) }) public void receive1(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); } }
二、生产者
@Test public void testFanout(){ rabbitTemplate.convertAndSend("logs","","Fanout模型发送的消息"); }
三、运行测试
route路由模型
一、消费者
package com.study.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 RouteConsumer { @RabbitListener(bindings = { @QueueBinding( value = @Queue,//临时队列 exchange = @Exchange(value = "directs",type = "direct"),//指定交换机名称和类型 key = {"info","error","warn"} ) }) public void receive1(String message){ System.out.println("message1 = " + message); } @RabbitListener(bindings = { @QueueBinding( value = @Queue,//临时队列 exchange = @Exchange(value = "directs",type = "direct"),//指定交换机名称和类型 key = {"error"} ) }) public void receive2(String message){ System.out.println("message2 = " + message); } }
二、生产者
@Test public void testRoute(){ rabbitTemplate.convertAndSend("directs","error","发送info的key的路由信息"); }
三、运行测试
Topic动态路由模型
一、消费者
package com.study.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 TopicConsumer { @RabbitListener(bindings = { @QueueBinding( value = @Queue,//临时队列 exchange = @Exchange(type = "topic",name = "topics"), key = {"user.save","user.*"} ) }) public void receive1(String message){ System.out.println("message1 = " + message); } @RabbitListener(bindings = { @QueueBinding( value = @Queue,//临时队列 exchange = @Exchange(type = "topic",name = "topics"), key = {"order.#","produce.#","user.*"} ) }) public void receive2(String message){ System.out.println("message2 = " + message); } }
二、生产者
@Test public void testTopic(){ rabbitTemplate.convertAndSend("topics","user.save","user.save 路由消息"); }
三、运行测试
这篇关于RabbitMQ(十二)——springboot整合RabbitMQ的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南