Objective
As we serve more and more and deploy more and more environments, as each service is deployed on different machines, it becomes troublesome to locate the problem quickly whenever there is a problem or an exception. Therefore, this section begins with SpringBoot
the monitoring-related knowledge points that begin to be explained. This chapter will first say that its own Actuator
features.
- A little knowledge
- Actuator Practice
- Native Endpoint
- Get Started Practice
- Custom Endpoints
- Customizing health Endpoints
- Custom Endpoints
- Resources
- Summarize
- At last
- Cliché
A little knowledge
Spring Boot Actuator
The spring boot
project is a monitoring module that provides a number of native endpoints, includes integrated functionality for introspection and monitoring of the application, and can view details of the application configuration, such as all beans in the application context, Health metrics , environment variables and a variety of important metrics , and so on, these are used to HTTP
request access. With these monitoring information, we can keep an eye on how the app is running.
Actuator Practice
Special Note:
Recently in the SpringCloud
tutorial, using the SpringBoot2.x
version, its and 1.x
version is different, need to open an additional endpoint, by default only info、health
two endpoints, others need to be configured. This tutorial is still a copy of the Spring Boot 1.5.15
original example. About 2.x
the follow up to update it, or it will be messy.
Native Endpoint
The native endpoint is a variety of WEB interfaces in the application that understand the internal state of the application as it runs. The native endpoint can also be divided into three categories:
- Application Configuration class: You can view static information of the application at run time: for example, automatic configuration information, loaded Springbean information, yml file configuration information, environment information, request mapping information;
- Metric class: Mainly the dynamic information of the running period, such as stack, request connection, some health index, metrics information, etc.
- Operation Control class: mainly refers to the shutdown, the user can send a request to the application of the monitoring function off.
The official website can see that there are many native endpoints
The following is a list of the built-in endpoint instructions:
ID |
Description |
does it need authentication | ?
actuator |
Provides the discovery page for other endpoints. Requires spring Hateoas on the classpath path. |
Need |
auditevents |
Displays audit event information for the current application. |
Need |
autoconfig |
Show automatic configuration information and show all automatically configured candidates and why they are "not" applied. |
Need |
beans |
Displays a complete list of all spring beans in the application. |
Need |
configprops |
Displays all configuration information. |
Need |
dump |
Dump all threads. |
Need |
env |
All environment variables are displayed. |
Need |
flyway |
Shows any Flyway database migrations that has been applied. |
Need |
health |
displaying application health information |
Don't need |
info |
Displays app information. |
Don't need |
loggers |
Displays and modifies the loggers configuration in the application. |
Need |
liquibase |
Displays any Liquibase database migrations that have been applied. |
Need |
metrics |
Displays the "metric" information for the current application. |
Need |
mappings |
Displays all @RequestMapping the URL grooming lists. |
Need |
shutdown |
Close the app (not enabled by default). |
Need |
trace |
Displays trace information (the default last 100 HTTP requests). |
Need |
From the official website of the document can also be seen, through the configuration file, modify whether an endpoint is open, need authentication access, etc.
Such as:
# 这里的id 是指访问的url路径endpoints.beans.id=springbeans# 关闭鉴权endpoints.beans.sensitive=false# 开启shutdownendpoints.shutdown.enabled=true
As for the actual output of each of the above endpoint information, we can look at the big guy's articles or access to the end of the terminal /docs
, there are examples of common command output, here is not posted. Otherwise the article looks lengthy, haha ~
Command details: http://www.ityouknow.com/springboot/2018/02/06/spring-boot-actuator.html
Access /docs
endpoints (how to use the instructions below):
Get Started Practice
0. Join Pom Dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 加入doc文档 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator-docs</artifactId> </dependency> <!-- 开启安全认证 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
Join spring-boot-actuator-docs
to view related documents, such as
1. configuration file (not available), specific can be configured according to the actual business.
# actuator的访问路径management.context-path=/monitor# 管理的端口调整成1234management.port=1234# 有些需要身份认证才能访问,可直接关闭鉴权 #management.security.enabled=true# 开启关闭应用端点endpoints.shutdown.enabled=true# 安全验证的账号密码security.user.name=oKongsecurity.user.password=123456
2. Write the startup class (Normal Startup Class), after launch, Access: http://127.0.0.1:1234/monitor/(because we have re-specified the access port context for monitor)
Access /monitor/beans
, is required authorization, you can see the need to enter the user name and password:
Enter the user name and password configured in the configuration file, you can access it normally:
Custom Endpoints
Although it SpringBoot
already has many endpoints, it is sufficient for most cases. However, for some special needs, you still need to customize the endpoint to satisfy. The next step is to briefly explain the creation of a custom endpoint.
Customizing health Endpoints
Health information can be used to check the running status of your app. So it is often used by the monitoring software to remind the production system to stop, whether the database is normal, or whether Redis is started, and so on, and generally the health endpoint information is more sensitive, should join the identity authentication.
The Automatically configured health endpoints are:
The following is a brief description:
name |
Description |
CassandraHealthIndicator |
Check that the Cassandra database is started. |
DiskSpaceHealthIndicator |
Check for insufficient disk space. |
DataSourceHealthIndicator |
Check to see if the connection can be obtained DataSource . |
ElasticsearchHealthIndicator |
Check that the Elasticsearch cluster is started. |
InfluxDbHealthIndicator |
Check that the InfluxDB server is started. |
JmsHealthIndicator |
Check that the JMS agent is started. |
MailHealthIndicator |
Check that the mail server is started. |
MongoHealthIndicator |
Check that the Mongo database is started. |
Neo4jHealthIndicator |
Check that the Neo4j server is started. |
RabbitHealthIndicator |
Check that the Rabbit server is started. |
RedisHealthIndicator |
Check that the Redis server is started. |
SolrHealthIndicator |
Check to Solr see if the server is started. |
These endpoints, spring-boot-starter-xxx
after the package is dependent on the import, using @Conditional
such annotations to automatically load, you can see org.springframework.boot.actuate.autoconfigure
the package of the automatic loading class.
For example, in the, when we join the spring-boot-starter-data-redis
dependency, RedisHealthIndicator
it will automatically be loaded, this time we visit under: Http://127.0.0.1:1234/monitor/health, can see the redis
node has been shown, the state is closed
The others are similar, the specific can look at the source code. Next, we use inheritance to AbstractHealthIndicator
customize a monitoring endpoint (and, of course, to implement an HealthIndicator
interface).
CustomHealthIndicator.java
/** * 自定义健康端点 继承AbstractHealthIndicator类 也可以实现 HealthIndicator接口的 * @author oKong * *///这里也可以使用 类似@ConditionalOnMissingBean写法自动加载的//这里的name 就是默认健康节点的名称了@Component("oKong")public class CustomHealthIndicator extends AbstractHealthIndicator{ @Override protected void doHealthCheck(Builder builder) throws Exception { //设置健康信息 builder.withDetail("code", "0123") .withDetail("version", "v0.1") //有其他信息可继续添加的 .up().build(); }}
Final effect:
{ "status": "DOWN", "oKong": { "status": "UP", "code": "0123", "version": "v0.1" }, "diskSpace": { "status": "UP", "total": 120032587776, "free": 8374538240, "threshold": 10485760 }, "redis": { "status": "DOWN", "error": "org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool" }}
This is not a lot of use, as for other details, you can check the official website: Security with Healthindicators
Custom Endpoints
Out of the original built-in endpoint to add, access to the path is also a built-in path, we can also fully customize an endpoint to achieve our business needs.
First, let's take a look at 健康端点
the corresponding class org.springframework.boot.actuate.endpoint.HealthEndpoint
, which is inherited AbstractEndpoint<T>
to implement. Through the IDE we can also see that most of the endpoints are inherited from this abstract class to complete.
Therefore, we also implement custom endpoints by inheriting this class.
CustomEndPoint.java
/** * 自定义端点 * @author oKong * */@Component@ConfigurationProperties(prefix = "endpoints.oKong")public class CustomEndPoint extends AbstractEndpoint<Map<String,Object>>{ public CustomEndPoint() { //设置ID 即访问路径 :/oKong super("oKong"); } /** * 返回信息 */ @Override public Map<String, Object> invoke() { Map<String, Object> result = new HashMap<>(); result.put("author", "oKong"); result.put("chapter", "chapter27"); result.put("mp", "lqdevOps"); return result; }}
After joining @ConfigurationProperties
, you can set the settings in the configuration file, and so on. Of course, you can add custom parameters, which is the default, no additional parameters are added.
Resources
docs.spring.io/spring-boot/docs/1.5.15.release/reference/htmlsingle/#production-ready
Www.jianshu.com/p/af9738634a21
Summarize
This note focuses on some practices related to native endpoints and custom endpoints. For general information, the use of these native endpoints can be basically satisfied, but because the JSON string returned is not clear enough. The next section introduces the use of Spring Boot Admin
visual monitoring, such as chart information, view log output, and so on, through the interface to display.
At last
At present, many big guys on the internet have a SpringBoot
series of tutorials, if there is a similar, please forgive me. The original is not easy, the code word is not easy, but also hope that everyone support. If there is something wrong in the text, also hope to put forward, thank you.
Cliché
- Personal QQ:
499452441
- Public Number:
lqdevOps
Personal blog: http://blog.lqdev.cn
Complete Example: github.com/xie19900123/spring-boot-learning/tree/master/chapter-27
Original Address:/HTTP