iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >SpringCloud分布式微服务b2b2c电子商务(十三)Springboot整合RabbitMQ
  • 114
分享到

SpringCloud分布式微服务b2b2c电子商务(十三)Springboot整合RabbitMQ

2023-06-05 04:06:28 114人浏览 泡泡鱼
摘要

这篇文章带你了解怎么整合RabbitMQ服务器,并且通过它怎么去发送和接收消息。我将构建一个SpringBoot工程,通过RabbitTemplate去通过MessageListenerAdapter去订阅一个POJO类型的消息。准备工作1

这篇文章带你了解怎么整合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的起步依赖:

<dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-amqp</artifactId>        </dependency>

创建消息接收者
在任何的消息队列程序中,你需要创建一个消息接收者,用于响应发送的消息。

@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);    }}

--结束END--

本文标题: SpringCloud分布式微服务b2b2c电子商务(十三)Springboot整合RabbitMQ

本文链接: https://www.lsjlt.com/news/241193.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作