SpringCloud分布式微服务b2b2c电子商务(十三)Springboot整合RabbitMQ
发表于:2025-12-04 作者:千家信息网编辑
千家信息网最后更新 2025年12月04日,这篇文章带你了解怎么整合RabbitMQ服务器,并且通过它怎么去发送和接收消息。我将构建一个springboot工程,通过RabbitTemplate去通过MessageListenerAdapter
千家信息网最后更新 2025年12月04日SpringCloud分布式微服务b2b2c电子商务(十三)Springboot整合RabbitMQ
这篇文章带你了解怎么整合RabbitMQ服务器,并且通过它怎么去发送和接收消息。我将构建一个springboot工程,通过
RabbitTemplate去通过MessageListenerAdapter去订阅一个POJO类型的消息。
准备工作
15min
IDEA
maven 3.0
在开始构建项目之前,机器需要安装rabbitmq,你可以去官网下载,http://www.rabbitmq.com/download.html ,如果
你是用的Mac(程序员都应该用mac吧),你可以这样下载:
brew install rabbitmq
安装完成后开启服务器:
rabbitmq-server
开启服务器成功,你可以看到以下信息:
brew install rabbitmq安装完成后开启服务器:rabbitmq-server开启服务器成功,你可以看到以下信息:
构建工程
构架一个SpringBoot工程,其pom文件依赖加上spring-boot-starter-amqp的起步依赖:
org.springframework.boot spring-boot-starter-amqp
创建消息接收者
在任何的消息队列程序中,你需要创建一个消息接收者,用于响应发送的消息。
@Componentpublic class Receiver { private CountDownLatch latch = new CountDownLatch(1); public void receiveMessage(String message) { System.out.println("Received <" + message + ">"); latch.countDown(); } public CountDownLatch getLatch() { return latch; }}消息接收者是一个简单的POJO类,它定义了一个方法去接收消息,当你注册它去接收消息,你可以给它取任何的名字。了解springcloud架构可以加求求:三五三六二四七二五九。其中,它有CountDownLatch这样的一个类,它是用于告诉发送者消息已经收到了,你不需要在应用程序中具体实现它,只需要latch.countDown()就行了。
创建消息监听,并发送一条消息
在spring程序中,RabbitTemplate提供了发送消息和接收消息的所有方法。你只需简单的配置下就行了:
需要一个消息监听容器
声明一个quene,一个exchange,并且绑定它们
一个组件去发送消息
代码清单如下:
package com.forezp;import com.forezp.message.Receiver;import org.springframework.amqp.core.Binding;import org.springframework.amqp.core.BindingBuilder;import org.springframework.amqp.core.Queue;import org.springframework.amqp.core.TopicExchange;import org.springframework.amqp.rabbit.connection.ConnectionFactory;import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;@SpringBootApplicationpublic class SpringbootRabbitmqApplication { final static String queueName = "spring-boot"; @Bean Queue queue() { return new Queue(queueName, false); } @Bean TopicExchange exchange() { return new TopicExchange("spring-boot-exchange"); } @Bean Binding binding(Queue queue, TopicExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with(queueName); } @Bean SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames(queueName); container.setMessageListener(listenerAdapter); return container; } @Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver, "receiveMessage"); } public static void main(String[] args) { SpringApplication.run(SpringbootRabbitmqApplication.class, args); }}
消息
服务
服务器
程序
工程
接收者
成功
信息
方法
监听
整合
代码
发送者
只需
名字
容器
应用程序
文件
机器
构架
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
提升网络技术的通道
简述计算机网络安全环境特点
软件开发公司有哪些资质
赛思网络技术有限公司
计算机网络技术是不是修电脑的
大学网络安全教育检讨书
安装2008数据库需要什么插件
星火矿池日本服务器还能用吗
数据库技术建议
炉石的服务器在哪个国家
取代数据库
队友找不到服务器怎么办
如何添加序列数据库
网络安全维护管理员
宁国库存软件开发服务设备
如何加盟腾讯云服务器
云计算与网络安全就业前景
金天鹅数据库迁移还能登陆吗
电视墙服务器特点
新手可以自学软件开发吗
solr6.6链接数据库
驾驶人员网络安全学习
腾讯软件开发人员工资
服务器颜色代码
德邦软件开发工资待遇
相通网络技术有限公司如何
网络安全运维怎么做
cydia 服务器
软件开发是免增值税吗
广东银行微服务架构数据库