svilborg/laravel-health

Laravel 健康检查

安装: 7

依赖: 0

建议者: 0

安全: 0

星标: 3

关注者: 2

分支: 0

开放问题: 13

类型:项目


README

Build Status Coverage Quality Score Latest Stable Version License

MicroProfile Health 在 Laravel 中的实现

MicroProfile Health 协议

配置

在 config/health.php 中注册健康检查类

    return [
        /*
         * |--------------------------------------------------------------------------
         * | Health Checks
         * |--------------------------------------------------------------------------
         * |
         */

        'checks' => [
             [
                 'class' => \Health\Checks\NullCheck::class,
                 'params' => []
             ],
             [
                 'class' => \Health\Checks\Servers\Database::class,
                 'params' => []
             ],
             [
                 'class' => \Health\Checks\Filesystem\DiskSpace::class,
                 'params' => [
                    'path' => '/'
                 ]
             ],
             [
                'class' => \Health\Checks\Env\Environment::class,
                'params' => [
                     'APP_ENV' => 'testing'
                 ]
             ]
        ]
    ];

添加 API 路由

    Route::get('/health', 'Health\Controllers\HealthController@check');

示例响应负载

{
    "status": "UP",
    "checks": [
        {
            "name": "health-checks-null-check",
            "status": "UP",
            "data": []
        },
        {
            "name": "health-checks-filesystem-disk-space",
            "status": "UP",
            "data": {
                "free_bytes": 119100669952,
                "free_human": "110.92 GB",
                "path": "/",
                "threshold": 100000000
            }
        },
        {
            "name": "health-checks-env-environment",
            "status": "UP",
            "data": {
                "variable": "APP_ENV",
                "value": "testing",
                "value_expected": "testing"
            }
        },
        {
            "name": "health-checks-filesystem-directory-is-readable",
            "status": "UP",
            "data": {
                "paths": [
                    "../tests"
                ]
            }
        },
        {
            "name": "health-checks-filesystem-file-is-readable",
            "status": "UP",
            "data": {
                "files": [
                    "TestCase.php"
                ]
            }
        }
    ]
}

自定义健康检查

    use Health\Checks\BaseCheck;
    use Health\Checks\HealthCheckInterface;

    class ServiceACheck extends BaseCheck implements HealthCheckInterface
    {

        /**
         *
         * {@inheritdoc}
         * @see \Health\Checks\HealthCheckInterface::call()
         */
        public function call()
        {
            $health = $this->getBuilder('Service A');

            if(!$this->serviceA->connect()) {
                $health->withData('error', 'Service A Failed')
                        ->down();
            }
            else {
                $health->up();
            }

            return $health->build();
        }
    }

健康检查 CLI 命令

$ php artisan health

    ✔ UP Health State
    ==============================

    ✔ UP health-checks-null-check
    ✔ UP health-checks-filesystem-disk-space {"free_bytes":119302803456,"free_human":"111.11 TB","path":"\/tmp","threshold":100000000}
    ✔ UP health-checks-env-environment {"variable":"APP_ENV","value":"testing","value_expected":"testing"}
    ✔ UP health-checks-filesystem-directory-is-readable {"paths":[".\/tests"]}
    ✔ UP health-checks-filesystem-file-is-readable {"files":[".\/tests\/TestCase.php"]}