21.7 实战案例:Go/Rust/数据库/微服务
21.7.1 Go 应用的最小化镜像构建
超小 Go Web 服务
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func healthHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"status":"healthy","version":"1.0.0"}`)
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
hostname, _ := os.Hostname()
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"message":"Hello from %s","version":"1.0.0"}`, hostname)
}
func main() {
http.HandleFunc("/health", healthHandler)
http.HandleFunc("/hello", helloHandler)
http.HandleFunc("/", helloHandler)
port := ":8080"
log.Printf("Server starting on %s", port)
if err := http.ListenAndServe(port, nil); err != nil {
log.Fatalf("Server failed: %v", err)
}
}带依赖的 Go 应用
21.7.2 Rust 应用的最小化镜像构建
21.7.3 数据库容器化最佳实践
PostgreSQL 生产部署
MySQL/MariaDB 部署
Redis 缓存部署
21.7.4 微服务架构的 Docker Compose 编排
21.7.5 使用 VS Code Dev Containers
最后更新于