Spring Boot Uncover and Combat (ix) Application monitoring-HTTP health monitoring

Source: Internet
Author: User
Tags solr redis server

Article Directory

    1. 1. built-in healthindicator monitoring and testing
    2. 2. Custom Healthindicator Monitoring detection
    3. 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.

  1. @Component
  2. Public class cusstatushealthindicator implements healthindicator {
  3. @Override
  4. Public Health Health () {
  5. int ErrorCode = check();
  6. if (errorCode! = 0) {
  7. return to health . Down()
  8. . withdetail("status", errorCode)
  9. . withdetail("message", "service Failure")
  10. . build();
  11. }
  12. return to health . Up(). Build();
  13. }
  14. private int Check() {
  15. //detection operations on monitoring objects
  16. return httpstatus. Not_found. Value();
  17. }
  18. }

Let's take a look at the print results.

  1. {
  2. " status": "Down",
  3. "Cusstatus": {
  4. "Status": 404,
  5. "Message": "service failure"
  6. }
  7. }

In addition, we can also create a detector class by inheriting the Abstracthealthindicator class.

  1. @Component
  2. Public class cusdiskspacehealthindicator extends abstracthealthindicator {
  3. private final filestore filestore;
  4. private final long thresholdbytes;
  5. @Autowired
  6. public cusdiskspacehealthindicator(
  7. @Value("${health.filestore.path:/}") String path,
  8. @Value("${health.filestore.threshold.bytes:10485760}") long thresholdbytes )
  9. throws IOException {
  10. Filestore = Files. Getfilestore(Paths. Get(path));
  11. this. Thresholdbytes = thresholdbytes;
  12. }
  13. @Override
  14. protected void dohealthcheck(health. Builder Builder) throws Exception {
  15. long diskfreeinbytes = filestore. Getunallocatedspace();
  16. if (diskfreeinbytes >= thresholdbytes) {
  17. Builder. Up();
  18. } else {
  19. Builder. Down();
  20. }
  21. long totalspaceinbytes = filestore. Gettotalspace();
  22. Builder. Withdetail("Disk.free", diskfreeinbytes);
  23. Builder. Withdetail("Disk.total", totalspaceinbytes);
  24. }
  25. }

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.

  1. {
  2. " status": "Up",
  3. "Cusdiskspace": {
  4. " status": "Up",
  5. "Disk.free": 79479193600,
  6. "Disk.total": 104856547328
  7. }
  8. }
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

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.