ge-tracker / influxdb-laravel
提供 InfluxDB 连接到 Laravel
v3.0.0
2024-01-19 10:56 UTC
Requires
- php: ^7.4|^8.0
- graham-campbell/manager: ^5.0
- illuminate/contracts: ^8.0|^9.0|^10.0
- illuminate/support: ^8.0|^9.0|^10.0
- influxdata/influxdb-client-php: ^3.4
Requires (Dev)
- guzzlehttp/guzzle: ^7.0.1
- orchestra/testbench: ^6.0|^7.0|^8.0
README
此包是influxdb-php客户端的 Laravel 包装器。我们使用graham-campbell/manager提供多个连接接口。
安装
此包需要 PHP 7.4+、Laravel 8+,并且与 InfluxDB 2.0/1.8+ 兼容。对于 InfluxDB 1.7 或更早版本,请参阅下一节中的 1.x 安装说明。
-
要在终端中安装最新版本的包,请运行以下命令
$ composer require ge-tracker/influxdb-laravel
Laravel 将自动发现位于
GeTracker\InfluxDBLaravel\InfluxDBLaravelServiceProvider
的包的服务提供者。 -
接下来,您应该发布应用程序的配置文件
$ php artisan vendor:publish
InfluxDB 1.x
要安装与 1.7x 兼容的版本,请安装此包的 1.x 版本。您可以在 GitHub 上查看 1.x 配置选项。
$ composer require "ge-tracker/influxdb-laravel:^1.0"
配置
发布后,此包的配置将位于 config/influxdb.php
。
默认连接名称
此选项('default'
)用于指定以下哪个连接作为所有工作的默认连接。当然,您可以使用管理器类同时使用多个连接。此设置的默认值为 'main'
。
InfluxDB 连接
此选项('connections'
)用于为应用程序设置每个连接。已包含一个示例配置,但您可以添加任意数量的连接。
用法
可以通过 Facade 或依赖注入访问底层 InfluxDB 连接实例。除非指定,否则包将默认使用 main
连接。
Facade
<?php // create an array of points $points = [ new InfluxDB\Point( 'test_metric', // name of the measurement null, // the measurement value ['host' => 'server01', 'region' => 'us-west'], // optional tags ['cpucount' => 10], // optional additional fields time() // Time precision has to be set to seconds! ), new InfluxDB\Point( 'test_metric', // name of the measurement null, // the measurement value ['host' => 'server01', 'region' => 'us-west'], // optional tags ['cpucount' => 10], // optional additional fields time() // Time precision has to be set to seconds! ) ]; $result = InfluxDB::writePoints($points, \InfluxDB\Database::PRECISION_SECONDS);
依赖注入
可以通过类型提示 InfluxDBManager
类来使用 DI
<?php namespace App; use GeTracker\InfluxDBLaravel\InfluxDBManager; class Foo { public function __construct( protected InfluxDBManager $influxDb ) { $this->influxDB = $influxDB; } public function bar() { return $this->influxDb->getQueryBuilder() ->select('usage, idle') ->from('cpu') ->where([ 'time < now() -1d', "host='host01'", ]) ->getResultSet(); } }
连接
InfluxDBManager
和 InfluxDB
Facade 都提供了一个 connection()
方法,允许与另一个 InfluxDB 连接交互
// The `main` connection will be used $manager->query("SELECT * FROM cpu"); // The `alternative` connection will be used $manager->connection('alternative')->query("SELECT * FROM cpu");