Prometheus + Grafana によるオンライン アプリケーションの観察、監視、早期警告...
- ヘルスステータス [コンポーネントのステータス、生存ステータス] ヘルス
- 実行インジケーター [CPU、メモリ、ガベージ コレクション、スループット、応答成功率...] メトリクス
- …
1. スプリングブートアクチュエーター
1. 基本的な使い方
1. シーン紹介
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. 露出インジケーター
management:
endpoints:
enabled-by-default: true #暴露所有端点信息
web:
exposure:
include: '*' #以web方式暴露
3. データにアクセスする
- http://localhost:8080/actuatorにアクセスし、利用可能なすべての監視エンドポイントを表示します
- http://localhost:8080/actuator/beans
- http://localhost:8080/actuator/configprops
- http://localhost:8080/actuator/metrics
- http://localhost:8080/actuator/metrics/jvm.gc.pause
- http://localhost:8080/actuator/エンドポイント名/detailPath
2. エンドポイント
1. 共通エンドポイント
ID | 説明する |
---|---|
auditevents |
現在のアプリケーションの監査イベント情報を公開します。AuditEventRepository コンポーネントが必要です |
beans |
アプリケーション内のすべての Spring Bean の完全なリストを表示します。 |
caches |
利用可能なキャッシュを公開する |
conditions |
一致または不一致の理由を含む、自動構成のすべての条件情報を表示します。 |
configprops |
すべて表示する@ConfigurationProperties |
env |
Spring プロパティを公開するConfigurableEnvironment |
flyway |
適用されたすべての Flyway データベース移行を表示します。1 つ以上のFlyway コンポーネントが必要です。 |
health |
アプリケーションの正常性情報を表示する |
httptrace |
HTTP トレース情報を表示します (デフォルトでは、最後の 100 件の HTTP リクエスト/レスポンス)。HttpTraceRepository コンポーネントが必要です |
info |
アプリケーション情報を表示する |
integrationgraph |
春を表示しますintegrationgraph 。依存する必要があるspring-integration-core |
loggers |
アプリケーション内のログの構成を表示および変更する |
liquibase |
適用されたすべての Liquibase データベース移行を表示します。1 つ以上のLiquibase コンポーネントが必要です |
metrics |
現在のアプリケーションの「メトリクス」情報を表示します |
mappings |
@RequestMapping すべてのパスのリストを表示 |
scheduledtasks |
アプリケーションにスケジュールされたタスクを表示する |
sessions |
Spring Session がサポートするセッション ストアからユーザー セッションを取得および削除できるようにします。Spring Session の使用を必要とするサーブレットベースの Web アプリケーション |
shutdown |
アプリケーションを正常にシャットダウンします。デフォルトで無効になっています |
startup |
ApplicationStartup によって収集された起動ステップ データを表示します。SpringApplication を使用して設定する必要がありますBufferingApplicationStartup |
threaddump |
スレッドダンプを実行する |
heapdump |
hprof ヒープダンプファイルを返す |
jolokia |
HTTP 経由で JMX Bean を公開します (Jolokia の導入が必要ですが、WebFlux には適用されません)。依存関係を導入する必要があるjolokia-core |
logfile |
ログ ファイルの内容を返します (設定logging.file.name またはlogging.file.path プロパティの場合)。HTTP Range ヘッダーを使用した部分的なログ ファイルの内容の取得のサポート |
prometheus |
Prometheus サーバーが収集できる形式でメトリクスを公開します。依存する必要があるmicrometer-registry-prometheus |
2. カスタムエンドポイント
- ヘルスモニタリング: 生還、死亡
- インジケーターモニタリング: 周波数、レート
1.ヘルスエンドポイント
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class MyHealthIndicator implements HealthIndicator {
@Override
public Health health() {
int errorCode = check(); // perform some specific health check
if (errorCode != 0) {
return Health.down().withDetail("Error Code", errorCode).build();
}
return Health.up().build();
}
}
构建Health
Health build = Health.down()
.withDetail("msg", "error service")
.withDetail("code", "500")
.withException(new RuntimeException())
.build();
management:
health:
enabled: true
show-details: always #总是显示详细信息。可显示每个模块的状态信息
@Component
public class MyComHealthIndicator extends AbstractHealthIndicator {
/**
* 真实的检查方法
* @param builder
* @throws Exception
*/
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
//mongodb。 获取连接进行测试
Map<String,Object> map = new HashMap<>();
// 检查完成
if(1 == 2){
// builder.up(); //健康
builder.status(Status.UP);
map.put("count",1);
map.put("ms",100);
}else {
// builder.down();
builder.status(Status.OUT_OF_SERVICE);
map.put("err","连接超时");
map.put("ms",3000);
}
builder.withDetail("code",100)
.withDetails(map);
}
}
2. メトリクスエンドポイント
class MyService{
Counter counter;
//默认一个构造时,参数会从ioc中拿
public MyService(MeterRegistry meterRegistry){
counter = meterRegistry.counter("myservice.method.running.counter");
}
public void hello() {
counter.increment();
}
}
2. 着陸を監視する
プロメテウス + グラファナに基づく
1. Prometheus + Grafana をインストールする
Prometheus + Grafana をインストールする
2. 依存関係をインポートする
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.10.6</version>
</dependency>
management:
endpoints:
web:
exposure: #暴露所有监控的端点
include: '*'
アクセス: http://localhost:8001/actuator/prometheus検証、すべてのインジケーターを prometheus 形式で返します
Java アプリケーションをサーバーにデプロイする
デプロイされたサービスhttp://192.168.254.129:8080/actuator/prometheusにアクセスできることを確認してください。
http://192.168.254.129:8080/アクチュエーター
3. データをプルするように Prometheus を構成する
## 修改 prometheus.yml 配置文件
scrape_configs:
- job_name: 'spring-boot-actuator-exporter'
metrics_path: '/actuator/prometheus' #指定抓取的路径
static_configs:
- targets: ['192.168.254.129:8080']
labels:
nodename: 'app-demo'
構成後は必ずコンテナを再起動してください
4. Grafana 監視パネルを構成する
- データソースの追加 (Prometheus)
- パネルを追加します。Grafana ダッシュボード マーケットにアクセスして好みのパネルを見つけることも、独自のパネルを開発することもできます
- マーケットでSpringBootを直接検索し、パネルがサポートするデータソースに注意して、パネルIDをコピーします
パネル ID を入力し、作成したばかりのデータ ソースを選択します
5.効果
アプリケーションの実行をしばらく待つと、対応する監視データが表示されます