Wednesday, April 29, 2020

Spring Boot Actuator

Spring boot Actuator provides many feature to monitor and manage your Spring boot application when deploying to production.There is following two way to monitor your application as below.

Definition of Actuator: Actuator is a manufacturing term that is monitoring our application, generating loggers, showing application health, showing database status etc.

Enabling Production ready feature: To Enable spring boot actuator we have to add maven dependency "spring-boot-starter-actuator"  as shown below.

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>

Note: I have shown maven example in my blog. you can use gradle as well.

Endpoints: Using actuator endpoints we can monitor and interact with our application in efficient way. Spring boot provides a number of built in endpoints, we can use that endpoints. We can enable and disable the endpoints accordingly.

Some important endpoints for spring actuator is as shown below:


EndpointDescription
/health
To Show the application health.
/beans
Shows complete spring beans information. 
/loggers
Shows configuration of loggers.
/env
Shows list of environment variable of the application. 
/info
Shows spring application information.
/mactrics
Shows matcics of the current spring boot application. Ex. free memory, used memory etc. 
/mapping
Shows list of @RequestMapping  paths.
We can enable and disable individual endpoints as well.

Syntax to enable endpoint: management.endpoint.<endpoint name>.enable = true
Syntax to disable endpoint: management.endpoint.<endpoint name>.enable = false

We can disable all endtpoints by default as well.
Syntax to disable all endpoints: management.endpoints.enabled-by-default = false

if you are using the YML file then use the below format to provide the configuration.
management: endpoints: web: exposure: include: "*"

We can Expose the endponts using following two ways:
  1. Using HTTP Endpoints
  2. Using JMX
we can use exclude and include properties to expose the endpoints.


Property NameDefault value
management.endpoints.jmx.exposure.exclude
*
management.endpoints.jmx.exposure.include
*
management.endpoints.web.exposure.exclude
*
management.endpoints.we.exposure.include
info, health
Note: The include property exposed the endpoints that we will use. and exclude property will prevent to expose the endpoints.
    HTTP Endpoints: HTTP endpoint include web endpoint. Http tracing provides the information about request and response exchange through web application. Using HTTP tracing, we can only access "/actuator/health" and "/actuator/info" over the HTTP protocol for the security reason.

    JMX: Using JMX we can access other endpoints such as "/actuator/beans", "/actuator/auditevents" etc. JMX(Java Monitoring and Management Console) can be found under JDK_HOME/bin directory. To Start Jconsole use the below step.
    • Open command prompt.






    • Navigate to the JDK_HOME/bin directory.
    • Execute JConsole.exe. 
    • Once you will type the JConsole.exe and press Enter then below popup will open.
    • Select Local process and select you application main class as below and press connect. Before executing above command make sure your application is up and running.
    • After pressing connect button if you are facing some issue as below then proceed with insecure connection.
    • After successful connection you will see the below popup.


    • After connecting it will open a popup with MyBean property in left panel in tree view as below.



    • By exploring the endpoints you will get details of all endpoints that you wish. for example, take an example for beans. So click on Beans => Select operation => beans. Once you will click on beans you will find java.util.Map along with the bean on the top of the operation invocation window as below.

      • By clicking on beans button you will get operation value as below.
      • you can see the operation value by pressing CTRL+A then copy all text by CTRL+C and then copy to the notepad++ where you can see all the beans operation. 

      Demo with spring boot application.

      To create Spring boot application using actuator follow the below steps.
      • Create spring boot application and import into you IDE. To create application got to https://start.spring.io/ and complete the below information.
      • Import the application into IDE.
      • Now open YML file and add the management configuration as below.
      • Run application and open browser and hit the below URL.
      http://localhost:8080/actuator/health

      • You will get the complete health with details as below.
      You can try with the different different endpoints like:

      You can also see the below output in postman.




      Thanks 
      Avinash Kumar Tiwari

      No comments:

      Post a Comment