Skip to content

Go生产环境最佳实践 - Golang企业级应用部署指南

生产环境中的Go应用面临着复杂的挑战,包括高并发、高可用性、安全性等要求。掌握生产环境最佳实践对于构建稳定可靠的企业级应用至关重要。

📋 重点面试题

面试题 1:Go应用生产环境部署和运维的最佳实践

难度级别:⭐⭐⭐⭐⭐
考察范围:生产运维/系统架构
技术标签production deployment system architecture reliability performance

详细解答

1. 生产环境架构设计

点击查看完整代码实现
点击查看完整代码实现
go
package main

import (
    "context"
    "fmt"
    "log"
    "net/http"
    "os"
    "os/signal"
    "runtime"
    "sync"
    "sync/atomic"
    "syscall"
    "time"
)

func demonstrateProductionPractices() {
    fmt.Println("=== Go生产环境最佳实践 ===")
    
    /*
    生产环境最佳实践体系:
    
    1. 应用架构设计:
       - 微服务架构模式
       - 容错和恢复机制
       - 负载均衡策略
       - 数据一致性保证
    
    2. 部署和配置:
       - 容器化部署
       - 配置管理
       - 环境隔离
       - 版本控制
    
    3. 监控和日志:
       - 健康检查机制
       - 指标收集
       - 日志聚合
       - 告警系统
    
    4. 安全和合规:
       - 认证授权
       - 数据加密
       - 审计日志
       - 安全扫描
    */
    
    demonstrateGracefulShutdown()
    demonstrateHealthChecks()
    demonstrateConfigurationManagement()
    demonstrateCircuitBreaker()
}

func demonstrateGracefulShutdown() {
    fmt.Println("\n--- 优雅关闭机制 ---")
    
    /*
    优雅关闭要点:
    
    1. 信号处理:监听SIGTERM/SIGINT信号
    2. 连接排空:等待现有请求完成
    3. 资源清理:关闭数据库连接、文件等
    4. 超时控制:避免无限等待
    */
    
    // 优雅关闭管理器
    type GracefulShutdown struct {
        server      *http.Server
        shutdownCh  chan os.Signal
        workers     []Worker
        maxWaitTime time.Duration
        logger      *log.Logger
    }
    
    type Worker interface {
        Start(ctx context.Context) error
        Stop(ctx context.Context) error
        Name() string
    }
    
    // 示例worker:后台任务处理器
    type BackgroundWorker struct {
        name     string
        stopCh   chan struct{}
        doneCh   chan struct{}
        taskCh   chan Task
        running  int32
    }
    
    type Task struct {
        ID   string
        Data interface{}
    }
    
    func NewBackgroundWorker(name string) *BackgroundWorker {
        return &BackgroundWorker{
            name:   name,
            stopCh: make(chan struct{}),
            doneCh: make(chan struct{}),
            taskCh: make(chan Task, 100),
        }
    }
    
    func (bw *BackgroundWorker) Name() string {
        return bw.name
    }
    
    func (bw *BackgroundWorker) Start(ctx context.Context) error {
        if !atomic.CompareAndSwapInt32(&bw.running, 0, 1) {
            return fmt.Errorf("worker %s already running", bw.name)
        }
        
        go bw.run()
        return nil
    }
    
    func (bw *BackgroundWorker) Stop(ctx context.Context) error {
        if !atomic.CompareAndSwapInt32(&bw.running, 1, 0) {
            return nil // 已经停止
        }
        
        close(bw.stopCh)
        
        select {
        case <-bw.doneCh:
            return nil
        case <-ctx.Done():
            return fmt.Errorf("worker %s shutdown timeout", bw.name)
        }
    }
    
    func (bw *BackgroundWorker) run() {
        defer close(bw.doneCh)
        
        for {
            select {
            case task := <-bw.taskCh:
                bw.processTask(task)
            case <-bw.stopCh:
                // 处理剩余任务
                bw.drainTasks()
                return
            }
        }
    }
    
    func (bw *BackgroundWorker) processTask(task Task) {
        // 模拟任务处理
        time.Sleep(50 * time.Millisecond)
        fmt.Printf("    Worker %s processed task %s\n", bw.name, task.ID)
    }
    
    func (bw *BackgroundWorker) drainTasks() {
        for {
            select {
            case task := <-bw.taskCh:
                bw.processTask(task)
            default:
                return
            }
        }
    }
    
    func (bw *BackgroundWorker) AddTask(task Task) {
        select {
        case bw.taskCh <- task:
        default:
            fmt.Printf("    Task queue full for worker %s\n", bw.name)
        }
    }
    
    func NewGracefulShutdown(maxWaitTime time.Duration) *GracefulShutdown {
        return &GracefulShutdown{
            shutdownCh:  make(chan os.Signal, 1),
            maxWaitTime: maxWaitTime,
            logger:      log.New(os.Stdout, "[SHUTDOWN] ", log.LstdFlags),
        }
    }
    
    func (gs *GracefulShutdown) AddWorker(worker Worker) {
        gs.workers = append(gs.workers, worker)
    }
    
    func (gs *GracefulShutdown) SetServer(server *http.Server) {
        gs.server = server
    }
    
    func (gs *GracefulShutdown) Start(ctx context.Context) error {
        // 监听关闭信号
        signal.Notify(gs.shutdownCh, syscall.SIGTERM, syscall.SIGINT)
        
        // 启动所有worker
        for _, worker := range gs.workers {
            if err := worker.Start(ctx); err != nil {
                gs.logger.Printf("Failed to start worker %s: %v", worker.Name(), err)
                return err
            }
            gs.logger.Printf("Worker %s started", worker.Name())
        }
        
        // 启动HTTP服务器
        if gs.server != nil {
            go func() {
                gs.logger.Printf("HTTP server starting on %s", gs.server.Addr)
                if err := gs.server.ListenAndServe(); err != http.ErrServerClosed {
                    gs.logger.Printf("HTTP server error: %v", err)
                }
            }()
        }
        
        // 等待关闭信号
        <-gs.shutdownCh
        gs.logger.Println("Shutdown signal received, starting graceful shutdown...")
        
        return gs.shutdown()
    }
    
    func (gs *GracefulShutdown) shutdown() error {
        shutdownCtx, cancel := context.WithTimeout(context.Background(), gs.maxWaitTime)
        defer cancel()
        
        var wg sync.WaitGroup
        errorCh := make(chan error, len(gs.workers)+1)
        
        // 关闭HTTP服务器
        if gs.server != nil {
            wg.Add(1)
            go func() {
                defer wg.Done()
                gs.logger.Println("Shutting down HTTP server...")
                if err := gs.server.Shutdown(shutdownCtx); err != nil {
                    errorCh <- fmt.Errorf("HTTP server shutdown error: %v", err)
                } else {
                    gs.logger.Println("HTTP server shutdown complete")
                }
            }()
        }
        
        // 关闭所有worker
        for _, worker := range gs.workers {
            wg.Add(1)
            go func(w Worker) {
                defer wg.Done()
                gs.logger.Printf("Shutting down worker %s...", w.Name())
                if err := w.Stop(shutdownCtx); err != nil {
                    errorCh <- fmt.Errorf("worker %s shutdown error: %v", w.Name(), err)
                } else {
                    gs.logger.Printf("Worker %s shutdown complete", w.Name())
                }
            }(worker)
        }
        
        // 等待所有组件关闭
        done := make(chan struct{})
        go func() {
            wg.Wait()
            close(done)
        }()
        
        select {
        case <-done:
            gs.logger.Println("Graceful shutdown completed successfully")
        case <-shutdownCtx.Done():
            gs.logger.Println("Shutdown timeout reached, forcing exit")
            return fmt.Errorf("shutdown timeout")
        }
        
        // 收集错误
        close(errorCh)
        var errors []error
        for err := range errorCh {
            errors = append(errors, err)
        }
        
        if len(errors) > 0 {
            gs.logger.Printf("Shutdown completed with %d errors", len(errors))
            return fmt.Errorf("shutdown errors: %v", errors)
        }
        
        return nil
    }
    
    // 演示优雅关闭
    fmt.Printf("优雅关闭机制演示:\n")
    
    // 创建HTTP处理器
    mux := http.NewServeMux()
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        time.Sleep(100 * time.Millisecond) // 模拟处理时间
        fmt.Fprintf(w, "Request processed at %s", time.Now().Format(time.RFC3339))
    })
    
    server := &http.Server{
        Addr:    ":8080",
        Handler: mux,
    }
    
    // 创建优雅关闭管理器
    shutdownManager := NewGracefulShutdown(30 * time.Second)
    shutdownManager.SetServer(server)
    
    // 添加后台worker
    worker1 := NewBackgroundWorker("task-processor-1")
    worker2 := NewBackgroundWorker("task-processor-2")
    shutdownManager.AddWorker(worker1)
    shutdownManager.AddWorker(worker2)
    
    // 模拟添加一些任务
    go func() {
        for i := 0; i < 10; i++ {
            task := Task{
                ID:   fmt.Sprintf("task-%d", i),
                Data: fmt.Sprintf("data-%d", i),
            }
            worker1.AddTask(task)
            time.Sleep(200 * time.Millisecond)
        }
    }()
    
    fmt.Printf("  启动应用程序(按Ctrl+C关闭)...\n")
    fmt.Printf("  HTTP服务器: http://localhost:8080\n")
    
    // 模拟运行一段时间后关闭
    go func() {
        time.Sleep(3 * time.Second)
        fmt.Printf("  发送关闭信号...\n")
        shutdownManager.shutdownCh <- syscall.SIGTERM
    }()
    
    ctx := context.Background()
    if err := shutdownManager.Start(ctx); err != nil {
        fmt.Printf("  ❌ 关闭失败: %v\n", err)
    } else {
        fmt.Printf("  ✅ 应用程序已优雅关闭\n")
    }
}

func demonstrateHealthChecks() {
    fmt.Println("\n--- 健康检查机制 ---")
    
    /*
    健康检查要点:
    
    1. 多层次检查:浅层检查和深层检查
    2. 依赖检查:数据库、外部服务等
    3. 性能检查:响应时间、资源使用
    4. 可配置性:检查项和阈值可配置
    */
    
    // 健康检查系统
    type HealthChecker struct {
        checks   map[string]HealthCheck
        timeout  time.Duration
        cache    map[string]CachedResult
        cacheTTL time.Duration
        mutex    sync.RWMutex
    }
    
    type HealthCheck interface {
        Name() string
        Check(ctx context.Context) HealthResult
        IsCritical() bool
    }
    
    type HealthResult struct {
        Status    HealthStatus `json:"status"`
        Message   string       `json:"message"`
        Latency   time.Duration `json:"latency"`
        Timestamp time.Time    `json:"timestamp"`
        Details   map[string]interface{} `json:"details,omitempty"`
    }
    
    type HealthStatus string
    
    const (
        HealthStatusHealthy   HealthStatus = "healthy"
        HealthStatusUnhealthy HealthStatus = "unhealthy"
        HealthStatusDegraded  HealthStatus = "degraded"
        HealthStatusUnknown   HealthStatus = "unknown"
    )
    
    type CachedResult struct {
        Result    HealthResult
        ExpiresAt time.Time
    }
    
    // 基础健康检查实现
    type BasicHealthCheck struct {
        name     string
        critical bool
        checkFn  func(ctx context.Context) HealthResult
    }
    
    func (bhc *BasicHealthCheck) Name() string {
        return bhc.name
    }
    
    func (bhc *BasicHealthCheck) Check(ctx context.Context) HealthResult {
        return bhc.checkFn(ctx)
    }
    
    func (bhc *BasicHealthCheck) IsCritical() bool {
        return bhc.critical
    }
    
    // 数据库健康检查
    type DatabaseHealthCheck struct {
        name         string
        critical     bool
        timeout      time.Duration
        maxLatency   time.Duration
    }
    
    func (dhc *DatabaseHealthCheck) Name() string {
        return dhc.name
    }
    
    func (dhc *DatabaseHealthCheck) IsCritical() bool {
        return dhc.critical
    }
    
    func (dhc *DatabaseHealthCheck) Check(ctx context.Context) HealthResult {
        start := time.Now()
        
        // 模拟数据库ping
        checkCtx, cancel := context.WithTimeout(ctx, dhc.timeout)
        defer cancel()
        
        select {
        case <-time.After(50 * time.Millisecond): // 模拟数据库响应
            latency := time.Since(start)
            
            if latency > dhc.maxLatency {
                return HealthResult{
                    Status:    HealthStatusDegraded,
                    Message:   fmt.Sprintf("数据库响应缓慢: %v", latency),
                    Latency:   latency,
                    Timestamp: time.Now(),
                    Details: map[string]interface{}{
                        "max_allowed_latency": dhc.maxLatency,
                    },
                }
            }
            
            return HealthResult{
                Status:    HealthStatusHealthy,
                Message:   "数据库连接正常",
                Latency:   latency,
                Timestamp: time.Now(),
                Details: map[string]interface{}{
                    "connection_pool_size": 10,
                    "active_connections":   5,
                },
            }
            
        case <-checkCtx.Done():
            return HealthResult{
                Status:    HealthStatusUnhealthy,
                Message:   "数据库连接超时",
                Latency:   time.Since(start),
                Timestamp: time.Now(),
            }
        }
    }
    
    func NewHealthChecker(timeout, cacheTTL time.Duration) *HealthChecker {
        return &HealthChecker{
            checks:   make(map[string]HealthCheck),
            timeout:  timeout,
            cache:    make(map[string]CachedResult),
            cacheTTL: cacheTTL,
        }
    }
    
    func (hc *HealthChecker) AddCheck(check HealthCheck) {
        hc.mutex.Lock()
        defer hc.mutex.Unlock()
        hc.checks[check.Name()] = check
    }
    
    func (hc *HealthChecker) CheckHealth(ctx context.Context) map[string]HealthResult {
        hc.mutex.RLock()
        checks := make(map[string]HealthCheck)
        for name, check := range hc.checks {
            checks[name] = check
        }
        hc.mutex.RUnlock()
        
        results := make(map[string]HealthResult)
        var wg sync.WaitGroup
        var mutex sync.Mutex
        
        for name, check := range checks {
            // 检查缓存
            if cached, valid := hc.getCachedResult(name); valid {
                results[name] = cached
                continue
            }
            
            wg.Add(1)
            go func(name string, check HealthCheck) {
                defer wg.Done()
                
                checkCtx, cancel := context.WithTimeout(ctx, hc.timeout)
                defer cancel()
                
                result := check.Check(checkCtx)
                
                mutex.Lock()
                results[name] = result
                mutex.Unlock()
                
                // 缓存结果
                hc.cacheResult(name, result)
            }(name, check)
        }
        
        wg.Wait()
        return results
    }
    
    func (hc *HealthChecker) getCachedResult(name string) (HealthResult, bool) {
        hc.mutex.RLock()
        defer hc.mutex.RUnlock()
        
        cached, exists := hc.cache[name]
        if !exists || time.Now().After(cached.ExpiresAt) {
            return HealthResult{}, false
        }
        
        return cached.Result, true
    }
    
    func (hc *HealthChecker) cacheResult(name string, result HealthResult) {
        hc.mutex.Lock()
        defer hc.mutex.Unlock()
        
        hc.cache[name] = CachedResult{
            Result:    result,
            ExpiresAt: time.Now().Add(hc.cacheTTL),
        }
    }
    
    func (hc *HealthChecker) GetOverallStatus(results map[string]HealthResult) HealthStatus {
        if len(results) == 0 {
            return HealthStatusUnknown
        }
        
        hasUnhealthy := false
        hasDegraded := false
        
        for _, result := range results {
            switch result.Status {
            case HealthStatusUnhealthy:
                hasUnhealthy = true
            case HealthStatusDegraded:
                hasDegraded = true
            }
        }
        
        if hasUnhealthy {
            return HealthStatusUnhealthy
        }
        if hasDegraded {
            return HealthStatusDegraded
        }
        
        return HealthStatusHealthy
    }
    
    // 演示健康检查
    fmt.Printf("健康检查机制演示:\n")
    
    checker := NewHealthChecker(5*time.Second, 30*time.Second)
    
    // 添加各种健康检查
    checker.AddCheck(&DatabaseHealthCheck{
        name:       "database",
        critical:   true,
        timeout:    2 * time.Second,
        maxLatency: 100 * time.Millisecond,
    })
    
    checker.AddCheck(&BasicHealthCheck{
        name:     "memory",
        critical: true,
        checkFn: func(ctx context.Context) HealthResult {
            var m runtime.MemStats
            runtime.ReadMemStats(&m)
            
            memUsageMB := float64(m.HeapInuse) / 1024 / 1024
            maxMemoryMB := 100.0 // 100MB限制
            
            if memUsageMB > maxMemoryMB {
                return HealthResult{
                    Status:    HealthStatusUnhealthy,
                    Message:   fmt.Sprintf("内存使用过高: %.2fMB", memUsageMB),
                    Timestamp: time.Now(),
                }
            }
            
            return HealthResult{
                Status:    HealthStatusHealthy,
                Message:   fmt.Sprintf("内存使用正常: %.2fMB", memUsageMB),
                Timestamp: time.Now(),
                Details: map[string]interface{}{
                    "heap_inuse_mb": memUsageMB,
                    "heap_objects":  m.HeapObjects,
                },
            }
        },
    })
    
    checker.AddCheck(&BasicHealthCheck{
        name:     "external_api",
        critical: false,
        checkFn: func(ctx context.Context) HealthResult {
            // 模拟外部API检查
            if time.Now().Unix()%5 < 4 { // 80%健康
                return HealthResult{
                    Status:    HealthStatusHealthy,
                    Message:   "外部API响应正常",
                    Timestamp: time.Now(),
                }
            }
            
            return HealthResult{
                Status:    HealthStatusDegraded,
                Message:   "外部API响应缓慢",
                Timestamp: time.Now(),
            }
        },
    })
    
    // 执行健康检查
    ctx := context.Background()
    results := checker.CheckHealth(ctx)
    overallStatus := checker.GetOverallStatus(results)
    
    fmt.Printf("  📊 健康检查结果:\n")
    fmt.Printf("    整体状态: %s\n", overallStatus)
    
    for name, result := range results {
        statusIcon := "✅"
        if result.Status == HealthStatusUnhealthy {
            statusIcon = "❌"
        } else if result.Status == HealthStatusDegraded {
            statusIcon = "⚠️"
        }
        
        fmt.Printf("    %s %s: %s (%v)\n", 
            statusIcon, name, result.Message, result.Latency)
        
        if result.Details != nil {
            for key, value := range result.Details {
                fmt.Printf("      %s: %v\n", key, value)
            }
        }
    }
    
    // 测试缓存
    fmt.Printf("\n  🔄 测试健康检查缓存:\n")
    start := time.Now()
    results2 := checker.CheckHealth(ctx)
    duration := time.Since(start)
    
    fmt.Printf("    缓存检查耗时: %v\n", duration)
    fmt.Printf("    结果一致性: %t\n", len(results) == len(results2))
}

func demonstrateConfigurationManagement() {
    fmt.Println("\n--- 配置管理 ---")
    
    /*
    配置管理要点:
    
    1. 多源配置:文件、环境变量、命令行参数
    2. 配置验证:格式验证和业务规则验证
    3. 热重载:运行时配置更新
    4. 安全管理:敏感信息加密存储
    */
    
    // 配置管理系统
    type ConfigManager struct {
        config      map[string]interface{}
        validators  map[string]Validator
        watchers    []ConfigWatcher
        mutex       sync.RWMutex
        reloadCh    chan struct{}
    }
    
    type Validator interface {
        Validate(key string, value interface{}) error
    }
    
    type ConfigWatcher interface {
        OnConfigChange(key string, oldValue, newValue interface{})
    }
    
    // 范围验证器
    type RangeValidator struct {
        Min, Max interface{}
    }
    
    func (rv *RangeValidator) Validate(key string, value interface{}) error {
        switch v := value.(type) {
        case int:
            min, ok1 := rv.Min.(int)
            max, ok2 := rv.Max.(int)
            if ok1 && ok2 {
                if v < min || v > max {
                    return fmt.Errorf("配置 %s%d 超出范围 [%d, %d]", key, v, min, max)
                }
            }
        case float64:
            min, ok1 := rv.Min.(float64)
            max, ok2 := rv.Max.(float64)
            if ok1 && ok2 {
                if v < min || v > max {
                    return fmt.Errorf("配置 %s%.2f 超出范围 [%.2f, %.2f]", key, v, min, max)
                }
            }
        }
        return nil
    }
    
    // 枚举验证器
    type EnumValidator struct {
        AllowedValues []interface{}
    }
    
    func (ev *EnumValidator) Validate(key string, value interface{}) error {
        for _, allowed := range ev.AllowedValues {
            if value == allowed {
                return nil
            }
        }
        return fmt.Errorf("配置 %s%v 不在允许列表中: %v", key, value, ev.AllowedValues)
    }
    
    // 配置观察者
    type LoggingWatcher struct{}
    
    func (lw *LoggingWatcher) OnConfigChange(key string, oldValue, newValue interface{}) {
        fmt.Printf("    配置变更: %s = %v -> %v\n", key, oldValue, newValue)
    }
    
    func NewConfigManager() *ConfigManager {
        return &ConfigManager{
            config:     make(map[string]interface{}),
            validators: make(map[string]Validator),
            reloadCh:   make(chan struct{}, 1),
        }
    }
    
    func (cm *ConfigManager) AddValidator(key string, validator Validator) {
        cm.mutex.Lock()
        defer cm.mutex.Unlock()
        cm.validators[key] = validator
    }
    
    func (cm *ConfigManager) AddWatcher(watcher ConfigWatcher) {
        cm.mutex.Lock()
        defer cm.mutex.Unlock()
        cm.watchers = append(cm.watchers, watcher)
    }
    
    func (cm *ConfigManager) Set(key string, value interface{}) error {
        cm.mutex.Lock()
        defer cm.mutex.Unlock()
        
        // 验证配置
        if validator, exists := cm.validators[key]; exists {
            if err := validator.Validate(key, value); err != nil {
                return err
            }
        }
        
        oldValue := cm.config[key]
        cm.config[key] = value
        
        // 通知观察者
        for _, watcher := range cm.watchers {
            watcher.OnConfigChange(key, oldValue, value)
        }
        
        return nil
    }
    
    func (cm *ConfigManager) Get(key string) (interface{}, bool) {
        cm.mutex.RLock()
        defer cm.mutex.RUnlock()
        value, exists := cm.config[key]
        return value, exists
    }
    
    func (cm *ConfigManager) GetString(key string, defaultValue string) string {
        if value, exists := cm.Get(key); exists {
            if str, ok := value.(string); ok {
                return str
            }
        }
        return defaultValue
    }
    
    func (cm *ConfigManager) GetInt(key string, defaultValue int) int {
        if value, exists := cm.Get(key); exists {
            if num, ok := value.(int); ok {
                return num
            }
        }
        return defaultValue
    }
    
    func (cm *ConfigManager) GetBool(key string, defaultValue bool) bool {
        if value, exists := cm.Get(key); exists {
            if b, ok := value.(bool); ok {
                return b
            }
        }
        return defaultValue
    }
    
    func (cm *ConfigManager) LoadFromEnv() {
        // 模拟从环境变量加载配置
        envConfigs := map[string]interface{}{
            "app.name":        os.Getenv("APP_NAME"),
            "app.port":        8080,
            "db.host":         "localhost",
            "db.port":         5432,
            "db.max_connections": 100,
            "log.level":       "info",
            "debug.enabled":   false,
        }
        
        for key, value := range envConfigs {
            if value != "" {
                cm.Set(key, value)
            }
        }
    }
    
    func (cm *ConfigManager) Reload() error {
        cm.mutex.Lock()
        defer cm.mutex.Unlock()
        
        fmt.Printf("    🔄 重新加载配置...\n")
        
        // 模拟从配置源重新加载
        newConfigs := map[string]interface{}{
            "db.max_connections": 150, // 模拟配置变更
            "log.level":          "debug",
        }
        
        for key, value := range newConfigs {
            if validator, exists := cm.validators[key]; exists {
                if err := validator.Validate(key, value); err != nil {
                    return fmt.Errorf("配置验证失败: %v", err)
                }
            }
            
            oldValue := cm.config[key]
            cm.config[key] = value
            
            // 通知观察者
            for _, watcher := range cm.watchers {
                watcher.OnConfigChange(key, oldValue, value)
            }
        }
        
        return nil
    }
    
    func (cm *ConfigManager) StartWatching(ctx context.Context) {
        go func() {
            ticker := time.NewTicker(5 * time.Second)
            defer ticker.Stop()
            
            for {
                select {
                case <-ctx.Done():
                    return
                case <-ticker.C:
                    // 模拟配置文件变更检查
                    select {
                    case cm.reloadCh <- struct{}{}:
                    default:
                    }
                case <-cm.reloadCh:
                    if err := cm.Reload(); err != nil {
                        fmt.Printf("    ❌ 配置重载失败: %v\n", err)
                    }
                }
            }
        }()
    }
    
    // 演示配置管理
    fmt.Printf("配置管理演示:\n")
    
    configManager := NewConfigManager()
    
    // 添加验证器
    configManager.AddValidator("db.port", &RangeValidator{Min: 1, Max: 65535})
    configManager.AddValidator("db.max_connections", &RangeValidator{Min: 1, Max: 1000})
    configManager.AddValidator("log.level", &EnumValidator{
        AllowedValues: []interface{}{"debug", "info", "warn", "error"},
    })
    
    // 添加观察者
    configManager.AddWatcher(&LoggingWatcher{})
    
    // 加载初始配置
    fmt.Printf("  📝 加载初始配置:\n")
    configManager.LoadFromEnv()
    
    // 显示当前配置
    fmt.Printf("\n  📋 当前配置:\n")
    configs := []string{"app.name", "db.host", "db.port", "db.max_connections", "log.level", "debug.enabled"}
    for _, key := range configs {
        if value, exists := configManager.Get(key); exists {
            fmt.Printf("    %s = %v\n", key, value)
        }
    }
    
    // 测试配置验证
    fmt.Printf("\n  🔍 测试配置验证:\n")
    
    // 有效配置
    if err := configManager.Set("db.port", 3306); err != nil {
        fmt.Printf("    ❌ %v\n", err)
    } else {
        fmt.Printf("    ✅ 数据库端口设置成功\n")
    }
    
    // 无效配置
    if err := configManager.Set("db.port", 99999); err != nil {
        fmt.Printf("    ✅ 验证成功拒绝无效端口: %v\n", err)
    }
    
    if err := configManager.Set("log.level", "invalid"); err != nil {
        fmt.Printf("    ✅ 验证成功拒绝无效日志级别: %v\n", err)
    }
    
    // 启动配置监控
    fmt.Printf("\n  👀 启动配置监控:\n")
    ctx, cancel := context.WithTimeout(context.Background(), 6*time.Second)
    defer cancel()
    
    configManager.StartWatching(ctx)
    
    // 触发配置重载
    time.Sleep(1 * time.Second)
    configManager.reloadCh <- struct{}{}
    
    time.Sleep(1 * time.Second)
}

func demonstrateCircuitBreaker() {
    fmt.Println("\n--- 熔断器模式 ---")
    
    /*
    熔断器要点:
    
    1. 三种状态:闭合、开放、半开
    2. 失败阈值:连续失败次数或失败率
    3. 超时恢复:开放状态的恢复时间
    4. 健康检查:半开状态的探测机制
    */
    
    // 熔断器状态
    type CircuitBreakerState int
    
    const (
        StateClosed CircuitBreakerState = iota
        StateOpen
        StateHalfOpen
    )
    
    func (s CircuitBreakerState) String() string {
        switch s {
        case StateClosed:
            return "CLOSED"
        case StateOpen:
            return "OPEN"
        case StateHalfOpen:
            return "HALF_OPEN"
        default:
            return "UNKNOWN"
        }
    }
    
    // 熔断器配置
    type CircuitBreakerConfig struct {
        FailureThreshold int           // 失败阈值
        RecoveryTimeout  time.Duration // 恢复超时
        MaxRequests      int           // 半开状态最大请求数
        Timeout          time.Duration // 请求超时
    }
    
    // 熔断器实现
    type CircuitBreaker struct {
        config       CircuitBreakerConfig
        state        CircuitBreakerState
        failures     int
        requests     int
        lastFailTime time.Time
        mutex        sync.RWMutex
    }
    
    func NewCircuitBreaker(config CircuitBreakerConfig) *CircuitBreaker {
        return &CircuitBreaker{
            config: config,
            state:  StateClosed,
        }
    }
    
    func (cb *CircuitBreaker) Call(fn func() error) error {
        cb.mutex.Lock()
        defer cb.mutex.Unlock()
        
        // 检查是否可以执行请求
        if !cb.canExecute() {
            return fmt.Errorf("circuit breaker is open")
        }
        
        // 执行请求
        cb.beforeRequest()
        
        // 创建超时上下文
        ctx, cancel := context.WithTimeout(context.Background(), cb.config.Timeout)
        defer cancel()
        
        // 在goroutine中执行请求
        resultCh := make(chan error, 1)
        go func() {
            resultCh <- fn()
        }()
        
        var err error
        select {
        case err = <-resultCh:
            // 正常完成
        case <-ctx.Done():
            err = fmt.Errorf("request timeout")
        }
        
        // 处理结果
        cb.afterRequest(err)
        
        return err
    }
    
    func (cb *CircuitBreaker) canExecute() bool {
        switch cb.state {
        case StateClosed:
            return true
        case StateOpen:
            if time.Since(cb.lastFailTime) >= cb.config.RecoveryTimeout {
                cb.state = StateHalfOpen
                cb.requests = 0
                return true
            }
            return false
        case StateHalfOpen:
            return cb.requests < cb.config.MaxRequests
        default:
            return false
        }
    }
    
    func (cb *CircuitBreaker) beforeRequest() {
        cb.requests++
    }
    
    func (cb *CircuitBreaker) afterRequest(err error) {
        if err != nil {
            cb.onFailure()
        } else {
            cb.onSuccess()
        }
    }
    
    func (cb *CircuitBreaker) onSuccess() {
        switch cb.state {
        case StateClosed:
            cb.failures = 0
        case StateHalfOpen:
            cb.state = StateClosed
            cb.failures = 0
            cb.requests = 0
        }
    }
    
    func (cb *CircuitBreaker) onFailure() {
        cb.failures++
        cb.lastFailTime = time.Now()
        
        switch cb.state {
        case StateClosed:
            if cb.failures >= cb.config.FailureThreshold {
                cb.state = StateOpen
            }
        case StateHalfOpen:
            cb.state = StateOpen
            cb.requests = 0
        }
    }
    
    func (cb *CircuitBreaker) GetState() CircuitBreakerState {
        cb.mutex.RLock()
        defer cb.mutex.RUnlock()
        return cb.state
    }
    
    func (cb *CircuitBreaker) GetStats() map[string]interface{} {
        cb.mutex.RLock()
        defer cb.mutex.RUnlock()
        
        return map[string]interface{}{
            "state":            cb.state.String(),
            "failures":         cb.failures,
            "requests":         cb.requests,
            "last_fail_time":   cb.lastFailTime,
        }
    }
    
    // 演示熔断器
    fmt.Printf("熔断器模式演示:\n")
    
    config := CircuitBreakerConfig{
        FailureThreshold: 3,
        RecoveryTimeout:  2 * time.Second,
        MaxRequests:      2,
        Timeout:          1 * time.Second,
    }
    
    breaker := NewCircuitBreaker(config)
    
    // 模拟外部服务
    var serviceCallCount int32
    mockService := func() error {
        count := atomic.AddInt32(&serviceCallCount, 1)
        
        // 前5次调用失败,后续成功
        if count <= 5 {
            time.Sleep(100 * time.Millisecond)
            return fmt.Errorf("service unavailable (call %d)", count)
        }
        
        time.Sleep(50 * time.Millisecond)
        return nil
    }
    
    // 测试熔断器行为
    fmt.Printf("  🔄 测试熔断器行为:\n")
    
    for i := 1; i <= 10; i++ {
        fmt.Printf("    请求 %d: ", i)
        
        err := breaker.Call(mockService)
        stats := breaker.GetStats()
        
        if err != nil {
            fmt.Printf("❌ %v (状态: %s, 失败: %v)\n", 
                err, stats["state"], stats["failures"])
        } else {
            fmt.Printf("✅ 成功 (状态: %s)\n", stats["state"])
        }
        
        time.Sleep(200 * time.Millisecond)
        
        // 在第6次请求后等待恢复
        if i == 6 {
            fmt.Printf("    等待熔断器恢复...\n")
            time.Sleep(2500 * time.Millisecond)
        }
    }
    
    fmt.Printf("\n  📊 熔断器最终状态:\n")
    finalStats := breaker.GetStats()
    for key, value := range finalStats {
        fmt.Printf("    %s: %v\n", key, value)
    }
    
    fmt.Printf("\n  📋 熔断器最佳实践:\n")
    fmt.Printf("    1. 合理设置失败阈值和恢复时间\n")
    fmt.Printf("    2. 监控熔断器状态和指标\n")
    fmt.Printf("    3. 提供降级服务或缓存\n")
    fmt.Printf("    4. 区分不同类型的错误\n")
    fmt.Printf("    5. 实现分布式熔断器状态同步\n")
}

func main() {
    demonstrateProductionPractices()
}

:::

🎯 核心知识点总结

优雅关闭要点

  1. 信号处理: 正确监听和处理系统关闭信号
  2. 资源清理: 确保所有资源得到妥善释放
  3. 超时控制: 避免关闭过程无限等待
  4. 组件协调: 协调多个组件的关闭顺序

健康检查要点

  1. 多层检查: 实现浅层和深层健康检查
  2. 依赖监控: 监控数据库、外部服务等依赖状态
  3. 性能监控: 检查响应时间和资源使用情况
  4. 结果缓存: 避免频繁执行昂贵的健康检查

配置管理要点

  1. 多源配置: 支持文件、环境变量等多种配置源
  2. 配置验证: 实现严格的配置格式和业务规则验证
  3. 热重载: 支持运行时配置更新
  4. 安全存储: 敏感配置信息的安全管理

熔断器要点

  1. 状态管理: 正确实现闭合、开放、半开三种状态
  2. 阈值设置: 合理设置失败阈值和恢复时间
  3. 监控告警: 实时监控熔断器状态和指标
  4. 降级策略: 提供服务降级和缓存机制

🔍 面试准备建议

  1. 架构理解: 深入理解微服务架构和分布式系统设计
  2. 运维经验: 积累生产环境部署和运维的实际经验
  3. 监控体系: 掌握完整的应用监控和告警体系建设
  4. 故障处理: 学会快速定位和解决生产环境问题
  5. 安全意识: 了解生产环境的安全要求和最佳实践

正在精进