ホームページ  >  記事  >  Java  >  Spring Boot アクチュエーターの使用に関する初心者ガイド

Spring Boot アクチュエーターの使用に関する初心者ガイド

WBOY
WBOYオリジナル
2024-08-02 10:00:24721ブラウズ

A Beginners Guide to Using Spring Boot Actuator

Spring Boot Actuator は、アプリケーションの監視と管理に役立つ実稼働対応の機能を提供する Spring Boot のサブプロジェクトです。これは、アプリケーションの正常性、メトリクス、環境に関する洞察を取得し、アプリケーションを動的に制御できる一連の組み込みエンドポイントを提供します。

スプリングブーツアクチュエーターとは何ですか?

Spring Boot Actuator は、アプリケーションの監視と対話に使用できる、すぐに使用できるエンドポイントをいくつか提供します。これらのエンドポイントには、HTTP、JMX 経由、または Spring Boot Admin を使用してアクセスできます。

スプリングブーツアクチュエーターの主な特徴

  1. ヘルスチェック: アプリケーションとその依存関係のヘルスチェックを監視します。
  2. メトリクス: メモリ使用量、ガベージ コレクション、Web リクエストの詳細などのさまざまなメトリクスを収集します。
  3. 環境情報: アプリケーションの環境プロパティにアクセスします。
  4. アプリケーション情報: バージョンや名前など、アプリケーションのビルドに関する情報を取得します。
  5. 動的ログ レベル: アプリケーションを再起動せずにログ レベルを変更します。
  6. HTTP トレース: HTTP リクエストをトレースします。

Spring Boot アクチュエータのセットアップ

1. アクチュエータの依存関係の追加

Spring Boot アプリケーションで Actuator を使用するには、pom.xml ファイルに Actuator の依存関係を追加する必要があります。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Gradle を使用している場合は、build.gradle ファイルに次のコードを追加します。

implementation 'org.springframework.boot:spring-boot-starter-actuator'

2. アクチュエーターエンドポイントの有効化

デフォルトでは、少数のエンドポイントのみが有効になっています。 application.yml ファイルで追加のエンドポイントを有効にできます:

management:
  endpoints:
    web:
      exposure:
        include: "*"  # This exposes all available endpoints
  endpoint:
    health:
      show-details: always  # Show detailed health information

アクチュエーターエンドポイントの使用

Actuator がセットアップされると、Actuator が提供するさまざまなエンドポイントにアクセスできるようになります。一般的に使用されるエンドポイントをいくつか示します:

1. 健康エンドポイント

/actuator/health エンドポイントは、アプリケーションの健全性に関する情報を提供します。

GET http://localhost:8080/actuator/health

応答例:

{
  "status": "UP",
  "components": {
    "db": {
      "status": "UP",
      "details": {
        "database": "H2",
        "result": 1
      }
    },
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 499963174912,
        "free": 16989374464,
        "threshold": 10485760,
        "exists": true
      }
    }
  }
}

2. メトリクスエンドポイント

/actuator/metrics エンドポイントは、アプリケーションに関連するさまざまなメトリクスを提供します。

GET http://localhost:8080/actuator/metrics

応答例:

{
  "names": [
    "jvm.memory.used",
    "jvm.gc.pause",
    "system.cpu.usage",
    "system.memory.usage",
    "http.server.requests"
  ]
}

特定のメトリクスの詳細を取得するには:

GET http://localhost:8080/actuator/metrics/jvm.memory.used

応答例:

{
  "name": "jvm.memory.used",
  "description": "The amount of used memory",
  "baseUnit": "bytes",
  "measurements": [
    {
      "statistic": "VALUE",
      "value": 5.1234567E7
    }
  ],
  "availableTags": [
    {
      "tag": "area",
      "values": [
        "heap",
        "nonheap"
      ]
    },
    {
      "tag": "id",
      "values": [
        "PS Eden Space",
        "PS Survivor Space",
        "PS Old Gen",
        "Metaspace",
        "Compressed Class Space"
      ]
    }
  ]
}

3. 環境エンドポイント

/actuator/env エンドポイントは、環境プロパティに関する情報を提供します。

GET http://localhost:8080/actuator/env

応答例:

{
  "activeProfiles": [],
  "propertySources": [
    {
      "name": "systemProperties",
      "properties": {
        "java.runtime.name": {
          "value": "Java(TM) SE Runtime Environment"
        },
        "java.vm.version": {
          "value": "25.181-b13"
        }
      }
    },
    {
      "name": "systemEnvironment",
      "properties": {
        "PATH": {
          "value": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        },
        "HOME": {
          "value": "/root"
        }
      }
    }
  ]
}

4. 情報エンドポイント

/actuator/info エンドポイントは、アプリケーションに関する情報を提供します。

GET http://localhost:8080/actuator/info

情報をカスタマイズするには、application.yml にプロパティを追加します。

info:
  app:
    name: My Spring Boot Application
    description: This is a sample Spring Boot application
    version: 1.0.0

アクチュエータのエンドポイントの保護

デフォルトでは、すべての Actuator エンドポイントは認証なしでアクセスできます。これらのエンドポイントを保護するには、Spring Security を使用できます。 Spring Security の依存関係を pom.xml に追加します:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

application.yml を更新してアクセスを制限します:

management:
  endpoints:
    web:
      exposure:
        include: "*"  # Expose all endpoints
  endpoint:
    health:
      show-details: always  # Show detailed health information

spring:
  security:
    user:
      name: admin  # Default username
      password: admin  # Default password

# Restrict actuator endpoints to authenticated users
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always
  security:
    enabled: true
    roles: ACTUATOR

HTTP セキュリティを構成するセキュリティ構成クラスを作成します:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/actuator/**").hasRole("ACTUATOR")
                .anyRequest().authenticated()
            .and()
            .httpBasic();
    }
}

この構成では、ACTUATOR ロールを持つ認証されたユーザーのみが Actuator エンドポイントにアクセスできます。

アクチュエータエンドポイントのカスタマイズ

カスタム Actuator エンドポイントを作成して、アプリケーションに固有の追加情報や機能を公開できます。カスタム エンドポイントの作成例を次に示します:

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

@Endpoint(id = "custom")
@Component
public class CustomEndpoint {

    @ReadOperation
    public String customEndpoint() {
        return "Custom Actuator Endpoint";
    }
}

カスタム エンドポイントにアクセスします:

GET http://localhost:8080/actuator/custom

結論

Spring Boot Actuator は、アプリケーションの監視と管理に役立つ堅牢なツール セットを提供します。組み込みのエンドポイントとカスタム エンドポイントを作成する機能を活用することで、アプリケーションのパフォーマンスと状態に関する貴重な洞察を得ることができます。これらのエンドポイントを Spring Security で保護して、承認されたユーザーのみがアクセスできるようにすると、管理と監視が簡単な実稼働対応のアプリケーションが得られます。

アクチュエーターは Spring Boot アプリケーションの重要な部分であり、アプリケーションのランタイム環境の状況を常に把握し、問題が発生したときに迅速に対応できるようにします。今すぐ Spring Boot Actuator の使用を開始して、アプリケーションの可観測性と運用機能を強化してください。

以上がSpring Boot アクチュエーターの使用に関する初心者ガイドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。