当前位置: 首页 > news >正文

SpringBoot整合RabbitMQ,笔记整理

1创建生产者工程springboot-rabbitmq-produce

2.修改pom.xml文件

<!--父工程-->
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.0</version><relativePath/>
</parent>
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--2. rabbitmq--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency>
</dependencies>

3.启动类  com.javacto.ProducerApplication

package com.javacto;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProducerApplication {public static void main(String[] args) {SpringApplication.run(ProducerApplication.class, args);}
}

4.创建application.yml

# 配置RabbitMQ的基本信息  ip 端口 username  password..
spring:rabbitmq:host: 192.168.64.139 # ipport: 5672username: javactopassword: javactovirtual-host: /javacto

5.创建RabbitMQ队列与交换机绑定的配置类com.javacto.rabbitmq.config.RabbitMQConfig


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {public static final String EXCHANGE_NAME = "boot_topic_exchange";public static final String QUEUE_NAME = "boot_queue";//1.交换机@Bean("bootExchange")public Exchange bootExchange(){return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();}//2.Queue 队列@Bean("bootQueue")public Queue bootQueue(){return QueueBuilder.durable(QUEUE_NAME).build();}//3. 队列和交互机绑定关系 Binding/*1. 知道哪个队列2. 知道哪个交换机3. routing key*/@Beanpublic Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){//  bind(队列).to(交换机).with(routingKey).noargs(不需要指定参数) 如果需要指它就.add()return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();}
}

6.创建com.javacto.test.ProducerTest 然后运行

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ="classpath:spring-rabbitmq-producer.xml")
public class ProductTest {//1.注入RabbitTemplate@Autowiredprivate RabbitTemplate rabbitTemplate;//2.发送消息@Testpublic  void testHelloWorld(){rabbitTemplate.convertAndSend("spring_queue","hello world spring222");}//2.发送消息@Testpublic  void testFanout(){rabbitTemplate.convertAndSend("spring_fanout_exchange","","spring fanout...");}//2.发送消息@Testpublic  void testTopics(){rabbitTemplate.convertAndSend("spring_topic_exchange","javacto.add","spring topic...");}
}

1创建消费者工程springboot-rabbitmq-produce

2.修改pom.xml文件

<!--父工程-->
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.0</version><relativePath/>
</parent>
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--2. rabbitmq--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency>
</dependencies>

3.启动类  com.javacto.ProducerApplication

package com.javacto;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProducerApplication {public static void main(String[] args) {SpringApplication.run(ProducerApplication.class, args);}
}

4.创建application.yml

# 配置RabbitMQ的基本信息  ip 端口 username  password..
spring:rabbitmq:host: 192.168.64.139 # ipport: 5672username: javactopassword: javactovirtual-host: /javacto

5.创建RabbitMQ队列与交换机绑定的配置类com.javacto.rabbitmq.config.RabbitMQConfig

package com.javacto.mq;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class RabbitMQListener {@RabbitListener(queues = "boot_queue")public  void listenerQueue(Message message){System.out.println(new String(message.getBody()));}
}

6.启动springBoot工程即可

http://www.lryc.cn/news/130961.html

相关文章:

  • 搜狗拼音暂用了VSCode及微信小程序开发者工具快捷键Ctrl + Shit + K 搜狗拼音截图快捷键
  • Python包sklearn画ROC曲线和PR曲线
  • snpEff变异注释的一点感想
  • “保姆级”考研下半年备考时间表
  • 具有弱监督学习的精确3D人脸重建:从单幅图像到图像集的Python实现详解
  • 查询投稿会议的好用网址
  • 一元三次方程的解
  • aardio开发语言Excel数据表读取修改保存实例练习
  • webshell绕过
  • Spring Boot 统一功能处理
  • 图像处理常见的两种拉流方式
  • 数据可视化数据调用浅析
  • 恒运资本:CPO概念发力走高,兆龙互联涨超10%,华是科技再创新高
  • 【蓝桥杯】[递归]母牛的故事
  • 使用RDP可视化远程桌面连接Linux系统
  • 数据可视化diff工具jsondiffpatch使用学习
  • pdf 转 word
  • 【数据结构OJ题】设计循环队列
  • Java 中创建对象有哪些方式?
  • Kafka 消息发送和消费流程
  • UVa10048 Audiophobia(floyd)
  • ​Redis概述
  • MsrayPlus多功能搜索引擎采集软件
  • 机器学习之概率论
  • 【深度学习 | 数据可视化】 视觉展示分类边界: Perceptron模型可视化iris数据集的决策边界
  • 【计算机视觉】相机基本知识(还在更新)
  • C++ (友元)(类嵌套时,成员函数以及类声明定义的顺序)小demo
  • 前端实习第五周周记
  • 【图论】Floyd算法
  • ceph数据分布