Article Directory
- 1. built-in healthindicator monitoring and testing
- 2. Custom Healthindicator Monitoring detection
- 3. Source Code
The health information is collected from all the healthindicator beans in ApplicationContext, and Spring Boot contains some healthindicator.
built-in healthindicator monitoring detection
Name |
Description |
Cassandrahealthindicator |
Checks a Cassandra database is up. |
Diskspacehealthindicator |
Checks for low disk space. |
Datasourcehealthindicator |
Checks a connection to DataSource can is obtained. |
Elasticsearchhealthindicator |
Checks that a Elasticsearch cluster is up. |
Jmshealthindicator |
Checks. A JMS broker is up. |
Mailhealthindicator |
Checks. A mail server is up. |
Mongohealthindicator |
Checks a Mongo database is up. |
Rabbithealthindicator |
Checks a Rabbit server is up. |
Redishealthindicator |
Checks. A Redis server is up. |
Solrhealthindicator |
Checks that a SOLR server was up. |
Let's take a look at the source code list.
As can be seen, Spring Boot helps us integrate many of the more common health monitors, such as MySQL, MongoDB, Redis, ElasticSearch, SOLR, RabbitMQ, and more.
Custom Healthindicator Monitoring detection
In general, the health monitoring provided by Spring Boot does not meet our complex business scenarios, when we need to customize our own healthindicator and expand our business monitoring.
We implement the Healthindicator interface to create a simple detector class. Its role is simple, just for service status monitoring. At this point, the health check is implemented by overriding the Heath () method.
- @Component
- Public class cusstatushealthindicator implements healthindicator {
- @Override
- Public Health Health () {
- int ErrorCode = check();
- if (errorCode! = 0) {
- return to health . Down()
- . withdetail("status", errorCode)
- . withdetail("message", "service Failure")
- . build();
- }
- return to health . Up(). Build();
- }
- private int Check() {
- //detection operations on monitoring objects
- return httpstatus. Not_found. Value();
- }
- }
Let's take a look at the print results.
- {
- " status": "Down",
- "Cusstatus": {
- "Status": 404,
- "Message": "service failure"
- }
- }
In addition, we can also create a detector class by inheriting the Abstracthealthindicator class.
- @Component
- Public class cusdiskspacehealthindicator extends abstracthealthindicator {
- private final filestore filestore;
- private final long thresholdbytes;
- @Autowired
- public cusdiskspacehealthindicator(
- @Value("${health.filestore.path:/}") String path,
- @Value("${health.filestore.threshold.bytes:10485760}") long thresholdbytes )
- throws IOException {
- Filestore = Files. Getfilestore(Paths. Get(path));
- this. Thresholdbytes = thresholdbytes;
- }
- @Override
- protected void dohealthcheck(health. Builder Builder) throws Exception {
- long diskfreeinbytes = filestore. Getunallocatedspace();
- if (diskfreeinbytes >= thresholdbytes) {
- Builder. Up();
- } else {
- Builder. Down();
- }
- long totalspaceinbytes = filestore. Gettotalspace();
- Builder. Withdetail("Disk.free", diskfreeinbytes);
- Builder. Withdetail("Disk.total", totalspaceinbytes);
- }
- }
The Abstracthealthindicator implements the Healthindicator interface and overrides the health () method to achieve a healthy check. Therefore, we just need to rewrite the Dohealthcheck method.
In general, we do not implement the Healthindicator interface directly, but instead inherit the Abstracthealthindicator abstract class. Because, we only need to rewrite the Dohealthcheck method, and in this method we focus on the specific health detection of the business logic service.
Let's take a look at the print results.
- {
- " status": "Up",
- "Cusdiskspace": {
- " status": "Up",
- "Disk.free": 79479193600,
- "Disk.total": 104856547328
- }
- }
Source Code
Related example complete code: springboot-action
Finish
If you feel that my article is helpful to you, please feel free to make a reward.
- Copyright NOTICE: This article was published by Liang in Liang Blog
- Reprint statement: Free reprint-Non-commercial-non-derivative-maintain attribution (Creative Sharing 3.0 license), non-commercial reprint please indicate the author and source, commercial reprint please contact the author himself.
- Article title: Spring Boot Secrets and Combat (ix) Application monitoring-HTTP health monitoring
- Article Link: http://blog.720ui.com/2017/springboot_09_actuator_http_healthindicator/
Spring Boot Uncover and Combat (ix) Application monitoring-HTTP health monitoring