Make MicroService that uses MQTT in sango and mqttcli

MicroService but is where subtly becoming Bazuri, here sango and mqttcli in MQTT Let's make a MicroService with.

  • (Note 1 : MicroService of the definition does not know well)
  • (Note 2 : Since a place often beat and mqttcli Wawari, please look in mind a wide even if every other problem)

Preparation

A small preparation is necessary beforehand.

1. Create sango account

First sango to create an account from. You can create it immediately if you have a GitHub account.

When you open the dashboard,

  • Destination : Mqtt: // <MQTT server address>: 1883
  • Access destination topic : shirou Atto github / #
  • Username : shirou Atto github
  • Password : secret <- appears when you click

So I will remember these.

2. Download mqttcli

https://github.com/shirou/mqttcli traced from drone.io to Because it is binary for various architecture is put , I will download it.

If necessary, chmod ugo+x please with the execution authority, and the like.

3. Describe the configuration file

~/.mqttcli.cfg to create, and then described the following contents.

{
  "host": "mqtt.shiguredo.jp", <--- MQTT server address
  "username": "shirou@github",  <--- user name
  "password": "<password>"
}

That's all there is to it. Let's test the connection.

4. Connection test

mqttcli sub -t "shirou@github/#" -d

Beat it,

INFO[0000] Broker URI: tcp://mqtt.shiguredo.jp:1883
INFO[0000] Connecting...
INFO[0000] Connected
INFO[0000] Topic: shirou@github/#

And if it comes out OK.

Make a MircoService

Let's create a MicroService.

$ tail -f /var/nginx/access.log | mqttcli pub -t "shirou@github/nginx/log" -s

sending nginx log to "shirou@github/nginx/log" topic is ready to serve MicroService.

Please input as follows on another terminal. The log of nginx is described in LTSV. Please change other people as appropriate

$ mqttcli sub -t "shirou@github/nginx/log" | \
  grep "status:500" --line-buffered | \
  mqttcli pub -t "shirou@github/nginx/log/500" -s

Yes, this send request to "shirou@github/nginx/log/500" only status is 500 were MicroService is finished. ( --line-bufferd not put and do not know immediately is buffered)

As you like the rest. For example, you can write it to a file.

$ mqttcli sub -t "shirou@github/nginx/log/500" > /var/log/error.log

Of course, all of these can be executed on the same host, or they can be different hosts.

further

Currently sango can only have up to six connections. However, more connections can be made in the paid version that is planned in the future (currently 50 plans).

Then,

  • We handle other status codes as well as 400
  • Send multiple server logs to the same topic and put together
  • It not only exports to a file, but also mails it and sends it to slack

You can create a MicroService such as. Topics also nginx/log/4/400 400`` and nginx/log/4/400 400`` leave sent as a separate that, nginx/log/4/# summarized to subscribe , And so on.

These MicroService topic against and Topic Pub/Sub , and # and + the when combined in various forms, I think in many more things can be.

Summary

This time using sango and mqttcli

  1. Send log of nginx
  2. Retrieve only the request with the status 500 and resubmit it
  3. Receive and write to file

We have created three MicroServices.

Since it is standard input and output, various applications work well. Of course there is no problem separating the hosts. Moreover, the response is very fast with lightweight MQTT.

Please use sango and try out various things!

Digression

In this way, MQTT seems to be compatible with streaming processing, is not it? spark streaming Toka?