Home > front end >  Is it possible to get a list of service that dynamically get down on my application that is on sprin
Is it possible to get a list of service that dynamically get down on my application that is on sprin

Time:03-25

My application contains rabbitmq,hazelcast,dynamodb,elastic I want build a mechanism that can tell me whenever any of my application get down I should be notified on an email.Can we include any service which i can track to get status of all application that are integrated in my spring boot application

I tried using try catch block but line of code got increased and it made my code very cumbersome as it difficult for me to add try catch at every method

CodePudding user response:

Spring Boot actuator provides a lot of useful endpoints, one of which is the health endpoint. The health endpoint returns the health status of your application based on its dependencies (databases, third party APIs, ...).

There are already builtin health indicators for RabbitMQ, Hazelcast and Elastic. There is no builtin health indicator for DynamoDB as far as I know, but you can also write your own health indicator, as seen in this question.

Now, to send you an email there are a two options:

  1. You can use an external program (eg. monitoring software) to regularly check the health actuator
  2. You can write it as part of your Spring boot application

Using an external program

If you use an external program, you can make it consume the /actuator/health endpoint. To enable this endpoint, configure the following property:

management.endpoints.web.exposure.include=health

By default this endpoint only exposes a single aggregated status of all health indicators combined. If you want to individually see which service is down, you can show more details by setting the following property to always or when_authorized:

management.endpoint.health.show-details=always | when_authorized

The response will look something like this:

{
  "status": "DOWN",
  "rabbit": {
    "status": "DOWN",
    "error": "org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)"
  }
}

Writing your own

You can write a scheduled task by using the @Scheduled annotation to regularly check the health status. To obtain the health status, you can autowire HealthEndpoint as seen in this question.

Then you can send an email with Spring.

@Component
public class HealthEmailSender {
    private final HealthEndpoint healthEndpoint;
    private final JavaMailSender mailSender;

    // TODO: Implement constructor to autowire healthEndpoint   mailSender

    @Scheduled(fixedRate = 10 * 60 * 1000) // Check once every 10 minutes
    public void sendEmailWhenBadHealth() {
        if (isHealthDown()) {
            sendMail();
        }
    }
    
    private boolean isHealthDown() {
        return Status.DOWN.equals(healthEndpoint.health().getStatus());
    }

    private void sendMail() {
        MimeMessage message = mailsender.createMimeMessage();
        // TODO: Set from / to / title / content
        mailSender.send(message);
    }
}

This code snippet above would send an e-mail as soon as any health indicator goes down (not only from the services you mentioned).

To obtain the health status of one of the services you're interested in, you can use healthEndpoint.healthForPath("rabbit").getStatus().

  • Related