Spring Boot集成RabbitMQ管理API实现认证访问与资源获取,能够满足项目中动态查看和管理RabbitMQ资源的需求,避免频繁登录RabbitMQ管理后台操作,提升开发效率。

RabbitMQ管理API基础说明
RabbitMQ默认提供了REST风格的管理API,默认端口为15672,所有接口都需要进行身份认证,默认的用户名和密码是guest/guest,仅允许本地访问。如果需要在远程调用,需要先创建具有对应权限的用户。
常用的管理API接口包括:
- /api/queues:获取所有队列信息
- /api/exchanges:获取所有交换器信息
- /api/bindings:获取所有绑定关系信息
- /api/connections:获取所有连接信息
Spring Boot项目基础配置
首先需要在Spring Boot项目中添加相关依赖,这里使用RestTemplate来调用RabbitMQ的管理API,所以需要引入web相关依赖。
添加项目依赖
在pom.xml中添加以下依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
配置RabbitMQ管理API参数
在application.yml中配置RabbitMQ管理API的基础信息:
rabbitmq:
manage:
host: 127.0.0.1
port: 15672
username: guest
password: guest
api-base-path: /api
实现管理API认证访问配置
RabbitMQ管理API使用HTTP Basic认证,所以需要在调用接口时添加认证头信息。我们可以通过配置RestTemplate,添加拦截器来统一处理认证逻辑。
配置RestTemplate
创建RestTemplate配置类,添加Basic认证拦截器:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
@Configuration
public class RabbitManageConfig {
@Value("${rabbitmq.manage.username}")
private String username;
@Value("${rabbitmq.manage.password}")
private String password;
@Bean
public RestTemplate rabbitManageRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
// 添加认证拦截器
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add((request, body, execution) -> {
// 构造Basic认证信息
String auth = username + ":" + password;
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
request.getHeaders().add("Authorization", "Basic " + encodedAuth);
return execution.execute(request, body);
});
restTemplate.setInterceptors(interceptors);
return restTemplate;
}
}
实现资源获取功能
完成认证配置后,就可以调用RabbitMQ管理API获取各类资源信息了,下面以获取队列信息和交换器信息为例展示具体实现。
定义资源实体类
首先定义队列信息的实体类,对应API返回的字段:
public class RabbitQueueInfo {
private String name;
private String vhost;
private boolean durable;
private boolean auto_delete;
private long messages;
private long messages_ready;
private long messages_unacknowledged;
// getter和setter方法省略
}
调用API获取队列列表
编写服务类,调用队列列表接口获取数据:
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@Service
public class RabbitResourceService {
@Autowired
private RestTemplate rabbitManageRestTemplate;
@Value("${rabbitmq.manage.host}")
private String host;
@Value("${rabbitmq.manage.port}")
private int port;
@Value("${rabbitmq.manage.api-base-path}")
private String apiBasePath;
private final ObjectMapper objectMapper = new ObjectMapper();
/**
* 获取所有队列信息
*/
public List<RabbitQueueInfo> getAllQueues() {
String url = "http://" + host + ":" + port + apiBasePath + "/queues";
try {
// 调用API获取原始JSON数据
String response = rabbitManageRestTemplate.getForObject(url, String.class);
// 转换为实体列表
return objectMapper.readValue(response, new TypeReference<List<RabbitQueueInfo>>() {});
} catch (Exception e) {
throw new RuntimeException("获取RabbitMQ队列信息失败", e);
}
}
}
获取交换器信息示例
获取交换器信息的实现和队列类似,只需要调整接口路径和对应的实体类即可:
public class RabbitExchangeInfo {
private String name;
private String vhost;
private String type;
private boolean durable;
private boolean auto_delete;
// getter和setter方法省略
}
// 在RabbitResourceService中添加方法
public List<RabbitExchangeInfo> getAllExchanges() {
String url = "http://" + host + ":" + port + apiBasePath + "/exchanges";
try {
String response = rabbitManageRestTemplate.getForObject(url, String.class);
return objectMapper.readValue(response, new TypeReference<List<RabbitExchangeInfo>>() {});
} catch (Exception e) {
throw new RuntimeException("获取RabbitMQ交换器信息失败", e);
}
}
功能测试
编写测试类验证功能是否正常:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class RabbitResourceTest {
@Autowired
private RabbitResourceService resourceService;
@Test
public void testGetQueues() {
List<RabbitQueueInfo> queues = resourceService.getAllQueues();
System.out.println("队列数量:" + queues.size());
for (RabbitQueueInfo queue : queues) {
System.out.println("队列名称:" + queue.getName() + ",消息数量:" + queue.getMessages());
}
}
@Test
public void testGetExchanges() {
List<RabbitExchangeInfo> exchanges = resourceService.getAllExchanges();
System.out.println("交换器数量:" + exchanges.size());
for (RabbitExchangeInfo exchange : exchanges) {
System.out.println("交换器名称:" + exchange.getName() + ",类型:" + exchange.getType());
}
}
}
注意事项
在实际使用中需要注意以下几点:
- 如果RabbitMQ开启了防火墙,需要确保15672端口可以被访问
- 不要使用guest用户进行远程访问,建议创建专用用户并分配最小权限
- 调用API时注意处理异常,比如网络异常、权限不足等情况
- 如果不需要获取所有资源,可以在API路径中添加vhost等参数过滤,例如/api/queues/vhost_name可以获取指定虚拟主机的队列
Spring_BootRabbitMQ管理API认证访问资源获取修改时间:2026-07-01 22:48:52