browncat/healthcheck-bundle
健康检查工具
Requires
- php: ^7.2 || ^8.0
- psr/log: ^1|^2|^3
- symfony/config: ^4.4.3|^5.0|^6.0
- symfony/dependency-injection: ^4.4.3|^5.0|^6.0
- symfony/framework-bundle: ^4.4.3|^5.0|^6.0
Requires (Dev)
- dg/bypass-finals: ^1.3
- doctrine/coding-standard: ^9.0
- doctrine/doctrine-bundle: ^2.3
- phpunit/phpunit: ^8.0 || ^9.5
- psalm/plugin-phpunit: ^0.16.0
- psalm/plugin-symfony: ^2.3
- squizlabs/php_codesniffer: ^3.6
- symfony/dotenv: ^4.4.3|^5.0|^6.0
- vimeo/psalm: ^4.8
README
此扩展包可用于轻松编写健康检查并公开端点,这些端点可用于例如Kubernetes来确定应用程序的健康状态。
此扩展包由两个核心组件组成,相互交织,即检查器和检查。
- (健康)检查是一个包含检查系统是否正确运行的逻辑的组件。
- (健康)检查器在需要时运行这些检查。例如,当请求端点
/healthz
时。
使用方法
此软件包可以使用composer安装
composer require browncat/healthcheck-bundle
启用端点
此扩展包提供一组端点,可启用以公开以下端点
/health/liveness
如果一个或多个检查失败,则返回503。与LivenessChecker
配对/health/readiness
如果一个或多个检查失败,则返回503。与ReadinessChecker
配对/health/startup
如果一个或多个检查失败,则返回503。与StartupChecker
配对/healthz
所有检查的JSON结果,如果一个或多个检查失败,则返回503。
要启用所有这些,请将以下内容添加到您的 routes.yaml
health: resource: "@HealthCheckBundle/Resources/config/routes.xml"
扩展包提供的健康检查
此扩展包附带一些预定义的健康检查。默认情况下,这些检查未启用,以免干扰您的流程。您可以通过Symfony的配置组件启用和配置这些检查。
启用扩展包提供的健康检查
例如,要启用检查 doctrine.connection
,创建或修改文件 config/packages/healthcheck.yaml
并添加以下内容
# config/packages/healthcheck.yaml health_check: checks: doctrine.connection:
有关提供的健康检查列表,请参阅此处。
上述配置将启用所有可用检查器的doctrine连接检查。要使用检查器子集,请在配置中添加以下内容
# config/packages/healthcheck.yaml health_check: checks: doctrine.connection: checkers: - Browncat\HealthCheckBundle\Checker\LivenessChecker - Browncat\HealthCheckBundle\Checker\ReadinessChecker
有关可用检查器的列表,请参阅此处
提供的健康检查列表
创建自己的健康检查
健康检查定义在扩展 Browncat\HealthCheckBundle\Check\HealthCheck
的类中。例如,您可能想检查应用程序与远程系统之间的连接。
// src/Check/ExampleCheck.php <?php namespace App\Check; use Browncat\HealthCheckBundle\Check\HealthCheck; use Browncat\HealthCheckBundle\Checker\LivenessChecker; use Browncat\HealthCheckBundle\Checker\ReadinessChecker; use Psr\Container\ContainerInterface; final class ExampleCheck extends HealthCheck { // Name of the health check protected $name = 'example:connection'; // List of checkers who should execute this check. public static $checkers = [ReadinessChecker::class, LivenessChecker::class]; public function __construct(ContainerInterface $container) { if ($container->has('example')) { $exampleService = $container->get('example'); if (!$exampleService->isConnected()) { $this->succeeded = false; $this->reasonPhrase = "Could not establish connection to example " . $connection->getName() . "."; } else { $this->succeeded = true; } } else { $this->skipped = true; $this->reasonPhrase = "example is not installed so this check is skipped."; } } }
注册健康检查
健康检查必须作为服务注册,并带有 health_check.check
标签。如果您使用的是默认services.yaml配置,则这已经为您完成,多亏了自动配置。
命名检查
检查应该有一个通用名称。这确保了当执行大量检查时可以找到它。可以通过填充 protected $name
来命名检查。
// src/Check/ExampleCheck.php use Browncat\HealthCheckBundle\Check\HealthCheck; final class ExampleCheck extends HealthCheck { protected $name = 'example:connection'; ... }
通过或失败检查
可以通过将布尔值传递给 $succeeded
属性来失败或通过检查。
// src/Check/ExampleCheck.php ... use Browncat\HealthCheckBundle\Check\HealthCheck; ... final class ExampleCheck extends HealthCheck { public function __construct(SomeService $someService) { if ($someService->isLoaded() { $this->succeeded = true } else { $this->succeeded = false; // (optional) set a reason for the failed test $this->reasonPhrase = "SomeService Could not be loaded!"; } } }
跳过检查
要跳过检查,请将属性 $skipped
设置为true。
// src/Check/ExampleCheck.php ... use Browncat\HealthCheckBundle\Check\HealthCheck; use Psr\Container\ContainerInterface; ... final class ExampleCheck extends HealthCheck { public function __construct(ContainerInterface $container) { if (!$container->has('someService')) { $this->skipped = true; $this->reasonPhrase = 'SomeService is skipped because it does not exist.'; } ... } }
(可选)设置检查器
默认情况下,所有检查器(就绪性、活跃性和可能还有一些其他配置的检查器)都会运行您所编写的检查。如果您想将检查范围缩小到仅与特定的检查器一起运行,请将 public static $checkers
填充为所需检查器的类引用。
// src/Check/ExampleCheck.php use Browncat\HealthCheckBundle\Check\HealthCheck; use Browncat\HealthCheckBundle\Checker\ReadinessChecker; final class ExampleCheck extends HealthCheck { public static $checkers = [ReadinessChecker::class]; ... }
可用的检查器列表
Browncat\HealthCheckBundle\Checker\LivenessChecker
Browncat\HealthCheckBundle\Checker\ReadinessChecker
Browncat\HealthCheckBundle\Checker\StartupChecker
Browncat\HealthCheckBundle\Checker\GlobalHealthChecker
(此检查器无论什么情况都会处理所有已注册的检查)