Get the App
SLTechnology News&Howtos  ›  Development  › 

How to monitor spring boot

Shulou Source: shulou.com Published: 2022-06-02 22:47:32 09月17日 Update

This article will explain in detail how to monitor spring boot. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

I. Index monitoring

Introduce the jar package:

Org.springframework.boot spring-boot-actuator

Enable it in web mode:

# enable all management.endpoints.enabled-by-default=true#web methods to expose management.endpoints.web.exposure.include=*

Second, commonly used monitoring endpoints

Look at this: the portal.

The most commonly used:

Health: health status to see if the app is available

Metrics:

Runtime metrics, JVM, threads, etc. (important)

Loggers:

Log record

Third, customize EndPoint

It is relatively simple to customize component health information, and you can also implement the interface mode:

Package com.example.demo; import org.springframework.boot.actuate.health.AbstractHealthIndicator;import org.springframework.boot.actuate.health.Health;import org.springframework.stereotype.Component; import java.util.HashMap;import java.util.Map / * * @ author Administrator * / @ Componentpublic class MyComHealthIndicator extends AbstractHealthIndicator {/ * Real check method * @ param builder * @ throws Exception * / @ Override protected void doHealthCheck (Health.Builder builder) throws Exception {Map map = new HashMap (); if (1 check 1) {builder.up (); map.put ("count", 1) Map.put ("msg", "health");} else {builder.down (); map.put ("msg", "timeout");} builder.withDetail ("code", 100) .withdetails (map);}}

Definition of INFO Endpoint:

1. The configuration file directly defines:

Info.mavenProjectName = @ project.artifactId@info.mavenProjectVersion=@project.version@

2. Write code:

Package com.example.demo; import org.springframework.boot.actuate.info.Info;import org.springframework.boot.actuate.info.InfoContributor;import org.springframework.stereotype.Component; @ Componentpublic class AppInfo implements InfoContributor {@ Override public void contribute (Info.Builder builder) {builder.withDetail ("msg", "really handsome!") ;}}

Metrics customizes endpoint, using MeterRegistry directly.

Custom Endpoint to monitor endpoints:

Package com.example.demo; import org.springframework.boot.actuate.endpoint.annotation.Endpoint;import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;import org.springframework.stereotype.Component; import java.util.Collections;import java.util.Map; @ Component@Endpoint (id = "myEndPoint") public class EndPoint {@ ReadOperation public Map read () {return Collections.singletonMap ("MG", "MG GOGO") } @ WriteOperation public void write () {System.out.println (tired);}}

When you access a custom metric, you access the read method

4. Spring boot admin (can be used)

Prepare a server that will regularly get the relevant content of each service.

De.codecentric spring-boot-admin-starter-server

Client registration:

De.codecentric spring-boot-admin-starter-client

Configuration properties file:

Spring: application: name: admin-client boot: admin: client: url: http://localhost:8769 interface:# use IP to register prefer-ip: tureserver: port: 8768 management: endpoints: web: exposure: include:'* endpoint: health: show-details: ALWAYS this article on "how spring boot is monitored" ends here Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Tags: Monitoring health content metrics methods articles commonly used files methods more endpoints configuration good practical important code information at the same time customers clients Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Docker macOS Linux Shulou Information