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:使用最广泛,速 ...
随机推荐
- js 输入整数
1.我用 /^\+?[1-9][0-9]*$/ 貌似不对(小数也可以输入) 2.输入整数 n = /^[1-9]\d*$/; . -]\d*$/; //判断字符串是否为数字 if (!value) ...
- json 文件打读取
1.获取文件路径 /* * BookController.class.getClassLoader().getResource("static/json/book_nav.json" ...
- python的logging,将log保存到文件
import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(line ...
- 写一个自定义类加载器demo
public class MyTest16 extends ClassLoader { private String classLoaderName; private String fileExten ...
- 微信支持的Authorization code授权模式(公众号开发)(开放平台资料中心中的代公众号发起网页授权)
链接:https://blog.csdn.net/ASZJBGD/article/details/82838356 主要流程分为两步: 1.获取code 2.通过code换取accesstoken 流 ...
- Service系统服务(六):rsync基本用法、rsync+SSH同步、配置rsync服务端、访问rsync共享资源、使用inotifywait工具、配置Web镜像同步、配置并验证Split分离解析
一.rsync基本用法 目标: 本例要求掌握远程同步的基本操作,使用rsync命令完成下列任务: 1> 将目录 /boot 同步到目录 /todir 下 2> 将目录 /boot 下的 ...
- ShopNC B2B2C最新版去除shop方法教程
1.转移shop下目录文件至根目录: 2.修改config.ini.php里的shop绑定域名更改为无shop目录: 3.将原始根目录的index.php更改为main.php文件名自己定!并修改in ...
- React的contextType的使用方法简介
上一篇介绍了Context的使用方法.但是Context会让组件变得不纯粹,因为依赖了全局变量.所以这决定了Context一般不会大规模的使用.所以一般在一个组件中使用一个Context就好. 由于C ...
- 6.zabbix微信告警3.2
原文地址: https://blog.cactifans.com/2016/01/27/zabbix%E5%BE%AE%E4%BF%A1%E5%91%8A%E8%AD%A6/ pdf : 链接: ht ...
- PHP 换行处理
换行符 unix系列用 \n windows系列用 \r\n mac用 \r PHP中可以用PHP_EOL来替代,以提高代码的源代码级可移植性 如: <?php echo PHP_EOL; // ...