SpringCloud分布式微服务b2b2c电子商务(十三)Springboot整合RabbitMQ
发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,这篇文章带你了解怎么整合RabbitMQ服务器,并且通过它怎么去发送和接收消息。我将构建一个springboot工程,通过RabbitTemplate去通过MessageListenerAdapter
千家信息网最后更新 2025年02月03日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安全错误
数据库的锁怎样保障安全
像素时光服务器
如何利用数据库选题
国家网络安全法制宣传周
浏览器怎么清楚数据库
2021网络安全宣传主题是什么
美信网络技术公司怎么找
什么软件开发棋牌
西城区网络软件开发
网络安全常用的关键技术有哪三类
部队网络安全的重要指示
破解版企业版数据库下载
ssis包配置数据库连接串
嵌入式和软件开发
清空数据库表高效率的方法
刺激战场亚服服务器超时
大学生网络安全意识调查心得
企业给服务器批量装系统
深挖网络安全模式
dell 塔式服务器换硬盘
云服务器什么用
网狐6603数据库下载
数据库字段如何修改
广州千八互联网科技有限公司
软件开发专业教材
科东网络安全监测装置脱管原因
需求分析图书管理系统数据库
导航软件开发平台
济南阿帕网络技术有限公司
横向数据库 纵向数据库的作用
ncre网络技术好过吗