【飛凌嵌入式 OK3399-C 開發板試用體驗】(安裝RabbitMQ消息隊列服務】

原創 作者 fcb5511 2020-09-30 13:29:00 RK3399 RK3399開發板 3399開發板

作者:fcb5511

提供兩種消息隊列服務安裝方式:

一、通過apt命令安裝
二、通過官方源碼以及binary包安裝

apt命令安裝方式獲取的erlang和rabbitmqserver版本一般比較低,erlang版本和rabbitmqserver版本如果匹配不好,在高并發,多連接的情況下,cpu以及內存消耗較大(不知道現在最新安裝版本有沒有這個問題,有興趣的可以試一試),優點是安裝和使用都很方便,下面是安裝和配置的各個步驟:

安裝RabbitMQServer需要的語言環境 erlang,執行如下命令
1apt-get installerlang-nox   安裝erlang

2erl  查看relang語言版本,成功執行則說明relang安裝成功


安裝RabbitMQServer消息隊列服務
apt-get install rabbitmq-server #安裝成功自動啟動
查看RabbitMQServer安裝狀態
systemctl status rabbitmq-server



啟用管理插件(可通過網頁登錄)
rabbitmq-plugins enable rabbitmq_management


rk3399瀏覽器中輸入localhost:15672,登陸用戶名和密碼分別為”guest””guest”


如果要實現局域網的其它計算機可訪問rk3399MQ服務,需要給MQ服務添加管理員以及管理員權限。
新建admin用戶并賦予權限
1rabbitmqctl add_useradmin admin
2rabbitmqctl set_user_tagsadmin administrator
3rabbitmqctl set_permissions-p / admin '.*' '.*' '.*'

局域網內計算機瀏覽器輸入192.168.1.109:15672,用戶名和密碼分別為”admin””admin”

源碼編譯和binary安裝需要從erlang官網下載源碼自行編譯為arm64平臺的安裝包,rabbitmqserver可直接從官網下載binaray包,手動添加環境變量。這種安裝方式優點是可以根據喜好安裝最新發布的版本。

github獲取erlang源碼包
wget  https://www.erlang.org/downloads/otp_src_22.1.tar.gz
解壓源碼包tar -zvxf otp_src_22.1.tar.gz #編譯&安裝 編譯前先安裝ssl相關的依賴庫,否則ok3399在啟動mq服務時會報錯apt-get install libssl-dev 接下來安裝erlangcd otp_src_22.1 ./otp_build autoconf./configure && make && sudo make install安裝不指定路徑會默認安裝到/usr/local/lib/erlang/目錄下

rabbitmqserver二進制包安裝
github獲取安裝包
wget
https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.0/rabbitmq-server-generic-unix-3.8.0.tar.xz

解壓
tar -xvf rabbitmq-server-generic-unix-3.8.0.tar.xz
將解壓后的包放到/usr/local目錄

配置環境變量
vim /etc/profile 在后面添加如下命令:export PATH=$PATH:/usr/local/lib/erlang/bin:/usr/local/rabbitmq_server-3.8.0/sbin讓新加的環境變量有效source /etc/profile
erl查看版本號


啟動rabbitmq-server
rabbitmq-plugins enable rabbitmq_management//允許網頁管理rabbitmq-server  rabbitmq-server -detached(后臺運行)
新建admin用戶并賦予權限
1rabbitmqctl add_useradmin admin
2rabbitmqctl set_user_tagsadmin administrator
3rabbitmqctl set_permissions-p / admin '.*' '.*' '.*'

至此,兩種方式安裝Rabbitmq服務完成。接下來用python來驗證一下功能,python實現一個消費者,一個生產者,生產間隔1秒鐘發送一幀數據,消費者從隊列取數據并打印。python使用rabbitmq需要引用pika庫,pika庫是獨立于python的第三方庫,需要使用pip命令安裝。
amqproducer.py程序:
import pika
import threading
import time
import signal
import os
class Producer(object):
    def __init__(self, host='127.0.0.1', port=5672,
                 user='admin', passwd='admin',
                 exchange='test_exchange', routing_key='test_key',
                 exchange_type= 'fanout', exhcange_durable=False, exchange_autodelete=True
                 ):
        self.host=host
        self.port=port
        self.user=user
        self.passwd=passwd
        self.exchange=exchange
        self.routing_key=routing_key
        self.exchange_type=exchange_type
        self.exhcange_durable=exhcange_durable
        self.exchange_autodelete=exchange_autodelete
        self.connection=None
        self.channel=None
    def CreateConnection(self):
        try:
            self.credentials=pika.PlainCredentials(self.user, self.passwd)
            self.connection = pika.BlockingConnection(pika.ConnectionParameters(host = self.host, port = self.port, virtual_host = '/', credentials=self.credentials))  
            self.channel = self.connection.channel()   
           
            self.channel.exchange_declare(exchange_type=self.exchange_type, exchange = self.exchange, durable = self.exhcange_durable, auto_delete=self.exchange_autodelete)
        except Exception as e:
            print("occurring an exception when the producer executing the function 'CreateConnection()':", e)
    def PublisMessage(self, Message):
        try:
            self.channel.basic_publish(exchange=self.exchange, routing_key=self.routing_key, properties=pika.BasicProperties(delivery_mode = 2), body=Message)
        except Exception as e:
           print("An exception occurred when publishing message:", e)
        


amqconsumer.py程序:

import pika
import time
class Consumer(object):
    def __init__(self, host='127.0.0.1', port=5672,
                 user='admin', passwd='admin',
                 queue='test_queue', exchange='test_exchange', routing_key='test_key',
                 queue_exclusive=False, queue_durable=False, queue_autodelete=True,
                 exchange_type= 'fanout', exhcange_durable=False, exchange_autodelete=True
                 ):
        self.host=host
        self.port=port
        self.user=user
        self.passwd=passwd
        self.queue=queue
        self.exchange=exchange
        self.routing_key=routing_key
        self.queue_exclusive=queue_exclusive
        self.queue_durable=queue_durable
        self.queue_autodelete=queue_autodelete
        self.exchange_type=exchange_type
        self.exhcange_durable=exhcange_durable
        self.exchange_autodelete=exchange_autodelete
        self.connection=None
        self.channel=None
        
    def CreateConnection(self):
        try:
            self.credentials=pika.PlainCredentials(self.user, self.passwd)
            self.connection = pika.BlockingConnection(pika.ConnectionParameters(host = self.host, port = self.port, virtual_host = '/', credentials=self.credentials))
            self.channel = self.connection.channel()
            self.channel.queue_declare(queue=self.queue, durable=self.queue_durable, exclusive=self.queue_exclusive, auto_delete=self.queue_autodelete)
            
            self.channel.exchange_declare(exchange_type=self.exchange_type, exchange = self.exchange, durable = self.exhcange_durable, auto_delete=self.exchange_autodelete)
            self.channel.queue_bind(queue = self.queue, exchange = self.exchange, routing_key = self.routing_key)
            self.channel.basic_consume(on_message_callback = self.on_response, auto_ack=True, queue=self.queue)
        except Exception as e:
            print("occurring an exception when the consumer  RecvData:", e)
            
    def on_response(self, ch, method, props, body):
            #print(" [%s] Received %r" % (time.time(), body))
            print(time.strftime('%H:%M:%S',time.localtime(time.time()))+': Get Message from '+self.host + ':'+body.decode())
    def ProcessConsumeEventBlock(self):
        try:
            self.channel.start_consuming()
        except Exception as e:
            print('An exception occurred when executing consuming, the exception is:',e)
    def ProcessConsumeEventNoneBlock(self):
        try:
            self.connection.process_data_events()
        except Exception as e:
            print('An exception occurred when executing consuming, the exception is:',e)

main.py程序:

import pika
import time
import threading
from amqconsumer import Consumer
from amqproducer import Producer
class ManageMent:
    def __init__(self):
        self.producer = Producer(host='localhost',user='guest',passwd='guest')
        self.consumer = Consumer(host='localhost',user='guest',passwd='guest')
        
   
    def CreateSever(self):
        self.consumer.CreateConnection()
        self.producer.CreateConnection()
   
    def ProducerRun(self):
        while True:
           self.producer.PublisMessage('this message is used for testing RabbitMQ')  
           time.sleep(0.3)
    def ConsumerRun(self):
        self.consumer.ProcessConsumeEventBlock()
        
    def Run1(self):
        while True:     
            self.producer.PublisMessage('this message is used for test RabbitMQ')  
            self.consumer.ProcessConsumeEventNoneBlock()
            time.sleep(1)
    def Run(self):
        self.Consumer_thread=threading.Thread(target=self.ConsumerRun)
        self.Producer_thread=threading.Thread(target=self.ProducerRun)
        self.Consumer_thread.start()
        self.Producer_thread.start()
  
if __name__ == '__main__':
    manage=ManageMent()
    manage.CreateSever()
    manage.Run()


最終運行效果如圖


此貼為基礎功能,下次將mq數據與qt界面結合,實現動態的界面。

相關產品 >

  • FET3399-C核心板

    飛凌RK3399安卓高性能核心板采用 采用六核Rockchip RK3399芯片,雙Cortex-A72大核+四Cortex-A53小核結構,對整數、浮點、內存等作了大幅優化,在整體性能、功耗及核心面積三個方面提升。以下將對瑞芯微芯片RK3399參數,RK3399核心板方案及其性能做具體介紹。如您對飛凌RK3399系列核心板有興趣,歡迎咨詢了解。

    了解詳情
    FET3399-C核心板
  • OK3399-C開發板

    飛凌嵌入式RK3399安卓開發板主芯片采用高性能六核CPU Rockchip RK3399,GPU采用Mail-T860四核 GPU,RK3399作為目RK產品線中低功耗、高性能的代表,可滿足人臉識別設備、機器人、無人機、IoT物聯網領域應用。飛凌RK3399開發板在整體性能、功耗及核心面積做了大幅度優化,更加滿足工業設計需求。飛凌RK3399開發板為進一步減少用戶二次開發難度,開放了底板原理圖,并提供了RK3399用戶手冊、芯片手冊,加上優質的技術服務,讓您的方案從構思到上市時間縮短。

    了解詳情
    OK3399-C開發板

推薦閱讀 換一批 換一批