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:使用最广泛,速 ...
随机推荐
- Java集合体系结构(List、Set、Collection、Map的区别和联系)
Java集合体系结构(List.Set.Collection.Map的区别和联系) 1.Collection 接口存储一组不唯一,无序的对象 2.List 接口存储一组不唯一,有序(插入顺序)的对象 ...
- linux-redis-install
安装redis3.2.9 wget cd make 编译完成后,将redis-cli redis-server redis-conf redis-benchmark配置文件复制到usr/redis文件 ...
- js调用浏览器
定义和用法 open() 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口. 语法 window.open(URL,name,specs,replace) 参数 说明 URL 可选.打开指定的页面 ...
- linux 应用层常用API/命令
一 API 1. bsearch NAME bsearch - binary search of a sorted array SYNOPSIS #include <stdlib.h> v ...
- hdu1059&poj1014 Dividing (dp,多重背包的二分优化)
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...
- [CSP-S模拟测试]:影魔(树状数组+线段树合并)
题目背景 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄.每一个灵魂,都有着自己 ...
- postgreSQL执行计划
" class="wiz-editor-body wiz-readonly" contenteditable="false"> explain命 ...
- 59、salesforce实现数据的批量处理
批处理,往自己的邮箱发一封邮件,批处理采用异步的处理方式处理数据,最多可以处理5000万条数据 global with sharing class MerchandiseBatch implement ...
- c#网络通信框架networkcomms内核解析之六 处理接收到的二进制数据
本文基于networkcomms2.3.1开源版本 gplv3协议 在networkcomms通信系统中,服务器端收到某连接上的数据后,数据会暂时存放在"数据包创建器"(Pack ...
- 用processing画李萨如曲线
李萨如曲线 有没有对示波器上变化曲线产生过兴趣,它叫做李萨如曲线: 数学上,利萨茹(Lissajous)曲线(又称利萨茹图形.李萨如图形或鲍迪奇(Bowditch)曲线)是两个沿着互相垂直方向的正弦振 ...