Spring Boot Reference Tutorial (iii) Internal application monitoring (actuator)

Source: Internet
Author: User
Tags spring boot actuator

3. Internal application Monitoring (actuator)

As mentioned in the previous 2.4, the traditional spring engineering in the initialization process,The bean's life cycle, the application of internal health conditions can not be monitored, in order to solve this problem,Spring boot provides a actuator actuator.

Here is a detailed description of what is actuator and how to use it.

1 Enable Actuator

3.1.1. Join the dependency

Add the following Starter Pom:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

3.1.2. Configuration

To add a configuration to the Application.properties configuration file:

management.security.enabled=false

 

注:因Actuator会将应用内部信息的端点暴露出来,这样就会产生安全问题,

Spring Boot默认将Springsecurity加入了工程,因此默认此配置是true,禁用安全以便测试。

3.1.3. Start

Direct Run as javaapplication boot.

3.1.4 Test

Test results above

Direct access to Http://localhost:8080/beans.

The JSON documents returned are all the beans in the in-app context, as well as the interrelationships.

2 Endpoints (endpoint)

The key feature of Spring Boot actuator is that it provides many web endpoints in the application to understand the internal state of the application as it runs. With actuator, you can see how beans are assembled together in the context of the spring application, mastering the Environment property information that the application can get, taking snapshots of runtime metrics, and so on.

The actuator provides 13 endpoints:

http method Path description

Get/autoconfig provides an automated configuration report that records which automatic configuration conditions have passed and which failed

Get/configprops describes how configuration properties (including default values) are injected into the bean

Get/beans describes all the beans in the application context, and their relationships

Get/dump taking a snapshot of thread activity

Get/env Get all environment properties

Get/env/{name} Gets a specific environment property value by name

Get/health reports the health metrics of the application, which are provided by the Healthindicator implementation class

Get/info gets custom information for the application, which is provided by the attributes that begin with info

Get/mappings describes all URI paths and their mappings to the controller (including the actuator endpoint)

Get/metrics reports various application metrics, such as memory usage and HTTP request count

Get/metrics/{name} report The application metrics for the specified name

Post/shutdown close application, requires endpoints.shutdown.enabled to be set to True

Get/trace provides basic HTTP request tracking information (timestamp, HTTP prime)

For details on each of the endpoints, please read theSpring boot combat-Jonathan Allen seventh chapter in-depth actuator.

I provide the electronic version of the book, no longer repeat it here. This chapter focuses on how to configure the use of actuator.

3 configuration using

For actuator Configuration items, you can view the Commonapplication Properties section of Spring's official spring Boot referenceguide .

3.1 Enable disable endpoint

Although actuator endpoints are useful, you don't necessarily need all these endpoints. By default, all endpoints (except

/shutdown) are enabled. set endpoints.endpoint-id.enabled to false to disable the endpoint and set endpoints.endpoint-id.enabled to true to start the endpoint.

Test Disable endpoint: Modify configuration file, add Endpoints.beans.enabled=false. Access after startup:

3.2 Modifying Endpoint IDs

If you want to modify the endpoint's fixed ID to a custom ID, add the configuration: endpoints.*.id=newid.

Test: Add Endpoints.beans.id=springbeans to the configuration file after boot access:

3.3 monitoring applications with JMX

Actuator also publishes its endpoints as an Mbean, which can be viewed and managed through JMX. The actuator endpoints are published under the Org.springframework.boot domain. Use Jcnsole.exe to view the following:

3.4 Adding custom metric information

get internal metrics from the/metrics endpoint for running applications, including memory, garbage collection, and thread information, if you want to define your own metrics as follows:

L Spring provides the following two interfaces and their uses:

Counterservice This interface defines three methods that are used to increase, decrease, or reset measures of a particular name, as shown in the following code:

Packageorg.springframework.boot.actuate.metrics;

Publicinterface Counterservice {

Voidincrement (String metricname);

Voiddecrement (String metricname);

Voidreset (String metricname);

}

The Gaugeservice interface is similar to Counterservice and can record a value in a measure of a particular name.

Packageorg.springframework.boot.actuate.metrics;

Publicinterface Gaugeservice {

Voidsubmit (String metricname, double value);

}

Spring Boot has provided both implementations. All we have to do is inject their instances into the required beans, call the methods at the appropriate time, and update the desired metrics.

Write the following code:

L Start test

Another: the injection of beans can be used in another way:

There are more ways to use actuator, such as adding custom tracking warehouses, inserting custom health metrics, and more.

The author does not demonstrate, if interested, can refer to "Spring boot combat."

3.5 Changing the endpoint path

In practice, the exposed endpoints may need to be grouped in order to separate the exposed endpoints from the application's business interface, or to facilitate security control, and to facilitate the use of monitoring information in-depth development of the endpoint.

To add a configuration to the configuration file:

Management.context-path=/endpoints

To start the test:

3.6 Endpoint Protection

3.3.6.1 using springsecurity configuration Items

Application security mechanism, we can use Spring security,Spring security is for the entire application, after use to access the application, the HTTP Basic Authentication dialog box will pop up.

L Join Dependency

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-security</artifactId>

</dependency>

L Join Configuration

Security.user.name=admin

Security.user.password=admin

Management.security.roles=superuser

L Start test

You can enter a configuration password, and in general development you will not use the configuration file to manage the application permissions, but instead use the database or LDAP.

3.3.6.2 Custom Security Configuration

We can use custom security configurations to override the automatic configuration options, which are not detailed here, and the subsequent chapters will detail the details of Springboot integrated with Spring security.

< instance code >

Github:https://github.com/chunyuding/springboot-demo

Https://github.com/chunyuding/SpringBoot-Demo.git

Code Cloud: Https://gitee.com/dingchunyu/SpringBoot-Demo

Https://gitee.com/dingchunyu/SpringBoot-Demo.git

< recommended books >

Baidu Cloud: Http://pan.baidu.com/s/1qYA0Nxi

Spring Boot Reference Tutorial (iii) Internal application monitoring (actuator)

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.