醋醋百科网

Good Luck To You!

SpringBoot整合RabbitMQ实现消息的发送和接收操作?

在介绍SpringBoot整合RabbitMQ之前首先先来介绍如何在Linux中安装RabbitMQ。想要在在Linux中安装RabbitMQ通常涉及安装Erlang(RabbitMQ的依赖项)和RabbitMQ本身的安装。下面我们就来介绍CentOS系统中如何安装好RabbitMQ服务。

在CentOS上安装RabbitMQ

第一步、更新系统包列表

sudo yum update -y

第二步、安装Erlang

通过RabbitMQ官方提供的Erlang仓库安装

sudo yum install -y epel-release
sudo yum install -y https://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm
sudo yum install -y erlang

第三步、添加RabbitMQ的YUM仓库

sudo yum install -y https://packagecloud.io/rabbitmq/rabbitmq-server/packages/el/7/rabbitmq-server-3.8.9-1.el7.noarch.rpm/download.rpm

第四步、安装RabbitMQ

sudo yum install -y rabbitmq-server

第五步、启动RabbitMQ服务并设置开机自启动

sudo systemctl enable rabbitmq-server
sudo systemctl start rabbitmq-server

检查RabbitMQ服务状态

sudo systemctl status rabbitmq-server

启用RabbitMQ管理控制台

RabbitMQ提供了一个Web管理插件,方便管理和监控。我们可以启动这个插件来进行可视化的管理。

sudo rabbitmq-plugins enable rabbitmq_management

管理控制台默认运行在端口15672上,可以通过以下URL访问,如下所示。

http://localhost:15672/

默认的用户名和密码均为guest。但是出于安全考虑,建议在生产环境中修改默认密码或创建新的用户。

安装好了RabbitMQ之后,接下来就来看看如何将RabbitMQ整合到SpringBoot中实现消息的发送和接收操作。

Spring Boot整合RabbitMQ

第一步、添加依赖

在pom.xml文件中添加RabbitMQ的依赖,如下所示。

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

第二步、配置RabbitMQ

在application.properties配置文件中添加RabbitMQ的连接配置信息,如下所示。使用默认的访客用户来进行操作。

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

第三步、创建配置类

我们可以创建一个配置类,用来定义队列信息、交换机信息和相关的绑定信息操作,如下所示。

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    public static final String QUEUE_NAME = "myQueue";
    public static final String EXCHANGE_NAME = "myExchange";

    @Bean
    public Queue myQueue() {
        return new Queue(QUEUE_NAME, true);
    }

    @Bean
    public TopicExchange myExchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    @Bean
    public Binding binding(Queue myQueue, TopicExchange myExchange) {
        return BindingBuilder.bind(myQueue).to(myExchange).with("routing.key.#");
    }
}

创建配置类完成之后,接下来就是需要创建消息的提供者和消息的消费者,分别如下所示。

消息生产者:消息生产者来发送消息到RabbitMQ

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RabbitMQProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(String message) {
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "routing.key.test", message);
    }
}

消息消费者,用来接收RabbitMQ中的消息

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
public class RabbitMQConsumer {

    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
    }
}

第四步、测试消息发送和接收

通过一个控制器来测试消息的发送

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RabbitMQController {

    @Autowired
    private RabbitMQProducer rabbitMQProducer;

    @GetMapping("/send")
    public String sendMessage(@RequestParam String message) {
        rabbitMQProducer.sendMessage(message);
        return "Message sent: " + message;
    }
}

启动应用程序,我们可以在浏览器中或者是通过其他的接口调用工具来访问
http://localhost:8080/send?message=HelloWorld
,接口,我们就可以看到控制台中会有发送的HelloWorld的消息。当然我们也可以在RabbitMQ的Web控制台中看到消息的发送和接收的相关情况。

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言