springboot集成使用rabbitmq笔记(2.rabbitmq使用)
1.引入包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency> <!-- spring-web相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- 工具 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
2.配置
spring:
rabbitmq:
addresses: 192.168.37.134
port: 5672
username: admin
password: admin
server:
port: 8035
context-path: /
rabbit:
queueName:
testQueue1: testqueue1
testQueue2: testqueue2
testQueue3: testqueue3
exchange:
exchangeName: test_publish_exchange
3.使用代码
3.1.配置类生成使用的bean
@Configuration
public class RabbitmqConfig { @Value("${rabbit.queueName.testQueue1}")
private String testQueue1; @Value("${rabbit.queueName.testQueue2}")
private String testQueue2; @Value("${rabbit.queueName.testQueue3}")
private String testQueue3; @Value("${rabbit.exchange.exchangeName}")
private String exchangeName; /**========================生产者配置===================*/
@Bean
public Exchange testExchange() {
return new TopicExchange(exchangeName);
} /**===================消费者配置===================*/
@Bean
public Queue getQueue1(){
return new Queue(testQueue1);
} @Bean
public Queue getQueue2(){
return new Queue(testQueue2);
} @Bean
public Queue getQueue3(){
return new Queue(testQueue3);
} @Bean
public Binding testConsumeBinding1() {
//消费队列绑定
return new Binding(testQueue1, Binding.DestinationType.QUEUE,
exchangeName,"*.test1.*", null);
} @Bean
public Binding testConsumeBinding2() {
//消费队列绑定
return new Binding(testQueue2, Binding.DestinationType.QUEUE,
exchangeName,"*.*.test2", null);
}
@Bean
public Binding testConsumeDlxBinding3() {
//消费队列绑定
return new Binding(testQueue3, Binding.DestinationType.QUEUE,
exchangeName,"test3.#", null);
}
}
3.2.发送消息
@RestController
@Slf4j
public class RabbitMQProduceController { /** 发送消息注入 AmqpTemplate*/
@Autowired
private AmqpTemplate rabbitTemplate; @Value("${rabbit.queueName.testQueue1}")
private String testQueue; @RequestMapping(value = "/send")
public String sendMsg(String msg){
send("test.test1.test2",msg+"-send1");
log.info("send1发送消息成功,routingKey:test.test1.test2,消息:{}",msg+"-send1"); send("test3.test1.test",msg+"-send2");
log.info("send2发送消息成功,routingKey:test3.test1.test,消息:{}",msg+"-send2"); send("test3.test2.test1",msg+"-send3");
log.info("send3发送消息成功,routingKey:test3.test2.test1,消息:{}",msg+"-send3");
return "ok";
} private void send(String routingKey,Object content) {
rabbitTemplate.convertAndSend("test_publish_exchange",routingKey,content);
}
}
3.3.接收消息
@Component
@Slf4j
public class RecieveListener {
@RabbitListener(queues = "${rabbit.queueName.testQueue1}")
public void processMsg(Message msg) {
log.info("1(*.test1.*).接收rabbitmq的msg : {}", msg.getPayload());
}
}
@Component
@RabbitListener(queues = "${rabbit.queueName.testQueue2}")
@Slf4j
public class RecieveListener2 {
@RabbitHandler
public void processMsg(@Payload String msg) {
log.info("2(*.*.test2).接收rabbitmq的msg : {}", msg.toString());
}
}
@Component
@Slf4j
public class RecieveListener3 {
@RabbitListener(queues = "${rabbit.queueName.testQueue3}")
public void processMsg(@Payload String msg) {
log.info("3(test3.#).接收rabbitmq的msg : {}", msg);
}
}
3.4.启动并访问接口http://localhost:8035/send?msg=message
INFO --- [nio--exec-] c.e.s.produce.RabbitMQProduceController : send1发送消息成功,routingKey:test.test1.test2,消息:message-send1
INFO --- [nio--exec-] c.e.s.produce.RabbitMQProduceController : send2发送消息成功,routingKey:test3.test1.test,消息:message-send2
INFO --- [nio--exec-] c.e.s.produce.RabbitMQProduceController : send3发送消息成功,routingKey:test3.test2.test1,消息:message-send3
INFO --- [cTaskExecutor-] c.e.s.consumer.RecieveListener3 : (test3.#).接收rabbitmq的msg : message-send2
INFO --- [cTaskExecutor-] c.e.s.consumer.RecieveListener2 : (*.*.test2).接收rabbitmq的msg : message-send1
INFO --- [cTaskExecutor-] c.e.s.consumer.RecieveListener : (*.test1.*).接收rabbitmq的msg : message-send1
INFO --- [cTaskExecutor-] c.e.s.consumer.RecieveListener : (*.test1.*).接收rabbitmq的msg : message-send2
INFO --- [cTaskExecutor-] c.e.s.consumer.RecieveListener3 : (test3.#).接收rabbitmq的msg : message-send3
springboot集成使用rabbitmq笔记(2.rabbitmq使用)的更多相关文章
- springboot集成使用rabbitmq笔记(1.rabbitmq安装)
使用rabbitmq笔记一 使用rabbitmq笔记二 使用rabbitmq笔记三 1.选择适配的版本,参考---https://www.rabbitmq.com/which-erlang.html ...
- RabbitMQ学习笔记(一):安装及Springboot集成
前言 MQ,即消息队列Message Queue的缩写. RabbitMQ 是MQ的一种,就像招商银行是银行的一种一样.主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用. 消息 ...
- springboot集成使用rabbitmq笔记(3.基本过程)
使用rabbitmq笔记一 使用rabbitmq笔记二 使用rabbitmq笔记三 1.AMQP协议 AMQP 0-9-1的工作过程如下图:消息(message)被发布者(publisher)发送给交 ...
- SpringBoot集成rabbitmq(二)
前言 在使用rabbitmq时,我们可以通过消息持久化来解决服务器因异常崩溃而造成的消息丢失.除此之外,我们还会遇到一个问题,当消息生产者发消息发送出去后,消息到底有没有正确到达服务器呢?如果不进行特 ...
- Java SpringBoot集成RabbitMq实战和总结
目录 交换器.队列.绑定的声明 关于消息序列化 同一个队列多消费类型 注解将消息和消息头注入消费者方法 关于消费者确认 关于发送者确认模式 消费消息.死信队列和RetryTemplate RPC模式的 ...
- SpringBoot集成RabbitMQ消息队列搭建与ACK消息确认入门
1.RabbitMQ介绍 RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗.Rabbi ...
- SpringBoot | 第十二章:RabbitMQ的集成和使用
前言 上节讲了缓存数据库redis的使用,在实际工作中,一般上在系统或者应用间通信或者进行异步通知(登录后发送短信或者邮件等)时,都会使用消息队列进行解决此业务场景的解耦问题.这章节讲解下消息队列Ra ...
- springboot集成rabbitmq并手动注册容器实现单个queue的ack模式
原文:https://blog.csdn.net/qq_38439885/article/details/88982373 进入正题,本文会介绍两种实现rabbitmq的ack模式的方法,分别为: 一 ...
- rabbitmq笔记(一)rabbitmq简介及基础
一.消息组件 如果从消息组件来讲主要划分位两类: 1.JMS组件:ActiveMQ(慢): 2.AMQP组件(协议):性能是最高的, 而AMQP有两个主要的开源: 1)RabbitMQ:使用最广泛,速 ...
随机推荐
- npm启动报错
npm 启动报错 event.js 160 报错原因: 端口号被占用 解决方法: 1.重新定义一个端口号. 2.任务管理器关掉node进程,重新运行npm.
- 【leetcode】901. Online Stock Span
题目如下: 解题思路:和[leetcode]84. Largest Rectangle in Histogram的核心是一样的,都是要找出当前元素之前第一个大于自己的元素. 代码如下: class S ...
- Mac系统下安装Homebrew后无法使用brew命令
打开终端输入 brew提示:command not found 解决方法 输入命令: sudo vim .bash_profile 然后输入以下代码: export PATH=/usr/local/b ...
- 四轴遥控器ADC部分
一.ADC参考手册学习 A/D转换可以按单次.连续设置采样:可以一一扫描或间断的对多个ADC通道进行采集. ADC的结果有左对齐和右对齐. ADC的输入时钟不得超过14Mhz,它是由PCLK2经分频产 ...
- [NOIP模拟测试31]题解
A.math 考场乱搞拿了95,2333. 考虑裴蜀定理:$ax+by=z$存在整数解,当且仅当$gcd(a,b)|z$. 那么如果某个数能够被拼出来,就必须满足所有$a_i$的$gcd$是它的因子. ...
- 聊一聊 http2.0
1. 我们认识http 协议,从最初的,客户端与服务器进行通讯,基于连接发生的请求与响应 在HTTP1.0时代,连接无法复用,每次下完单,都被强制登出/关机,下一次下单,就得重新登录. 为了解决htt ...
- What is the difference between UserControl, WebControl, RenderedControl and CompositeControl?
What is the difference between UserControl, WebControl, RenderedControl and CompositeControl? UserCo ...
- MacBook Pro 快捷键2
Mac 键盘快捷键 您可以按下组合键来实现通常需要鼠标.触控板或其他输入设备才能完成的操作. 要使用键盘快捷键,请按住一个或多个修饰键,同时按快捷键的最后一个键.例如,要使用快捷键 Command ...
- 泛微oa系统com.eweaver.base.DataAction文件sql参数sql注入
URL/ServiceAction/com.eweaver.base.DataAction?sql=select%201,2,3,4,5,6,7,8,9,233%20from%20DUAL%20
- python学习笔记:文件操作和集合
一.文件操作 文件读写步骤:有一个文件,打开文件,操作文件读写文件,关闭文件. python 文件读写模式r,r+,w,w+,a,a+的区别(附代码示例) 模式 可做操作 若文件不存在 是否覆盖 r ...