Spring Bootの主な機能はAutoConfig(自動構成)であり、米国のユーザーにとっては、さまざまなスターターです。
Spring Boot-Actuatorは、スターターも提供します。これは、自動的に構成されています。使用に関しては、スターターを依存関係に追加してからプロジェクトを開始するだけです。
<Dependency> groupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-actuator </artifactid> </dependency>
一般的に使用されるエンドポイント
Spring Boot-Actuatorは多くの便利なエンドポイントを提供し、Spring Bootアプリケーションのさまざまな監視を提供します。一般的に使用されているエンドポイントについて話しましょう。
/健康アプリケーションの健康状態
/configPropsスプリングブートが公開されたときに個別のJARパッケージになり、構成ファイルが含まれる可能性があるため、アプリケーションの構成情報を取得します。構成ファイルを確認する必要がある場合、ConfigPropsEndPointを使用して、一部の構成が正しいかどうかを確認できます。
/最新のHTTP要求情報をトレースします
HealthEndPoint
http:// localhost:8088/Healthにアクセスすると、HealthEndPointがディスク検出やデータベース検出などのデフォルトの監視結果を提供することがわかります。
{"status": "up"、 "diskspace":{"status": "up"、 "total":398458875904、 "free":315106918400、 "threshold":10485760}、 "db":{"" status ":" "": "" "" "実際、Spring Boot-Actuatorのソースコードを見ると、HealthEndPointが提供する情報はこれに限定されないことがわかります。 org.springframework.boot.actuate.healthパッケージの下には、elasticsearchhealthindicator、redishealthindicator、rabbithealthindicatorなどがあります。
つまり、HealthEndPointは、ES、Redisなどのコンポーネントの健康情報も提供します。
カスタムインジケータは、HealthEndPointを拡張します
ソースコードを見ると、ディスクとデータベースの健康情報は、diskspacehealthindicatorおよびdatasourcehealthindicatorによって実装されます。カスタマイズされたコンポーネントの一部を監視すると、インジケーターを実装することもできます。
@ComponentPublic ClassユーザーはHealthIndicator {/** *ユーザー監視アクセス:http:// localhost:8088/health * * @return custom public Health(){return new Health.builder()。 "up")。up()。build(); }}この時点で、http:// localhost:8088/healthこの時点で返された結果は、カスタマイズされたユーザーの健康情報を含め、次のとおりです。
{"status": "up"、 "user":{"status": "up"、 "usercount":10、 "userstatus": "up"}、 "diskspace":{"status": "up"、 "total":398458875904、 "free":315097989120、 ":10485760 「ステータス」:「アップ」、「データベース」:「mysql "、" hello ":1}}カスタムエンドポイント
実際、HealthEndPointを拡張していくつかのヘルスチェックを追加することに加えて、プログラムランタイム中にいくつかの情報表示を提供するためにいくつかのエンドポイントをカスタマイズすることもできます。
@configurationpublic class endpointautoconfig {@bean public endpoint <map <string、object >> customendpoint(){return new SystemEndpoint(); }}@configurationProperties(prefix = "endpoints.customsystem")public class systemendpoint extends abstractendpoint <map <string、object >> {public systemendpoint(){super( "customsystem"); } @Override public Map <string、object> invoke(){map <string、object> result = new Hashmap <>(); map <string、string> map = system.getenv(); result.put( "username"、map.get( "username")); result.put( "ComputerName"、map.get( "ComputerName")); result.put( "userdomain"、map.get( "userdomain"));返品結果; }}http:// localhost:8088/customsystemにアクセスして、カスタマイズされたエンドポイントを表示します。結果は次のとおりです。
{"username": "xxx"、 "userdomain": "desktop-6ean1h4"、 "computername": "desktop-6ean1h4"}スプリングブートアプリケーションにアクチュエータを追加した後、予想されるヘルスインターフェイスが結果を返します。
{status: "up"、diskspace:{status: "up"、合計:250182889472、無料:31169568768、しきい値:10485760}、db:{status: "up"、database: "h2"、hello:1}}}}ステータスが返されたばかりの場合
{ステータス: "Up"}アプリケーションに新しい構成を追加する必要があります。 YML構成ファイルを例として使用すると、次の構成を追加する必要があります。
管理:セキュリティ:有効:falseEndpoints:健康:敏感:false
management.endpoint.health.show-details = Always
要約します
上記は、編集者によって導入されたスプリングブート実装プロジェクトの健康チェックと監視です。私はそれが誰にでも役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は、すべての人に時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!