In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "sample Analysis of SpringBoot Index Monitoring actuator", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample Analysis of SpringBoot Index Monitoring actuator".
1. Write at the front
First of all, we must talk about the four cores of SpringBoot:
Automatic assembly: simple configuration or even zero configuration can run the project
Start-up dependence: scene initiator
Actuator: metrics monitoring
Command line interface: command line
After every microservice of 2.SpringBoot Actuator is deployed on the cloud in the future, we need to monitor, track, audit, control and so on. SpringBoot extracts Actuator scenarios, so that we can quickly reference each micro-service to obtain production-level application monitoring, audit and other functions.
To enable metrics monitoring, you first need to add the following dependencies to the pom file:
Org.springframework.boot spring-boot-starter-actuator
Then make the following configuration in the configuration file:
Server: port: 8080 # expose all monitoring information: HTTPmanagement: endpoints: enabled-by-default: true # enable all monitoring endpoint information by default web: exposure: include:'*'# expose all endpoints in web mode
Then start the project and test it:
The content of the test in the following figure is the various index parameter information that can be monitored in the current project.
In the metric monitoring function, there is a word that is often mentioned: endpoint. The common endpoints that are commonly used are as follows:
Above, we visit the url monitored by metrics: http://localhost:8080/actuator/ can get all the endpoint information. So if you want to get some endpoint information, url should be:
Http://localhost:8080/actuator/endpointName/detailPath .
Health check endpoints are commonly used on cloud platforms, where the platform will regularly check the health status of the application. We need Health Endpoint to return a collection of health status of a series of components of the current application for the platform.
The important points are:
The result returned by health endpoint should be a summary report after a series of health examinations.
Many health checks are automatically configured by default, such as database, redis, etc.
You can easily add a custom health check-up mechanism
Provides detailed, hierarchical, spatial metric information that can be obtained by pull (active push) or push (passive acquisition)
Interfacing multiple monitoring systems through Metrics
Simplify core Metrics development
Add a custom Metrics or extend an existing Metrics
The above test results are that we get the detailed metric information of an endpoint according to the current project.
In addition, we can also turn on or disable these endpoints manually. (see configuration file below)
Server: port: 8080 # expose all monitoring information: HTTPmanagement: endpoints: enabled-by-default: false # enable all monitoring endpoint information by default web: exposure: include:'*'# expose all endpoints in web mode # you need to enable or disable an Endpoint# configuration mode: management.endpoint..enabled = true/false endpoint: health: show-details: Always # always shows the details of health endpoints enabled: true info: enabled: true beans: enabled: true
The test screenshot above is what happens when we manually open some endpoints and close some endpoints.
3. Customized Endpoint3.1 Custom health Endpoint Information
All of the above is using the official Endpoint provided to us by SpringBoot, so we can also customize Endpoint (that is, customized Endpoint).
There are two ways: ① inherits the AbstractHealthIndicator abstract class (doHealthCheck (Health.Builder builder) method), and ② implements the HealthIndicator interface (overriding the health () method). I'm going to do a simple test in the first way.
Package com.szh.boot.health; import org.springframework.boot.actuate.health.AbstractHealthIndicator;import org.springframework.boot.actuate.health.Health;import org.springframework.boot.actuate.health.Status;import org.springframework.stereotype.Component; import java.util.HashMap;import java.util.Map / * * * / @ 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 (); / / simulate the check process if (1 = = 1) {/ / builder.up () / / healthy builder.status (Status.UP); map.put ("count", 1); map.put ("ms", 100);} else {/ / builder.down (); / / downtime builder.status (Status.DOWN); map.put ("error", "connection timeout") Map.put ("ms", 3000);} builder.withDetail ("code", 20001) .withdetails (map);}}
The configuration file is as follows:?
Server: port: 8080 # expose all monitoring information: HTTPmanagement: endpoints: enabled-by-default: false # enable all monitoring endpoint information by default web: exposure: include:'*'# expose all endpoints in web mode # you need to enable or disable an Endpoint# configuration mode: management.endpoint..enabled = true/false endpoint: health: show-details: Always # always displays the details of health endpoints enabled: true
Then we start the test and access the path: http://localhost:8080/actuator/health. We can see from the result that there is an endpoint myCom that we customized (named by removing the following HealthIndicator from the MyComHealthIndicator class).
3.2Custom info endpoint information
First add the following to the configuration file: (the last few lines)
Server: port: 8080 # expose all monitoring information: HTTPmanagement: endpoints: enabled-by-default: false # enable all monitoring endpoint information by default web: exposure: include:'*'# expose all endpoints in web mode # you need to enable or disable an Endpoint# configuration mode: management.endpoint..enabled = true/false endpoint: health: show-details: Always # always shows the details of health endpoints enabled: trueinfo: enabled: true beans: enabled: trueinfo: appName: spring-boot-actuator-endpoint-info version: 2.0.0 mavenProjectName: @ project.artifactId@ mavenProjectVersion: @ project.version@
Then create a class that implements the interface InfoContributor and overrides the contribute (Info.Builder builder) method in the interface.
Package com.szh.boot.info; import org.springframework.boot.actuate.info.Info;import org.springframework.boot.actuate.info.InfoContributor;import org.springframework.stereotype.Component; import java.util.Collections; / * * * / @ Componentpublic class ExampleInfoContributor implements InfoContributor {@ Override public void contribute (Info.Builder builder) {builder.withDetail ("example", Collections.singletonMap ("key", "value"));}}
Finally, let's start the test and access the path: http://localhost:8080/actuator/info. The data we get is what we wrote in code above.
The above is all the contents of the article "sample Analysis of SpringBoot Metrics Monitoring actuator". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.