基于小熊派Hi3861鸿蒙开发的IoT物联网学习【五】
BearPi-HM_Nano开发板鸿蒙OS内核编程开发——消息队列
什么是消息队列?
答:消息队列中间件是分布式系统中重要的组件,主要解决应用耦合、异步消息、流量削锋等问题。实现高性能、高可用、可伸缩和最终一致性架构。是大型分布式系统不可缺少的中间件。
目前在生产环境,使用较多的消息队列有ActiveMQ、RabbitMQ、ZeroMQ、Kafka、MetaMQ、RocketMQ等。
Message Queue
MQ三大优势:解耦、异步、流量消峰
2.1 解耦:降低耦合度
2.2 异步:系统A把数据写到MQ里,系统B、C、D就自己去拿,慢慢消费就行了。
2.3 流量削锋
下游系统下单时只需要往MQ里发消息,我的订单系统可以设定消费的频率,比如每秒我就消费2000个消息(在数据库的可承受范围),
不管你下游系统每秒下多少单,我都保持这个速率,既不会影响我订单系统的数据库,也不影响你下游系统的下单操作,很好的保护了系统,也提高了下单的吞吐量。
#include#include <string.h> #include //在Message_example函数中,通过osMessageQueueNew()函数创建了消息队列ID,#include "ohos_init.h" #include "cmsis_os2.h" //number of Message Queue Objects #define MSGQUEUE_OBJECTS 16 typedef struct { //object data type char *Buf; uint8_t Idx; } MSGQUEUE_OBJ_t; MSGQUEUE_OBJ_t msg;
//Thread_MsgQueue1()函数中通过osMessageQueuePut()函数向消息队列中发送消息。
//在Thread_MsgQueue2()函数中通过osMessageQueueGet()函数读取消息队列中的消息比打印出来。
//message queue id osMessageQueueId_t mid_MsgQueue; void Thread_MsgQueue1(void *argument) { (void)argument; //do some work... msg.Buf = "Hello BearPi-HM_Nano!"; msg.Idx = 0U; while (1) { osMessageQueuePut(mid_MsgQueue, &msg, 0U, 0U); //suspend thread osThreadYield(); osDelay(100); } } void Thread_MsgQueue2(void *argument) { (void)argument; osStatus_t status; while (1) { //Insert thread code here... //wait for message status = osMessageQueueGet(mid_MsgQueue, &msg, NULL, 0U); if (status == osOK) { printf("Message Queue Get msg:%s\n", msg.Buf); } } } static void Message_example(void) { //函数osMessageQueueNew创建并初始化一个消息队列对象。该函数返回消息队列对象标识符,如果出现错误则返回NULL,
//可以在RTOS启动(调用 osKernelStart)之前安全地调用该函数,也可以在内核初始化 (调用 osKernelInitialize)之前调用该函数。
mid_MsgQueue = osMessageQueueNew(MSGQUEUE_OBJECTS, 100, NULL);
if (mid_MsgQueue == NULL)
{
printf("Falied to create Message Queue!\n");
}
osThreadAttr_t attr;
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 1024 * 10;
attr.priority = 25;
attr.name = "Thread_MsgQueue1";
if (osThreadNew(Thread_MsgQueue1, NULL, &attr) == NULL)
{
printf("Falied to create Thread_MsgQueue1!\n");
}
attr.name = "Thread_MsgQueue2";
if (osThreadNew(Thread_MsgQueue2, NULL, &attr) == NULL)
{
printf("Falied to create Thread_MsgQueue2!\n");
}
}
APP_FEATURE_INIT(Message_example);
最后的运行结果:
### 运行结果"section18115713118"> 示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,会打印从消息队列中获取的消息。 ```c Message Queue Get msg:Hello BearPi-HM_Nano! Message Queue Get msg:Hello BearPi-HM_Nano! Message Queue Get msg:Hello BearPi-HM_Nano! Message Queue Get msg:Hello BearPi-HM_Nano! Message Queue Get msg:Hello BearPi-HM_Nano! ```