Real-Time Temperature Monitoring with Go and gRPC ( Twilio Integration, MongoDB, weatherAPI, Plot diagrams, Jaeger - Grafana - Loki ) π
APACHE-2.0 License
Questo progetto dimostra l'integrazione di servizi gRPC con il linguaggio Go, fornendo un esempio completo di un server gRPC che serve dati di temperatura e un client gRPC che li consuma, il tutto con una gestione avanzata delle metriche, connessione a MongoDB per persistere i dati, funzionalitΓ di allerta tramite Twilio. Il progetto utilizza Docker per facilitare l'impostazione dell'ambiente, inclusi la configurazione di MongoDB e altre dipendenze tipo jaeger.
Click here for the English version
βββ api --> qua dentro il file .proto
β βββ protos
β β βββ temperature.proto
β βββ api.md
βββ docker ---> tutto cio' relativo a docker
β βββ docker-compose.yml
β βββ init-mongo.js
β βββ loki-config.yml
β βββ otel-collector-config.yaml
β βββ prometheus.yml
βββ extra --> immagini del progetto
βββ pkg --> pkg tutto quello che deve essere visibile e condiviso
β βββ telemetry
β βββ temperature
βββ temperature_grpc_client -> il Client GRPC
β βββ cmd
β βββ plot
β βββ service
β βββ client.md
βββ temperature_grpc_server --> il Server GRPC
β βββ alert_twilio
β β βββ alert.go
β βββ cmd
β β βββ server
β β β βββ main.go
β βββ mongodb --> connessione a MONGODB
β β βββ config
β β βββ temperature_repo
β βββ service
β β βββ server.go
β βββ server.md
βββ .editorconfig
βββ .gitignore
βββ changelog.md
βββ go.mod
βββ go.sum
Prima di eseguire il progetto, Γ¨ necessario ottenere una chiave API per utilizzare il servizio WeatherAPI. Questo servizio Γ¨ completamente gratuito e puoi ottenere la tua chiave API seguendo questi passi:
Per mantenere la tua chiave API sicura e fuori dal codice sorgente, configurala come una variabile d'ambiente. Segui questi passi:
export WEATHER_API_KEY=la-tua-api-key
$env:WEATHER_API_KEY="la-tua-api-key"
Assicurati che la variabile d'ambiente WEATHER_API_KEY
sia impostata correttamente prima di procedere.
Assicurati di avere Docker installato e di avere accesso al file docker-compose.yml
:
docker-compose.yml
Se desideri inviare avvisi tramite Twilio, segui i seguenti passi per creare un account su Twilio e configurare le variabili d'ambiente:
Consulta: Twilio WhatsApp Quickstart (Go)
Configurare le seguenti variabili d'ambiente:
export TWILIO_ACCOUNT_SID=il-tuo-account-sid
export TWILIO_AUTH_TOKEN=il-tuo-auth-token
export TWILIO_PHONE_NUMBER=il-tuo-numero-di-telefono
export ALERT_PHONE_NUMBER=il-numero-di-telefono-per-gli-avvisi
Una volta configurata la chiave API, puoi avviare il server:
Naviga alla directory temperature_grpc_server
:
cd temperature_grpc_server/cmd/server
Esegui il server:
go run main.go
Il server inizierΓ ad ascoltare le richieste gRPC.
Con il server in esecuzione, ora puoi avviare il client per inviare richieste al server:
Apri un nuovo terminale e naviga alla directory temperature_grpc_client
:
cd temperature_grpc_client/cmd/client
Esegui il client:
go run main.go
Il client invierΓ una richiesta gRPC al server e riceverΓ una risposta con i dati di temperatura.
Ogni volta che arresti l'applicazione, in particolare il client, vedrai disegnato un grafico che mostrerΓ la variazione della temperatura.
gRPC puΓ² essere considerato un successore di RPC, ed Γ¨ leggero in termini di peso. Google lo ha sviluppato per la comunicazione tra microservizi e altri sistemi che necessitano di interagire. Ci sono diversi vantaggi nell'usare gRPC.
Oltre ai vantaggi chiave menzionati sopra, gRPC promuove un design migliore per la tua applicazione. gRPC Γ¨ orientato all'API, a differenza di REST che Γ¨ orientato alle risorse. Γ anche asincrono per impostazione predefinita, il che significa che non blocca il thread su richiesta e puΓ² servire milioni di richieste in parallelo, garantendo un'alta scalabilitΓ .
gRPC Γ¨ circa sette volte piΓΉ veloce di REST nel ricevere dati e circa dieci volte piΓΉ veloce di REST nel trasmettere dati per un payload specifico. Questo Γ¨ principalmente dovuto al pacchettamento compatto dei Protocol Buffers e all'uso di HTTP/2 da parte di gRPC.
AndreaCavallo
Before running the project, you need to obtain an API key to use the WeatherAPI service. This service is completely free, and you can get your API key by following these steps:
To keep your API key secure and out of the source code, configure it as an environment variable. Follow these steps:
export WEATHER_API_KEY=your-api-key
$env:WEATHER_API_KEY="your-api-key"
Ensure the WEATHER_API_KEY
environment variable is set correctly before proceeding.
Ensure you have Docker installed and access to the docker-compose.yml
file:
docker-compose.yml
.If you wish to send alerts via Twilio, follow these steps to create a Twilio account and set up the environment variables:
Consult: Twilio WhatsApp Quickstart (Go)
Set the following environment variables:
export TWILIO_ACCOUNT_SID=your-account-sid
export TWILIO_AUTH_TOKEN=your-auth-token
export TWILIO_PHONE_NUMBER=your-phone-number
export ALERT_PHONE_NUMBER=alert-phone-number
Once the API key is configured, you can start the server:
Navigate to the temperature_grpc_server
directory:
cd temperature_grpc_server/cmd/server
Start the server:
go run main.go
The server will start listening for gRPC requests.
With the server running, you can now start the client to send requests to the server:
Open a new terminal and navigate to the temperature_grpc_client
directory:
cd temperature_grpc_client/cmd/client
Start the client:
go run main.go
The client will send a gRPC request to the server and receive a response with the temperature data.
Whenever you shut down the application, especially the client, you will see a graph showing the temperature variation.
gRPC can be considered a successor to RPC and is lightweight. Google developed it for communication between microservices and other systems that need to interact. There are several advantages to using gRPC.
In addition to the key advantages mentioned above, gRPC promotes better design for your application. gRPC is API-oriented, unlike REST, which is resource-oriented. It is also asynchronous by default, meaning it does not block the thread on request and can serve millions of requests in parallel, ensuring high scalability.
gRPC is about seven times faster than REST in receiving data and about ten times faster than REST in transmitting data for a specific payload. This is primarily due to the compact packing of Protocol Buffers and the use of HTTP/2 by gRPC.
Andrea Cavallo