1、说明:
RabbitMQ的控制台界面每个版本的差别不大,而且网上存在大量的文章进行说明,我挑了一篇相对比较详细,大家可以看下 RabbitMQ管理页面各种属性详解
2、如何更好更快的了解管理页面?
为了更好的了解管理图表,大家可以启动一个生产者,代码如下:
##application.yml
server:
port: 8021
spring:
#给项目来个名字
application:
name: rabbitmq-provider
#配置rabbitMq 服务器
rabbitmq:
host: 192.10.214.1
port: 6005
username: root
password: pnadTk3m
//pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dai</groupId>
<artifactId>rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rabbitmq</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
//常量
package com.dai.rabbitmq.constants;
/**
* <h1>BaseConstatns</h1>
* @author daihuhu
* @date 2021/11/25
*/
public class Constants {
public static final String HZ_EXCHANGE = "dai_alarm";
public static final String HZ_ROUTING_KEY = "dai";
public static final String HZ_QUEUE = "multalarm_dai";
}
//配置类
package com.dai.rabbitmq.config;
import com.dai.rabbitmq.constants.Constants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* <h1>DirectRabbitConfig</h1>
* @author daihuhu
* @date 2021/11/25
*/
@Configuration
public class DirectRabbitConfig {
/**定制:接收安徽省厅合众推送的结构化告警数据*/
@Bean
public Queue directQueueHZ() {
return new Queue(Constants.HZ_QUEUE);
}
/**Direct交换机*/
@Bean
DirectExchange directExchangeHZ() {
return new DirectExchange(Constants.HZ_EXCHANGE);
}
/**绑定 将队列和交换机绑定, 并设置用于匹配键*/
@Bean
Binding bindingDirectExchangedHZ(Queue directQueueHZ, DirectExchange directExchange) {
return BindingBuilder.bind(directQueueHZ).to(directExchange).with(Constants.HZ_ROUTING_KEY);
}
}
// An highlighted block
package com.dai.rabbitmq.task;
import com.alibaba.fastjson.JSONObject;
import com.dai.rabbitmq.constants.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.UUID;
/**
* <h1>ScheduleSendMessage</h1>
*
* @author daihuhu
*/
@Component
public class ScheduleSendMessage {
/**
* 使用RabbitTemplate,提供了接收/发送等等方法
*/
@Autowired
RabbitTemplate rabbitTemplate;
@Scheduled(cron = "0/10 * * * * *")
public void scheduleSendMessage() {
JSONObject map = new JSONObject();
map.put("book_name","Java编程思想")
map.put("author", "Bruce Eckel");
map.put("happenedTime", System.currentTimeMillis());
JSONObject attribute = new JSONObject();
attribute.put("111","wangdan");
attribute.put("222","gaofang");
attribute.put("333","caoye");
attribute.put("444","anyumeng");
attribute.put("deskmate","yixueer");
attribute.put("girlfriend","mengli");
map.put("attribute",attribute);
String jsonString = JSONObject.toJSONString(map);
rabbitTemplate.convertAndSend(Constants.HZ_EXCHANGE, Constants.HZ_ROUTING_KEY, jsonString);
System.out.println(new Date() + "发送成功");
}
}
// 启动类
package com.dai.rabbitmq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class RabbitmqApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitmqApplication.class, args);
}
}