导航菜单

云原生与容器化

云原生基础

  • 云原生概念。
  • 容器化技术。
  • 微服务架构。
// 云原生应用示例
<?php

// 1. 环境变量配置
// .env
APP_ENV=production
APP_DEBUG=false
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=app
DB_USERNAME=root
DB_PASSWORD=password

// 2. 健康检查
class HealthCheckController extends Controller
{
    public function check()
    {
        return response()->json([
            "status" => "healthy",
            "timestamp" => time(),
            "services" => [
                "database" => $this->checkDatabase(),
                "cache" => $this->checkCache(),
                "storage" => $this->checkStorage()
            ]
        ]);
    }

    private function checkDatabase()
    {
        try {
            DB::connection()->getPdo();
            return "healthy";
        } catch (\Exception $e) {
            return "unhealthy";
        }
    }

    private function checkCache()
    {
        try {
            Cache::put("health_check", "ok", 1);
            return Cache::get("health_check") === "ok" ? "healthy" : "unhealthy";
        } catch (\Exception $e) {
            return "unhealthy";
        }
    }

    private function checkStorage()
    {
        try {
            Storage::disk("local")->put("health_check.txt", "ok");
            return Storage::disk("local")->get("health_check.txt") === "ok" ? "healthy" : "unhealthy";
        } catch (\Exception $e) {
            return "unhealthy";
        }
    }
}

// 3. 配置管理
class ConfigManager
{
    private $config;

    public function __construct()
    {
        $this->config = [
            "app" => [
                "name" => env("APP_NAME", "Laravel"),
                "env" => env("APP_ENV", "production"),
                "debug" => env("APP_DEBUG", false),
            ],
            "database" => [
                "connection" => env("DB_CONNECTION", "mysql"),
                "host" => env("DB_HOST", "127.0.0.1"),
                "port" => env("DB_PORT", "3306"),
                "database" => env("DB_DATABASE", "forge"),
                "username" => env("DB_USERNAME", "forge"),
                "password" => env("DB_PASSWORD", ""),
            ],
        ];
    }

    public function get($key, $default = null)
    {
        return data_get($this->config, $key, $default);
    }
}
?>