Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to bridge messages to RabbitMQ in EMQ X rules engine

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to bridge messages to RabbitMQ in the EMQ X rules engine. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Scene introduction

This scenario requires that messages that meet the criteria under the topic specified by EMQ X be stored in RabbitMQ.

RabbitMQ is an open source implementation of AMQP message middleware developed by Erlang, which is mainly used for decoupling between components. The sender of the message does not need to know the existence of the message consumer, and vice versa. RabbitMQ is used to store and forward messages in distributed systems, which performs well in terms of ease of use, throughput, scalability, high availability and so on. In EMQ X million-level message throughput scenarios, RabbitMQ is one of the best persistence / bridging solutions.

In this scenario, the information reported by the device end is as follows:

Report topic: cmd/state/:id, in which id represents the vehicle client identification number

Message body:

{"id": "NXP-058659730253-963945118132721-22", / / client identification code "speed": 32.12, / / vehicle speed "direction": 198.33212, / / driving direction "tachometer": 3211, / / engine speed When the value is greater than 8000, you need to store "dynamical": 8.93, / / instantaneous fuel consumption "location": {/ / GPS longitude and latitude data "lng": 116.296011, "lat": 40.005091}, "ts": 1563268202 / / reporting time}

When the reported data engine speed value is greater than 8000, the current information is stored for subsequent analysis of the user's vehicle usage.

Preparatory work

For more information on local installation of RabbitMQ, please see download and install RabbitMQ tutorials. It is recommended to download a separate binary package for developers. After decompression, enter the software directory and start rabbitmq-server on the command line:

. / sbin/rabbitmq-server configuration instructions for creating resources

Open EMQ X Dashboard, go to the resources page of the left menu, click the New button, and type the RabbitMQ server information to create resources.

The network environment of the nodes in the EMQ X cluster may not be connected to each other. After the resources are created, click the status button in the list to view the connection status of each node resources. If the resources on the node are not available, check whether the configuration and network connectivity are correct, and click the reconnect button to reconnect manually.

Create a rule

Go to the rules page of the left menu and click the New button to create the rules. Here, we choose to trigger the event message release, and trigger the rule for data processing when the message is released.

After the trigger event is selected, we can see the optional fields and sample SQL on the interface:

Filter the required fields

The rule engine uses SQL statements to process rule conditions. In this business, we need to select all the fields in payload separately and select them in payload.fieldName format. We also need the topic, qos and id information of the message context. The current SQL is as follows:

SELECT payload.id as client_id, payload.speed as speed, payload.tachometer as tachometer, payload.ts as ts, idFROM "message.publish" WHERE topic = ~ 'tUniverse'to establish the screening conditions

Use the SQL statement WHERE sentence for conditional filtering. In this business, we need to define two conditions:

Only deal with cmd/state/:id topics and filter topic with the topic wildcard = ~: topic = ~ 'cmd/state/+'

Only messages with tachometer > 8000 are processed and tachometer is filtered using comparators: payload.tachometer > 8000

The SQL obtained from the previous step is as follows:

SELECT payload.id as client_id, payload.speed as speed, payload.tachometer as tachometer, payload.ts as ts, idFROM "message.publish" WHERE topic = ~ 'cmd/state/+' AND payload.tachometer > 8000 use the SQL test function for output testing

With the SQL testing function, we can view the current SQL processed data output in real time, which requires us to specify payload and other simulation raw data.

The payload data is as follows. Be careful to change the size of the tachometer value to meet the SQL condition:

{"id": "NXP-058659730253-963945118132721-22", "speed": 32.12, "direction": 198.33212, "tachometer": 9001, "dynamical": 8.93, "location": {"lng": 116.296011, "lat": 40.005091}, "ts": 1563268202}

Click the SQL test switch button, change topic and payload to the information in the scenario, and click the test button to view the data output:

The test output data is:

{"client_id": "NXP-058659730253-963945118132721-22", "id": "589A429E9572FB44B0000057C0001", "speed": 32.12, "tachometer": 9001, "ts": 1563268202}

The test output is as expected, and we can take the next steps.

Add response action to bridge messages to RabbitMQ

After the input and output of the SQL condition is correct, we continue to add the response action, configure the write-related parameters, and store the filter results to RabbitMQ.

Click the add button in the response action, select the bridge data to the RabbitMQ action, select the resource you just selected, and fill

RabbitMQ Exchange,Exchange Type and Routing Key.

Test expected results

We successfully created a rule that contains a processing action, and the desired effect of the action is as follows:

When the device reports a message to the cmd/state/:id topic, the SQL will be hit when the tachometer value in the message exceeds 8000, and the hit number in the rule list will be increased by 1.

Subscribers to RabbitMQ will receive a piece of data with the same value as the current message.

Test using the Websocket tool in Dashboard

Switch to the tool-> Websocket page, use any information client to connect to EMQ X, and send the following information on the message card after the connection is successful:

Topic: cmd/state/NXP-058659730253-963945118132721-22

Message body:

{"id": "NXP-058659730253-963945118132721-22", "speed": 32.12, "direction": 198.33212, "tachometer": 9001, "dynamical": 8.93, "location": {"lng": 116.296011, "lat": 40.005091}, "ts": 1563268202}

Click the send button, and after sending it successfully, you can see that the statistical value of the current rule is 1.

Use the command line to view the data table to record the data as follows:

So far, we have implemented the business development that uses the rule engine to bridge messages to RabbitMQ through the rule engine.

Thank you for reading! This is the end of the article on "how to bridge messages to RabbitMQ in EMQ X rule engine". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report