持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第13天,点击查看活动详情
上一篇主要讲的时mongodb存消息数据,这一篇主要讲利用RabbitMQ削峰添谷。
使用RabbitMQ推送消息,以及削峰填谷
引入消息队列MQ,然后在Java后端系统上面,用异步多线程的方法,向消息队列MQ中发送消息,这样Web系统发布公告消息的时候就不占用数据库正常的CRUD操作。每当用户登入时,去查询所在MQ上的消息,并进行入库操作。使用同步接收,采用轮询的方式接收消息。
引入依赖
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
创建RabbitMQ配置类
连接RabbitMQ需要用到ConnectionFactory,所以我们要自己创建好ConnectionFactory对象然后注册给Spring框架,这就需要我们创建RabbitMQConfig类。
@Configuration
public class RabbitMQConfig {
@Bean
public ConnectionFactory getFactory() {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.99.101"); //Linux主机的IP地址
factory.setPort(5672); //RabbitMQ端口号
return factory;
}
}
创建消息任务类
以前我们使用异步多线程的方式发送邮件,那么这次我们要创建的多线程任务类是用来收发RabbitMQ消息的,而且内部包含了同步执行和异步执行两种方式。
@Slf4j
@Component
public class MessageTask {
@Autowired
private ConnectionFactory factory;
@Autowired
private MessageService messageService;
/**
* 同步发送消息
*
* @param topic 主题
* @param entity 消息对象
*/
public void send(String topic, MessageEntity entity) {
String id = messageService.insertMessage(entity); //向MongoDB保存消息数据,返回消息ID
//向RabbitMQ发送消息
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
//连接到某个Topic
channel.queueDeclare(topic, true, false, false, null);
HashMap header = new HashMap(); //存放属性数据
header.put("messageId", id);
//创建AMQP协议参数对象,添加附加属性
AMQP.BasicProperties properties = new AMQP.BasicProperties().builder().headers(header).build();
channel.basicPublish("", topic, properties, entity.getMsg().getBytes());
log.debug("消息发送成功");
} catch (Exception e) {
log.error("执行异常", e);
throw new EmosException("向MQ发送消息失败");
}
}
/**
* 异步发送消息
*
* @param topic 主题
* @param entity
*/
@Async
public void sendAsync(String topic, MessageEntity entity) {
send(topic, entity);
}
/**
* 同步接收数据
*
* @param topic 主题
* @return 接收消息数量
*/
public int receive(String topic) {
int i = 0;
try (//接收消息数据
Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
// 从队列中获取消息,不自动确认
channel.queueDeclare(topic, true, false, false, null);
//Topic中有多少条数据未知,所以使用死循环接收数据,直到接收不到消息,退出死循环
while (true) {
//创建响应接收数据,禁止自动发送Ack应答
GetResponse response = channel.basicGet(topic, false);
if (response != null) {
AMQP.BasicProperties properties = response.getProps();
Map<String, Object> header = properties.getHeaders(); //获取附加属性对象
String messageId = header.get("messageId").toString();
byte[] body = response.getBody();//获取消息正文
String message = new String(body);
log.debug("从RabbitMQ接收的消息:" + message);
MessageRefEntity entity = new MessageRefEntity();
entity.setMessageId(messageId);
entity.setReceiverId(Integer.parseInt(topic));
entity.setReadFlag(false);
entity.setLastFlag(true);
messageService.insertRef(entity); //把消息存储在MongoDB中
//数据保存到MongoDB后,才发送Ack应答,让Topic删除这条消息
long deliveryTag = response.getEnvelope().getDeliveryTag();
channel.basicAck(deliveryTag, false);
i++;
} else {
break; //接收不到消息,则退出死循环
}
}
} catch (Exception e) {
log.error("执行异常", e);
}
return i;
}
/**
* 异步接收数据
*
* @param topic
* @return
*/
@Async
public int receiveAysnc(String topic) {
return receive(topic);
}
/**
* 同步删除消息队列
*
* @param topic 主题
*/
public void deleteQueue(String topic) {
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.queueDelete(topic);
log.debug("消息队列成功删除");
} catch (Exception e) {
log.error("删除队列失败", e);
throw new EmosException("删除队列失败");
}
}
/**
* 异步删除消息队列
*
* @param topic 主题
*/
@Async
public void deleteQueueAsync(String topic) {
deleteQueue(topic);
}
}
如何使用
现在到这一步,我们就可以在我们的代码中使用RabbitMQ进行消息的首发了。
发送消息
MessageEntity entity = new MessageEntity();
entity.setSenderId(0);
entity.setSenderName("系统消息");
entity.setUuid(IdUtil.simpleUUID());
entity.setMsg("消息主体内容");
entity.setSendTime(new Date());
messageTask.sendAsync(id + "", entity);
}
接收消息
messageTask.receiveAysnc(id+"");