BAEL-6179: Add Actuator sample without Spring Boot (#14032)

* BAEL-6179: Add Actuator sample without Spring Boot

* BAEL-6179: Simplify custom health indicator
This commit is contained in:
Ralf Ueberfuhr
2023-05-18 14:01:50 +02:00
committed by GitHub
parent 9bb795bfd3
commit 68324c8f6b
9 changed files with 253 additions and 0 deletions
@@ -0,0 +1,26 @@
package com.baeldung.spring.actuator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ActuatorConfiguration {
@Bean
public HealthIndicator sampleHealthIndicator() {
return Health.up()
.withDetail("info", "Sample Health")
::build;
}
@Bean
public InfoContributor provideSampleInfos() {
return builder ->
builder
.withDetail("app-title", "Actuator Sample Application");
}
}
@@ -0,0 +1,11 @@
package com.baeldung.spring.actuator;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
@EnableAutoConfiguration
@PropertySource("classpath:application.properties")
@ComponentScan(basePackageClasses = AppConfig.class)
public class AppConfig {
}
@@ -0,0 +1,64 @@
package com.baeldung.spring.actuator;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class Start {
private static final Logger logger = LoggerFactory.getLogger(Start.class);
private static final int DEFAULT_PORT = 8080;
private static final String CONTEXT_PATH = "/";
private static final String CONFIG_LOCATION = AppConfig.class.getName();
private static final String MAPPING_URL = "/*";
/*
* This application runs a Jetty webcontainer that runs the
* Spring Dispatcher Servlet.
*/
public static void main(String[] args) throws Exception {
new Start().startJetty(getPortFromArgs(args));
}
private static int getPortFromArgs(String[] args) {
if (args.length > 0) {
try {
return Integer.parseInt(args[0]);
} catch (NumberFormatException ignore) {
}
}
logger.info("No server port configured, falling back to {}", DEFAULT_PORT);
return DEFAULT_PORT;
}
private void startJetty(int port) throws Exception {
logger.info("Starting server at port {}", port);
Server server = new Server(port);
server.setHandler(getServletContextHandler(getContext()));
server.start();
logger.info("Server started at port {}", port);
server.join();
}
private static ServletContextHandler getServletContextHandler(WebApplicationContext context) {
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setErrorHandler(null);
contextHandler.setContextPath(CONTEXT_PATH);
contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), MAPPING_URL);
contextHandler.addEventListener(new ContextLoaderListener(context));
return contextHandler;
}
private static WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation(CONFIG_LOCATION);
return context;
}
}
@@ -0,0 +1,23 @@
package com.baeldung.spring.actuator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfiguration {
@Bean
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry
.addViewController("/")
.setViewName("redirect:/actuator");
}
};
}
}
@@ -0,0 +1,2 @@
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always