Real-World Project - Building a Microservice
The Microservice That Survived Black Friday
// Health check endpoint
func (s *Server) healthHandler(w http.ResponseWriter, r *http.Request) {
if !s.isHealthy() {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
}
// Graceful shutdown
func (s *Server) Shutdown(ctx context.Context) error {
log.Info("Shutting down gracefully...")
return s.httpServer.Shutdown(ctx)
}
// Metrics endpoint
func (s *Server) metricsHandler(w http.ResponseWriter, r *http.Request) {
promhttp.Handler().ServeHTTP(w, r)
}Project Structure
Building the HTTP Server
Server Setup
Graceful Shutdown
Health Checks and Readiness Probes
Health Check
Readiness Check
Metrics with Prometheus
Setup Prometheus
Define Metrics
Expose Metrics
Structured Logging
Docker Containerization
Multi-Stage Dockerfile
Kubernetes Deployment
Deployment
Service
Deploy to Kubernetes
Complete Working Example
Makefile for Development
Your Challenge
Key Takeaways
What I Learned
Congratulations! 🎉
Last updated