Spring Actuator Source Analysis

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/wsscy2004/article/details/50166333

Actuator Endpoint

The actuator module exposes some interfaces through endpoint, which can be rest or jmx, among other ways.

If you use rest mode, usually SPRINGMVC is using @requestmapping, and @controller labels a controller method, if you do not use SPRINGMVC, you do not introduce a SPRINGMVC package. Then Springboot will go wrong. So in order not to take the normal SPRINGMVC mechanism, actuator with endpointhandlermapping rewrite requestmappinginfohandlermapping, Matching is the "controller" that implements the Mvcendpoint interface.

Mvcendpoint is the endpoint SPRINGMVC layer decoration, added @requestmapping, as well as @responsebody. Specific logic delegated to endpoint processing. The ID of the endpoint is the URL.

The method for customizing endpoint is already mentioned in the documentation.

Health Check

Healthendpoint is actuator comes with the health check, the specific check operation is to healthindicator processing, according to the document, implementation healthindicator can customize some of the health check logic As follows

@Component  Public class Implements healthindicator {    @Override    Public Health Health  () {  return  New  Health.builder ()    . Withdetail (//  Some logic check tair    //  Some logic check TFS    . Status ("$")    . Down ()    . Build ();}    }

Now visit the health endpoint:

$ curl http://localhost:8080/health{    "status": "Down",    "Tair": " Timeout ",    " TFS ":" OK "}

Healthindicatorautoconfiguration will automatically configure all healthindicator before endpointautoconfiguration
Actuator has brought some healthindicator, auto-enable parts:

Multiple healchindicator will be called by Compositehealthindicator Healthaggregator do aggregate (aggregation), currently only orderedhealthaggregator, for sorting

Metrics Endpoint

This endpoint shows metrics information, the specific metrics is implemented by the Publicmetrics interface class processing.
Metricsendpoint maintains a list of publicmetrics, actuator has been implemented as follows:

All active publicmetrics can be accessed by visiting /metrics :

{    "counter.status.200.root": +,    "Counter.status.200.metrics": 3,    "Counter.status.200.star-star": 5,    "Counter.status.401.root": 4,    "Gauge.response.star-star": 6,    "Gauge.response.root": 2,    " Gauge.response.metrics ": 3,    " classes ": 5808,    " classes.loaded ": 5808,    " classes.unloaded ": 0,    " heap ": 3728384,    " heap.committed ": 986624,    " Heap.init ": 262144,    " heap.used ": 52765,    " mem ": 986624,    "Mem.free": 933858,    "processors": 8,    "Threads": "    Threads.daemon": One, "    threads.peak" : "    uptime": 494836,    "Instance.uptime": 489782,    "datasource.primary.active": 5,    " Datasource.primary.usage ": 0.25}



With this publicmetrics you can get access to the controller:

" Gauge.response.hi ": 5,"counter.status.200.hi": 19,

Hi interface Response time, number of visits, respectively.

The whole process:
Metricrepositoryautoconfiguration-Counterbuffers,gaugebuffers for saving count data
Metricrepositoryautoconfiguration, initialize Gaugeservice + counterservice (Counterbuffers,gaugebuffers included)
Metricfilterautoconfiguration, initialize Metricsfilter, which 过滤器 uses Gaugeservice + Counterservice statistical access times and response times
Publicmetricsautoconfiguration, initialize metricreaderpublicmetrics, plug into Compositemetricreader (CounterBuffers, Gaugebuffers).
Metricreaderpublicmetrics reading Counterbuffers,gaugebuffers saved statistics

Let's focus on MetricsFilter this filter:

Custom Metrics

Depending on the document, you can inject Counterservice or gaugeservice to statistics in your business code:

Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.boot.actuate.metrics.CounterService;ImportOrg.springframework.stereotype.Service; @Service Public classMyService {Private FinalCounterservice Counterservice; @Autowired PublicMyService (Counterservice counterservice) { This. Counterservice =Counterservice; } @PostConstruct Public voidExamplemethod () { This. Counterservice.increment ("services.system.myservice.invoked"); }}
   @PostConstruct must be added

Of course, you can use AOP to do a method level statistic. But I want to do a business-agnostic, integrated into the framework of metrics statistics



Spring Actuator Source Analysis (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.