Spring Boot – Teknonauts https://teknonauts.com Stay Tuned With Technology Sun, 18 Apr 2021 09:56:24 +0000 en-US hourly 1 https://wordpress.org/?v=5.7.5 https://teknonauts.com/wp-content/uploads/2021/01/cropped-teknonauts_favicon_original-1.png Spring Boot – Teknonauts https://teknonauts.com 32 32 #4 Spring Boot Actuator – Helpful hand in monitoring https://teknonauts.com/spring-boot-actuator/ https://teknonauts.com/spring-boot-actuator/#respond Tue, 30 Mar 2021 19:19:22 +0000 https://teknonauts.com/?p=2217

Spring Boot actuator helps us to manage and monitor our application when we deploy our application to production. It contains various endpoints by which we can see where the resource is up, it has two types of endpoints which we can use to monitor our spring boot application which are JMX and HTTP, it provides us the production-ready application by which we can monitor the health of our application we should use this feature. It also applied some more things automatically in applications like metrics, health, and auditing.

Features of Spring Boot Actuator

The spring-boot-actuator module provides all of Spring Boot’s production-ready features. The simplest way to enable the features is to add a dependency to the spring-boot-starter-actuator ‘Starter’.

1. Provide Production Ready Feature

We have one module which provides us all the production ready features. To enable this feature into the project we need to add one dependency i.e. spring-boot-starter-actuator ‘Starter’.

2. Auditing

We all have spring security in our project for authentication so this actuator has audit framework as well which manage various events by default like access denied, failures and authentication success. So this feature is very important and it implements lockout for user based on a number of failures in the authentication. Now to enable this we need to configure AuditEventRepository type bean in our spring boot application. Spring boot also provides one InMemoryAuditEventRepository but this has very limited features so this can only recommend using in a development phase, if we want to move our application to production then we should go for AuditEventRepository configuration.

3. Customer Auditing

we can also implement customize security events but fir this we need to provide implementations of the interface which are:

AbstractAuthenticationAuditListener

AbstractAuthorizationAuditListener

4. Endpoints

Spring boot actuator provides us various built-in endpoints to manage and monitor our application and by this, we can also interact with our application. It has so many built-in endpoints as well as we can create our endpoint also. We can also disable and enable this endpoint according to the need. Suppose we want to access these endpoints so in order to access them we need to expose them on to the network via HTTP or JMX. Here application prefers HTTP over JMX URL starts with / actuator and after this ID we can add to access them.

5. Loggers

One main reason for this feature is it allows us to configure our loggers in our application at runtime. We can also view the individual logger or the entire list configuration. This configuration are made both implicitly and explicitly. Implicitly are once which are provided by the login framework and explicitly which we have done. Various level mentioned below:

  • null
  • WARN
  • TRACE
  • INFO
  • ERROR
  • OFF
  • FATAL
  • DEBUG
spring boot actuator

6. Metrics

As discussed actuator provide us auto-configuration and management for micrometer, it is a facade which provides support for various monitoring purpose that includes:

  • Datadog
  • Prometheus
  • Elastic
  • Atlas
  • Ganglia
  • StatsD
  • Graphite
  • AppOptics
  • Humio
  • Influx
  • Simple (in-memory)
  • JMX
  • Dynatrace
  • KairosDB
  • New Relic
  • SignalFx
  • Wavefront

7. JMX

JMX stands for Java Management Extensions, it allows us to manage and monitor our spring boot applications. To enable this feature we need to do configuration in an out project like set the configuration property spring.jmx.enabled = true. If we want to disable this property or we do not want to expose our endpoints using JMX then we need to configure like below:

management.endpoints.jmx.exposure.exclude

8. HTTP

This feature is used to expose our endpoints over the network via HTTP this is enabled by default. We just need to prefix /actuator followed by the ID of the endpoint. It can happen sometimes that we have already used / actuator for other purposes so we can also change the prefix by setting some property which shows below:

management.endpoints.web.base-path=/manage

9. Process Monitoring

Spring boot provides us two classes that are very useful for process monitoring which are as follows :

  • WebServerPortFileWriter: This class used to create a file which contains information about the server port on which the application is up and running. By default, it creates with name application.port
  • ApplicationPidFileWriter: This class creates a file that conation the application PDI. By default, it creates with name application.pid

10. Cloud Foundry Support

Spring boot actuator also provides us support when we deploy our application on the cloud. It provides us /cloudfoundryapplication path which provides us a secure route to all our endpoints.

11. HTTP Tracing

This we can enable by configuring HttpTraceRepository type bean into our application. Spring boot provides us InMemoryHttpTraceRepository but it has limited support which can be used in development. For the production-ready application, we can go for HttpTraceRepository.

Spring Boot Actuator endpoints

We have so many inbuilt endpoints available in spring boot actuator.

  • auditevents
  • beans
  • caches
  • conditions
  • configprops
  • env
  • flyway
  • health
  • httptrace
  • info
  • integrationgraph
  • loggers
  • liquibase
  • metrics
  • mappings
  • scheduledtasks
  • sessions
  • shutdown
  • threaddump

Spring Boot Actuator Properties:

  • management.security.enabled=true
  • management.security.roles=USER
  • security.basic.enabled=true
  • security.user.name=user123
  • security.user.passowrd=user123

Examples of Spring Boot Actuator

Follow the below mentioned steps to create your first spring boot project using an actuator.

  • First, we need to create the spring boot project for that you can follow this link https://start.spring.io/
  • Here mention the group names, artifact id and add the necessary dependency. The dependency for the actuator is mentioned below.
  • Add this to pom.xml

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

  • Java Class:

package com.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoActuator
{
@GetMapping("/demo_act")
public String demoAc()
{
return "Demo actuator!!";
}
}

Output:

  • For now, we are disabling the security feature of a spring boot actuator. For this we will set one property which is mentioned below:

management.security.enabled=false

Conclusion

So actuator provides us many features to make our spring boot application production ready which helps us to monitor and manage our application very easily also it reduces the maintenance task for us. We can also trace the exception and errors in production very easily.

Refer below link to know more about Actuators:

https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html

Explore more in teknonauts.

]]>
https://teknonauts.com/spring-boot-actuator/feed/ 0