Top Spring Boot Actuator Interview Questions (2021) | CodeUsingJava






















Spring Boot Actuator Interview Questions

Most frequently asked Spring Boot Actuator Interview Questions


  1. What is the role of Spring Boot Actuator?
  2. How to configure Actuator in Spring Boot?
  3. How can we configure Actuators with Spring MVC?
  4. Name some of the useful Actuators Endpoints?
  5. How can we enable only a specific endpoint in Spring Boot Actuators?
  6. How can the Actuator Endpoints be secured with the role?
  7. How to add the prefix to all the actuators endpoints?
  8. How can we filter the metrices in the actuators ?
  9. How to configure Prometheus in Spring Boot?
  10. Is it possible to rename the endpoints i.e env to myenv? If yes, how?
  11. What is Spring Boot Actuator?
  12. How can we monitor Spring Boot application using Actuator?
  13. What are the Spring Boot key components?
  14. How does a spring boot application get started?
  15. What are Starter Dependencies?
  16. Explain the process of managing and monitoring in Spring Boot Actuator?
  17. What are different ways of running Spring Boot Application?
  18. What are the Spring Boot Annotations?
  19. What is the process of Datasources?
  20. What embedded servers does Spring Boot support?
  21. How to disable specific auto-configuration?
  22. How to enable all endpoints in actuator?
  23. How to add a custom health check in spring boot health?
  24. How can we config reload with Spring Boot?
  25. How can we enable logging in spring boot actuator health check API?
  26. How to configure Spring Boot logback RollingFileAppender and have actuator logfile?


What is the role of Spring Boot Actuator??

Spring Boot Actuator functionality which would help in monitoring the application with almost zero-configuration or code.It makes the application a production-grade by providing several key factors. For example, the health endpoint is exposed as /health.

How to configure Actuator in Spring Boot?

To configure the Actuator in Spring boot Application, we need to add only a single dependency in the pom.xml file and the by-default endpoints will be enabled in our application.
	            <dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-actuator</artifactId>
	                 </dependency> 

How can we configure Actuators with Spring MVC?

The actuators can be configured in the Spring MVC in the following manner-
Add the following dependencies in the pom.xml file:
  
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-actuator</artifactId>
    <version>1.3.5.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.5.RELEASE</version>
</dependency>
Now we update the configuration file in the following manner-
   
    @Configuration
@EnableWebMvc
@Import({
        EndpointAutoConfiguration.class , PublicMetricsAutoConfiguration.class , HealthIndicatorAutoConfiguration.class
})
public class MyActuatorConfig {

    @Bean
    @Autowired
    public EndpointHandlerMapping endpointHandlerMapping(Collection<? extends MvcEndpoint> endpoints) {
        return new EndpointHandlerMapping(endpoints);
    }

    @Bean
    @Autowired
    public EndpointMvcAdapter metricsEndPoint(MetricsEndpoint delegate) {
        return new EndpointMvcAdapter(delegate);
    }
}
 


Name some of the useful Actuators Endpoints?

Some of the useful endpoints available in Spring Boot are as follows-
  • Logger helps in accessing the log level.
  • Health endpoint helps in providing the basic health information about the application. It is active by default.
  • Beans This endpoint will return all the configured beans in our application.
  • env This endpoint provides information about the environment properties.
  • info This endpoint is used for displaying the application information. It is active by default
  • mappings This endpoint is used for displaying all the @RequestMapping paths.


How can we enable only a specific endpoint in Spring Boot Actuators?

It is possible to enable only some of the specific endpoints in Spring Boot Actuators by doing the following configurations:
management.endpoints.enabled-by-default = false
management.endpoint.health.enabled = true
management.endpoints.web.exposure.include = health

Now only the /health endpoint will be enabled.

How can the Actuator Endpoints be secured with the role?

    @Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
       http
            .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
            .authorizeRequests()
                .antMatchers("/ajax/**").authenticated()           
                .antMatchers("/actuator/**").hasRole("ADMIN")  
                .anyRequest().authenticated()  
                .and()
            .csrf()
                .disable();
    }
}

How to add the prefix to all the actuators endpoints?

    management.endpoints.web.base-path=/secure
 
Now we can access all the endpoints as follows-
    /env ->     /secure/env
/health ->  /secure/health
/info ->    /secure/info


How can we filter the metrices in the actuators ?

There are several default metrices available inside the /actuators/metrices endpoints.But it is possible that only the specific gets displayed.
The property management.metrics.enable is used for the purpose. For example, If the user wants to disable all jvm properties, then the command can be written as: management.metrics.enable.jvm=false
If there are a number of properties for the same metrices, the one which is more specific will be applied.
management.metrics.enable.jvm=false
management.metrics.enable.jvm.memory.max=true


How to configure Prometheus in Spring Boot

to configure Prometheus in Spring Boot, we need to add the following deendency:
 
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
 
After adding this dependency, the PrometheusMeterRegistry and the CollectorRegistry are configured in the Spring boot application. They export the metrices data to the Prometheus server in a format so that it can be easily scrapped.

Is it possible to rename the endpoints i.e env to myenv? If yes, how?

Yes, it is possible to rename the endpoints by the following property:
    management.endpoints.web.path-mapping.env=myenv


What is Spring Boot Actuator?

Spring Boot Actuator is used for containing the actuator endpoints, we can use HTTP and JMX endpoints for managing and monitoring the spring boot application.It is mainly used for exposing operational information about the running application such as health, metrics, info, dump, env, etc.It uses HTTP endpoints or JMX beans to enable us to interact with it. If this dependency is on the classpath all endpoints will available for us out of the box.

How can we monitor Spring Boot application using Actuator?


Spring Boot Actuator


What are the Spring Boot key components?

Key components of spring-boot are:
  • Spring Boot auto-configuration
  • Spring Boot CLI
  • Spring Boot starter POMs
  • Spring Boot Actuators

How does a spring boot application get started?

@SpringBootApplication
public class MyApplication {
   
       public static void main(String[] args) {    
   
             SpringApplication.run(MyApplication.class);        
               // other statements    
       }
}


What are Starter Dependencies?

Spring Boot Starter is used as a collection of all the relevant transitive dependencies which are needed to start a particular functionality.
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>


Explain the process of managing and monitoring in Spring Boot Actuator?


Spring Boot Actuator


What are different ways of running Spring Boot Application?

Different ways of running Spring Boot Application are as follows:
  • Running from an IDE
  • Running as a Packaged Application
  • Using the Maven Plugin
  • Using External Tomcat
  • Using the Gradle Plugin


What are the Spring Boot Annotations?

  • @Bean used at the method level and indicating the method produces a bean that is to be managed by Spring container.
  • @Service used for showing annotated class is a service class.
  • @Repository used for accessing database directly.
  • @Configuration used as a source of bean definitions.
  • @Controller used to indicate that the class is a web request handler.
  • @RequestMapping used with the class as well as the method.
  • @Autowired used to auto-wire spring bean on setter methods, constructor and instance variable.
  • @Component used for turning the class into Spring bean at the auto-scan time.
  • @SpringBootApplication consists of @Configuration, @ComponentScan, and @EnabeAutoConfiguration.
  • @ComponetScan used to scan a package of beans.
  • @Required is applied to bean setter methods.
  • @Qualifier is used along with @Autowired annotation.
  • @CookieValue is used at the method parameter level as an argument of the request mapping method.
  • @Lazy is used in the component class.
  • @EnableAutoConfiguration is placed on the main application class.


What is the process of Datasources?


Spring Boot Actuator


What embedded servers does Spring Boot support?

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>


How to disable specific auto-configuration?

@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
public class SampleApplication
{
}


How to enable all endpoints in actuator?

management.endpoints.web.expose=*
# if you'd like to expose shutdown:
# management.endpoint.shutdown.enabled=true

management.endpoints.web.exposure.include=*
# if you'd like to expose shutdown:
# management.endpoint.shutdown.enabled=true


How can we add a custom health check in spring boot health?

@Component
public class CustomHealthCheck extends AbstractHealthIndicator {
    @Override
    protected void doHealthCheck(Health.Check bldr) throws Exception {
        // TODO implement some check
        boolean running = true;
        if (running) {
          bldr.up();
        } else {
          bldr.down();
        }
    }
}


How can we config reload with Spring Boot?


Spring Boot Actuator


How can we enable logging in spring boot actuator health check API?

@Component
@EndpointWebExtension(endpoint = HealthEndpoint.class)
public class HealthEndpointWebExtension {

    private HealthEndpoint healthEndpoint;
    private HealthStatusHttpMapper statusHttpMapper;

    // Constructor

    @ReadOperation
    public WebEndpointResponse<Health> health() {
        Health health = this.healthEndpoint.health();
        Integer status = this.statusHttpMapper.mapStatus(health.getStatus());
        // log in depending on health check
        return new WebEndpointResponse<>(health, status);
    }
}


How to configure Spring Boot logback RollingFileAppender and have actuator logfile?

<file>.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} - %msg%n
        </Pattern>
    </encoder>