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:使用最广泛,速 ...
随机推荐
- CSS 3D 的魅力
作者 | 子慕大诗人 来源 | www.cnblogs.com/1wen/p/9064011.html 前言: 最近玩了玩用css来构建3D效果,写了几个demo,所以博客总结一下. 在阅读 ...
- idea spring boot搭建笔记
如何建立spring boot项目 首先建一个empty Probject,Modules导入新创建的Project new modules选择Spring lnitializr ->next( ...
- 所有anaconda安装包失败的解决办法汇总
多试几次,有时候网络不稳定下载一半会停掉 更改Pip源 pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple 1.首先确认镜像源 ...
- c# 6.0 语法特性
namespace _6._0新特性 { using static _6._0新特性.Statics.StaticClass; class Program { static void Main(str ...
- bzoj 1876
传送门 http://www.lydsy.com/JudgeOnline/problem.php?id=1876 二进制gcd 学到了(' ' ) 高精还得压位,最开始没写压位,然后调了1h ...
- 2017 NOIp 初赛体验
很菜...我还是太蒟蒻了. d 老师太强了... 应该能有七十几分 初赛稳了 Update: 五十几分...
- (2)C++基本类型
一.整形 short .int. long. long long sizeof查看类型所占的字节数 cout << sizeof(short)<<endl;//2 字节 cou ...
- Openstack组件部署 — keystone(domain, projects, users, and roles)
目录 目录 前文列表 Create a domain projects users and roles domain projects users and roles的意义和作用 Create the ...
- 几个常见的漏洞(xss攻击 cookie未清除 nginx信息泄露)与处理方法
项目在安全检查中发现很多问题,要求整改,其中就有最常见的xss攻击 漏洞描述 渗透测试人员检测到网站筛选框均存在反射型跨站脚本攻击,例如: "><script>alert( ...
- Django框架(十五)—— forms组件、局部钩子、全局钩子
目录 forms组件.局部钩子.全局钩子 一.什么是forms组件 二.forms组件的使用 1.使用语法 2.组件的参数 3.注意点 三.渲染模板 四.渲染错误信息 五.局部钩子 1.什么是局部钩子 ...