当前位置:网站首页>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
边栏推荐
- Mongodb database shell command execution
- Introduction and advanced level of MySQL (II)
- GC垃圾回收器详解
- How to adjust the brightness of win11? Four methods of adjusting screen brightness in win11
- Introduction and advanced MySQL (III)
- EasyCVR接入设备后播放视频出现卡顿现象的原因分析及解决
- What kind of knowledge payment system functions are more conducive to the development of the platform and lecturers?
- Implementation of grayscale publishing with haproxy
- Two month software testing training scam? How to choose training institutions?
- Go exe generates icon version information
猜你喜欢
随机推荐
Introduction and advanced level of MySQL (6)
注意力机制及代码实现
Can I get employed after two months of software testing training?
使用自开发的代理服务器解决 SAP UI5 FileUploader 上传文件时遇到的跨域访问错误试读版
MYSQL入门与进阶(九)
[GXYCTF2019]StrongestMind
Go exe generates icon version information
Chinese enterprise service industry market in 2022
408 review strategy (strengthening stage)
【实战】用OpenCV实现页面扭曲矫正
Swiftui component how to implement textfield of hidden part of phone number mask (tutorial includes source code)
CTR click through rate prediction practice project of advertising recommendation!
Is two months of software testing training reliable?
微信安装包11年膨胀575倍,UP主:“98%的文件是垃圾”;苹果应用商店被曝大量色情App;四大科技巨头呼吁废除闰秒|极客头条...
A priori, a posteriori, likelihood
Example of observer mode of C -- ordering milk
Introduction and advanced level of MySQL (10)
2022 Niuke multi School Game 2 J. link with arithmetic progress (three points + enumeration)
One Hot编码是什么?为什么要用它,什么时候用它?
Win11电脑摄像头打开看不见,显示黑屏如何解决?








