SpringBoot+RabbitMQ实现消息订阅发送
RabbitMQ 安装 (docker)
- 拉取rabbitmq镜像文件
# 未指定版本,拉取最新版本
root# docker pull rabbitmq
- 启动镜像
root# docker run -d --hostname my-rabbit --name rabbit -p 15672:15672 -p 5672:5672 rabbitmq
- 配置rabbitmq插件
# 配置rabbitmq web管理页面
root# docker exec -it 容器id rabbitmq-plugins enable rabbitmq_management
# 配置rabbitmq mqtt协议
root# docker exec -it 容器id rabbitmq-plugins enable rabbitmq_mqtt
# 如果我们需要浏览器通过websocket连接到rabbitmq,我们还需要启动rabbitmq_web_mqtt插件
root# docker exec 容器id rabbitmq-plugins enable rabbitmq_web_mqtt
# docker 查看已安装插件
root# docker exec rabbitmq rabbitmq-plugins list
-
访问web管理页面
浏览器输入:ip+15672 -
可能出现问题
5.1 Stats in management UI are disabled on this node。- docker exec -it 容器id /bin/bash
- cat /etc/rabbitmq/conf.d/management_agent.disable_metrics_collector.conf (true)
- cd /etc/rabbitmq/conf.d/
- echo management_agent.disable_metrics_collector = false > management_agent.disable_metrics_collector.conf (将true修改为false)
- docker restart 容器id
SpringBoot 配置
- 添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.12.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
- 添加yml配置
server:
port: 32372
spring:
application:
name: spring-rabbitmq
# rabbitmq 配置信息
rabbitmq:
host: 192.168.48.132
port: 5672
username: guest
password: guest
- 生产者
package com.ao.springrabbitmq.service.producer;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Producer {
@Autowired
private RabbitTemplate rabbitTemplate;
public void produce() {
String message = "晚上加班";
System.out.println("老板说:" + message);
rabbitTemplate.convertAndSend("notice_queue", message);
}
}
- 消费者
package com.ao.springrabbitmq.service.consumer;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class Consumer {
@RabbitHandler
@RabbitListener(queuesToDeclare = @Queue("notice_queue"))
public void process(String message) {
System.out.println("打工人收到通知:" + message);
}
}
- 测试类
package com.ao.springrabbitmq;
import com.ao.springrabbitmq.service.producer.Producer;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class RabbitMQTests {
@Autowired
Producer producer;
@Test
public void amqpTest() throws InterruptedException {
// 生产者发送消息
producer.produce();
Thread.sleep(1000);
}
}