当前位置:网站首页>How does the mqtt server built with emqx forward data and save it to the cloud database?
How does the mqtt server built with emqx forward data and save it to the cloud database?
2022-07-28 18:58:00 【Let's GO Tiger】
Record your own solutions to this problem !
One 、 Technical principle
Create a new resource interface to forward the specified data , Then set rules for filtering subject messages in the rule engine , Filter out the messages of the topic you need , Set forwarded http Interface (URL),http Request to select POST or GET request , The requirement is to write servlet Interface with doPost()、doGet() Methods corresponding to the .servlet The logic in the interface is to add, delete, modify and query the database , Of course servlet The interface is also deployed in the cloud !
Two 、 Implementation steps
1、 Create a new resource interface :

open EMQX The background of , Create a new resource , Resource type selection webhook, resources ID You can modify it yourself or not , Fill in url, Here I use tomcat Server deployment servlet Interface , The port is 8080, If you use other servers to deploy , Please fill in the port of the server !
2、 Set the subject message filtering rules and configure the forwarding resource interface :

Rules of choice , Click to create a new rule . Here you need to write a filter for topic messages SQL sentence .
there payload It's a message object , Every message received by the topic has the following properties , The data we transmit is payload in , It is json Format , So when uploading your data, you'd better also use json Format .
{
"username": "u_emqx",
"topic": "smartHome",
"timestamp": 1614515277516,
"qos": 1,
"publish_received_at": 1614515277516,
"peerhost": "127.0.0.1",
"payload": "{\"t\":\"light\",\"light\":\"20\"}",
"node": "[email protected]",
"metadata": {
"rule_id": "test_rule4a19e8ea"
},
"id": "5BC64A199D064F44072000AB80000",
"flags": {
"sys": true,
"event": true
},
"clientid": "c_emqx"
}
my payload The first data is the sensor type , The second data is the real data collected by the sensor , You can design according to your own situation , Then click test , If the message sent meets the setting SQL sentence , You can see the output result .
After finding the message that conforms to the rules , Add a corresponding action , Type select to send data to web service , Associate resources and select the resource interface you just created , Just leave the message content template blank by default !
That's it MQTT The server forwards data to the specified interface , The next step is to write servlet interface program 
3、 To write servlet interface program
It may take a little bit here JavaWeb Basics , Will use doPost() and doGet() The method is almost done , The rest is the addition, deletion, modification and query of the database .
If you select POST request , Then in doPost() Get from method MQTT Data forwarded by the server , Because with http Protocol transfer , The data will be passed out in the form of parameters ,POST The requested parameters are obtained in the request body , Get is json Formatted data ;
If you choose GET request , It's just doGet() Method , Data is also passed in the form of parameters , But it is transmitted in clear text , So directly in doGet() Method, just call the function to get the request parameters .
Because there are many types of sensors , Need to match multiple servlet interface program , So you need to be in web.xml Next, set the mapping relationship , For example, enter... In the address field :http://ip:8080/ Project name /light, It will match you LightServlet Deal with your request , Because I've put light and LightServlet The binding . Did you learn JavaWeb You should know this .
4、 Deploy in the cloud servlet Interface
Because I haven't learned much Linux, Therefore, it is suggested that you can use the pagoda panel to install tomcat The server and MySQL database , About the installation of pagoda panels , Baidu has many !
Install well tomcat After the server , find tomcat In the catalog webapp Catalog , And then put what's written Servlet Interface upload here , It will automatically decompress !
stay server.xml Middle configuration , As for why , There are three kinds on Baidu tomcat How to deploy the project !
5、 test servlet Interface

stay EMQX The test function is provided in the tools menu in the background of , Click on websocket, Connect mqtt After the server , Enter the simulated message , Click Send , You can see mysql Two rows of data are added to the light intensity table in the data , prove servlet Interface is ok Of .
This is just to add data , Other queries , The modification function is also implemented in this way . That's my personal right EMQX Just a little insight into forwarding data to the database !
My own functions are as follows :
The source code is also for you , Interested friends can study it !
Address :https://github.com/ldcowq/smartHome
边栏推荐
- How to adjust the brightness of win11? Four methods of adjusting screen brightness in win11
- Live broadcast platform software development, JS implementation by alphabetical order
- The open source of "avoiding disease and avoiding medicine" will not go far
- MYSQL入门与进阶(七)
- Interviewer: what are the usage scenarios of ThreadLocal? How to avoid memory leakage?
- Unity 之 切换语言导致报错:System.FormatException:String was not recognized as a valid DateTime.
- Pyqt5 rapid development and practice 5.3 multithreading
- Win11电脑摄像头打开看不见,显示黑屏如何解决?
- 112. Use the self-developed proxy server to solve the cross domain access error encountered when uploading files by SAP ui5 fileuploader
- Xiaobai must see the development route of software testing
猜你喜欢

Win11电脑摄像头打开看不见,显示黑屏如何解决?

Getting started with gateway

MYSQL入门与进阶(八)

Configuration tutorial: how does the organizational structure of the new version of easycvr (v2.5.0) cascade to the superior platform?

What does real HTAP mean to users and developers?

What is one hot code? Why use it and when?

My creation anniversary -- July 25th, 2022

1.2、队列

Redis cache avalanche, penetration, breakdown, bloom filter, detailed explanation of distributed lock

What kind of knowledge payment system functions are more conducive to the development of the platform and lecturers?
随机推荐
MYSQL入门与进阶(三)
The open source of "avoiding disease and avoiding medicine" will not go far
CTR click through rate prediction practice project of advertising recommendation!
Configuration tutorial: how does the organizational structure of the new version of easycvr (v2.5.0) cascade to the superior platform?
全新升级!《云原生架构白皮书 2022 版》重磅发布
MYSQL入门与进阶(四)
kotlin:Nothing
Zero knowledge proof: zkp with DDH assumption
Meta Q2 earnings: revenue fell for the first time, and metaverse will compete with apple
[GXYCTF2019]StrongestMind
kotlin:out in
Can I get employed after two months of software testing training?
Introduction and advanced MySQL (4)
408 review strategy (strengthening stage)
[actual combat] realize page distortion correction with OpenCV
MySQL date function
2022-07-27 第四小组 修身课 学习笔记(every day)
Cause analysis and solution of video jam after easycvr is connected to the device
What kind of knowledge payment system functions are more conducive to the development of the platform and lecturers?
LVS manual