1. 使用rabbitmq笔记一
  2. 使用rabbitmq笔记二
  3. 使用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使用)的更多相关文章

  1. springboot集成使用rabbitmq笔记(1.rabbitmq安装)

    使用rabbitmq笔记一 使用rabbitmq笔记二 使用rabbitmq笔记三 1.选择适配的版本,参考---https://www.rabbitmq.com/which-erlang.html ...

  2. RabbitMQ学习笔记(一):安装及Springboot集成

    前言 MQ,即消息队列Message Queue的缩写. RabbitMQ 是MQ的一种,就像招商银行是银行的一种一样.主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用. 消息 ...

  3. springboot集成使用rabbitmq笔记(3.基本过程)

    使用rabbitmq笔记一 使用rabbitmq笔记二 使用rabbitmq笔记三 1.AMQP协议 AMQP 0-9-1的工作过程如下图:消息(message)被发布者(publisher)发送给交 ...

  4. SpringBoot集成rabbitmq(二)

    前言 在使用rabbitmq时,我们可以通过消息持久化来解决服务器因异常崩溃而造成的消息丢失.除此之外,我们还会遇到一个问题,当消息生产者发消息发送出去后,消息到底有没有正确到达服务器呢?如果不进行特 ...

  5. Java SpringBoot集成RabbitMq实战和总结

    目录 交换器.队列.绑定的声明 关于消息序列化 同一个队列多消费类型 注解将消息和消息头注入消费者方法 关于消费者确认 关于发送者确认模式 消费消息.死信队列和RetryTemplate RPC模式的 ...

  6. SpringBoot集成RabbitMQ消息队列搭建与ACK消息确认入门

    1.RabbitMQ介绍 RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗.Rabbi ...

  7. SpringBoot | 第十二章:RabbitMQ的集成和使用

    前言 上节讲了缓存数据库redis的使用,在实际工作中,一般上在系统或者应用间通信或者进行异步通知(登录后发送短信或者邮件等)时,都会使用消息队列进行解决此业务场景的解耦问题.这章节讲解下消息队列Ra ...

  8. springboot集成rabbitmq并手动注册容器实现单个queue的ack模式

    原文:https://blog.csdn.net/qq_38439885/article/details/88982373 进入正题,本文会介绍两种实现rabbitmq的ack模式的方法,分别为: 一 ...

  9. rabbitmq笔记(一)rabbitmq简介及基础

    一.消息组件 如果从消息组件来讲主要划分位两类: 1.JMS组件:ActiveMQ(慢): 2.AMQP组件(协议):性能是最高的, 而AMQP有两个主要的开源: 1)RabbitMQ:使用最广泛,速 ...

随机推荐

  1. 备份Oracle 数据库。

    #!/bin/bash# 2018-07-07 oracle database back#filename=`date +%Y%m%d`filename=`date +%Y_%m_%d_%H%M`di ...

  2. SpringBoot 中定时执行注解(@Scheduled、@EnableScheduling)

    项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息.Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor .TaskScheduler 接口. ...

  3. Dictonary(Python)(一)

    基本用法: .keys .values .items >>> D = dict(a=1,b=2,c=3) >>> D {'a': 1, 'b': 2, 'c': 3 ...

  4. Ubuntu修改用户和root密码

    如果要修改root的密码:sudo passwd 如果要修改_当前_用户的密码:passwd 如果要修改其他用户的密码(你得有权限):sudo passwd USERNAME,USERNAME就是你要 ...

  5. RAM/ROM IP一次性总结

    1, 若需要修改memory mode, 需重新编译; 若不需要修改memory mode, 直接修改宏参数即可; 2, 宏参数列表: 3, 注意用LE搭memory的情况; 4, memory ty ...

  6. mysql 获取任意一个月的所有天数。

    SELECT ADDDATE(y.first, x.d - 1) as dFROM(SELECT 1 AS d UNION ALLSELECT 2 UNION ALLSELECT 3 UNION AL ...

  7. 2019牛客多校第五场H - subsequence 2 拓扑

    H - subsequence 2 题意 要你使用前\(m\)个小写字母构造一个长度为\(n\)的字符串 有\(m*(m-1)/2\)个限制条件: \(c_{1} .c_{2}. len\):表示除去 ...

  8. 容器————priority_queue

    #include <queue> 与queue不同的是可以自定义其中数据的优先级,让优先级高的先出队列. 优先队列具有队列的所有特性,包括基本操作,只是在这基础上添加了内部的一个排序,它本 ...

  9. svn服务端安装、迁移教程、Eclipse切换svn连接库

    svn服务端安装.迁移教程.Eclipse切换svn连接库 安装教程 1.下载svn服务端安装程序 2.运行程序,安装 这里端口号根据自己定义,避免与其他程序冲突 到这里就安装完成 迁移教程 这里举例 ...

  10. delphi 以系统权限运行程序的代码

    program sysrun; uses Windows, SysUtils, tlhelp32, AccCtrl, AclAPI; function findprocess(TheProcName: ...