symsensor / actuator-bundle
可扩展的包,用于提供部署应用程序的运行时健康信息或信息
Requires
- symfony/framework-bundle: ^6.0 || ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.22
- mikey179/vfsstream: ^1.6
- phpstan/phpstan: ^1.10
- phpstan/phpstan-phpunit: ^1.1
- phpstan/phpstan-symfony: ^1.1
- phpunit/phpunit: ^10.3
- symfony/browser-kit: ^6.0
- symfony/yaml: ^6.0
README
ActuatorBundle 提供了基本两个功能
- 它提供了一个端点,可以用作健康探测 URL
- 另一个端点可以读取部署软件的运行时信息
它也非常容易扩展,因此您可以将自定义逻辑添加到这两个功能中。您还可以在这个GitHub 组织中找到一些预定义的扩展。
该包受到了akondas/symfony-actuator-bundle的极大启发。
安装
请确保全局安装了 Composer,如 Composer 文档中的安装章节所述。
使用 Symfony Flex 的应用程序
打开命令行,进入您的项目目录,并执行以下命令
$ composer require symsensor/actuator-bundle
不使用 Symfony Flex 的应用程序
步骤 1: 下载包
打开命令行,进入您的项目目录,并执行以下命令以下载此包的最新稳定版本
$ composer require symsensor/actuator-bundle
步骤 2: 启用包
然后,通过将其添加到项目 config/bundles.php
文件中注册的包列表中来启用该包
// config/bundles.php return [ // ... SymSensor\ActuatorBundle\SymSensorActuatorBundle::class => ['all' => true], ];
安装后,您必须在您的 config/routes.yaml
文件中配置路由
actuator: resource: '@SymSensorActuatorBundle/config/routing.yaml' prefix: /_actuator
路由定义了两个端点
<prefix>/health
<prefix>/info
您应该使用内置的 安全性 来保护这两个端点,以确保它们不是公开可访问的。
配置
可以使用名为 config/packages/sym_sensor_actuator.yaml
的配置文件来配置该包。以下代码片段显示了所有配置的默认值
sym_sensor_actuator: health: enabled: true builtin: disk_space: enabled: true threshold: 52428800 path: '%kernel.project_dir%' info: enabled: true builtin: php: enabled: true symfony: enabled: true git: enabled: true
以下表格概述了配置
扩展
健康指示器
您可以编写自己的健康指示器,并实现自己的逻辑来确定应用程序的状态。为此,您必须实现接口 HealthIndicator
并使用标签 sym_sensor_actuator.health_indicator
标记您的服务。
例如,在 src/Health/CustomHealthIndicator.php
下添加以下类
<?php declare(strict_types=1); namespace App\Health; use SymSensor\ActuatorBundle\Service\Health\HealthIndicator; use SymSensor\ActuatorBundle\Service\Health\Health; class CustomHealthIndicator implements HealthIndicator { public function name(): string { return 'custom'; } public function health(): Health { return Health::up()->setDetails(['state' => 'OK!']); } }
然后在 config/services.yaml
中添加以下定义
services: App\Health\CustomHealthIndicator: tags: ['sym_sensor_actuator.health_indicator']
信息收集器
与健康指示器类似,您也可以编写一个暴露信息的服务。为此,您必须实现接口 Collector
并添加标签 sym_sensor_actuator.info_collector
。
<?php declare(strict_types=1); namespace App\Info; use SymSensor\ActuatorBundle\Service\Info\Collector\Collector; use SymSensor\ActuatorBundle\Service\Info\Info; class CustomInfoCollector implements Collector { public function collect(): Info { return new Info('my-info', [ 'time' => time() ]); } }
然后在 config/services.yaml
中添加以下定义
services: App\Info\CustomInfoCollector: tags: ['sym_sensor_actuator.info_collector']
许可
ActuatorBundle 在 MIT 许可下发布。有关详细信息,请参阅捆绑包中的 LICENSE 文件。
作者
最初由 Arkadiusz Kondas 开发