A working demo usage of docker, docker-compose, mongodb, python3, docker-compose, mosquitto:
First usecase an api that generates random numbers and lists them
Second one deals with crud operations CRUD (create, read, update, delete) application over a user collection
The third one will use a MQTT server (Mosquitto) to allow to publish sensor updates over MQTT The updates will be saved in mongodb (/demo/sensors). It will also compute a running average for each sensor and publish it to a separate topic
The applications will run in parallel using docker-compose
- random_demo will run on port 80
- the crud on port 81
- MQTT service will run on default port 1883
Docker, docker-compose, python, flask microframework [Mosquitto MQTT] (https://mosquitto.org/) [Curl] (https://curl.haxx.se/)
- How to install docker: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04
- How to install docker compose: https://docs.docker.com/compose/install/
docker-compose up
For applications 1 and 2 after we start the server using the command above, we'll be testing the requests using linux [curl][https://curl.haxx.se/docs/manpage.html]
For the application 3 we'll be using mosquitto cli to test the pub-sub it's working
To install mosquitto cli tool:
sudo apt-get install mosquitto-clients
- Random service
The random number collection has only one documents with '_is' lasts and an "items" key that will be a capped array
- Generate a random number between 10 and 100:
curl -i "http://localhost/random/10/100"
- View last 5 generated numbers list:
curl -i "http://localhost/random-list"
- CRUD service
The user collections contains _id which is the userid, a name and a email:
- PUT request, this request will add a user with a userid, name and email:
example: add a new user with name dan email [email protected] and userid 189
curl -X PUT -d [email protected] -d name=dan http://localhost:81/users/189
- POST request, this request will modify a user name or email or both:
example, modify the email of the user with id 10
curl -X POST -d [email protected] http://localhost:81/users/10
- GET request, this request will output as json the id, name and email of the user
example for with id 1:
curl -i "http://localhost:81/users/1"
- DELETE request, this request will delete a user with a specified id
example for with id 1:
curl -X DELETE -i "http://localhost:81/users/1"
- MQTT service
- To update a sensor with id: "some_sensor" and value "some_value" use:
mosquitto_pub -h localhost -p 1883 -d -t some_sensor -m some_value
This will publish to mosquitto in a topic named "some_sensor" and value "some_value".
Our python script will listen to this topic too, and save in the mongo sensors collections the value for the sensor in a capped array.
After it writes the new values, it reads the last 5 values and computes an average (running average) and publish it to topic "average/some_sensor"
- To get updates for sensor "some_sensor" use:
mosquitto_sub -h localhost -p 1883 -d -t some_sensor
This will just listen for updates for the topic "some_sensor"
- To get updates for the average 5 values for sensor "some_sensor" use:
mosquitto_sub -h localhost -p 1883 -d -t averages/some_sensor
This will just listen for updates for the topic "averages/some_sensor" and get a running average for the sensor "some_sensor". Each time someone publishes a new value for a sensor, the python script will calculate the average values of last 5 values and publishes it