talentrydev / health-check
PHP库和symfony包,用于执行健康检查
4.0.0
2024-05-31 13:27 UTC
Requires
- php: ^8.3
- ext-json: *
- ext-pdo: *
- predis/predis: ^1.1 || ^2.0
- psr/log: ^1.1 || ^2 || ^3
- symfony/framework-bundle: ^6.0
- symfony/yaml: ^6.0
Requires (Dev)
- phpunit/phpunit: ^11
- psr/simple-cache: ^3
- squizlabs/php_codesniffer: ^3.7
- symfony/browser-kit: ^6.0
- symfony/monolog-bundle: ^3.10
README
此模块提供了在您的应用程序中公开健康检查端点的基本工具。它可以在纯PHP代码中使用,但为了最大程度地简化使用,建议使用提供的symfony包。
安装和配置symfony包
- 运行
composer require talentrydev/health-check
- 将symfony包添加到内核的
registerBundles
方法中
return [
//...
new \Talentry\HealthCheck\SymfonyBundle\HealthCheckBundle();
];
- 通过向您的symfony config.yaml文件中添加以下内容来配置包。有关所有可用选项及其默认值(所有内容都是可选的),请参阅以下内容。
health_check:
service_name: API //this is the name of the root service that will appear in the health check endpoint response
mysql:
enabled: true //set to false to disable the mysql health checker
host: localhost
port: 3306
user: root
password: root
database: mysql
redis:
enabled: true //set to false to disable the redis health checker
host: localhost
port: 6379
- 向您的路由配置中添加以下内容
healthcheck:
resource: "@HealthCheckBundle/Resources/config/routing.yml"
使用健康检查端点
向以下位置发送GET HTTP请求
这将执行应用程序自身的健康检查,但不会检查依赖服务。若要同时检查这些服务,请将它们添加到URL中,如下所示
<base_url>/healthcheck?mysql=1&redis=1
或指定dependencies=1
来运行对所有依赖的健康检查
<base_url>/healthcheck?dependencies=1
扩展包
要创建自己的健康检查器,只需实现Talentry\HealthCheck\HealthChecker\HealthChecker
接口。在getServiceName
的返回中指定的名称是您将传递给健康检查端点的名称。