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:
parent
9bb795bfd3
commit
68324c8f6b
|
@ -0,0 +1,3 @@
|
|||
## Parent Spring 5
|
||||
|
||||
This is a parent module for all projects using Spring 5
|
|
@ -0,0 +1,41 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>parent-spring-6</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>parent-spring-6</name>
|
||||
<packaging>pom</packaging>
|
||||
<description>Parent for all spring 6 core modules</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-framework-bom</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring.version>6.0.8</spring.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
8
pom.xml
8
pom.xml
|
@ -331,6 +331,7 @@
|
|||
<module>parent-boot-2</module>
|
||||
<module>parent-spring-4</module>
|
||||
<module>parent-spring-5</module>
|
||||
<module>parent-spring-6</module>
|
||||
<module>parent-java</module>
|
||||
|
||||
<module>checker-framework</module>
|
||||
|
@ -408,6 +409,7 @@
|
|||
<module>parent-boot-2</module>
|
||||
<module>parent-spring-4</module>
|
||||
<module>parent-spring-5</module>
|
||||
<module>parent-spring-6</module>
|
||||
<module>parent-java</module>
|
||||
|
||||
<module>spring-4</module>
|
||||
|
@ -468,6 +470,7 @@
|
|||
<module>parent-boot-2</module>
|
||||
<module>parent-spring-4</module>
|
||||
<module>parent-spring-5</module>
|
||||
<module>parent-spring-6</module>
|
||||
<module>parent-java</module>
|
||||
|
||||
<module>apache-spark</module>
|
||||
|
@ -509,6 +512,7 @@
|
|||
<module>parent-boot-2</module>
|
||||
<module>parent-spring-4</module>
|
||||
<module>parent-spring-5</module>
|
||||
<module>parent-spring-6</module>
|
||||
<module>parent-java</module>
|
||||
|
||||
<module>checker-framework</module>
|
||||
|
@ -577,6 +581,7 @@
|
|||
<module>parent-boot-2</module>
|
||||
<module>parent-spring-4</module>
|
||||
<module>parent-spring-5</module>
|
||||
<module>parent-spring-6</module>
|
||||
<module>parent-java</module>
|
||||
|
||||
<module>spring-4</module>
|
||||
|
@ -629,6 +634,7 @@
|
|||
<module>parent-boot-2</module>
|
||||
<module>parent-spring-4</module>
|
||||
<module>parent-spring-5</module>
|
||||
<module>parent-spring-6</module>
|
||||
<module>parent-java</module>
|
||||
|
||||
<module>apache-spark</module>
|
||||
|
@ -896,6 +902,7 @@
|
|||
<module>spring-5-webflux</module>
|
||||
<module>spring-5-webflux-2</module>
|
||||
<module>spring-activiti</module>
|
||||
<module>spring-actuator</module>
|
||||
<module>spring-core-2</module>
|
||||
<module>spring-core-3</module>
|
||||
<module>spring-credhub</module>
|
||||
|
@ -1201,6 +1208,7 @@
|
|||
<module>parent-boot-2</module>
|
||||
<module>parent-spring-4</module>
|
||||
<module>parent-spring-5</module>
|
||||
<module>parent-spring-6</module>
|
||||
<module>parent-java</module>
|
||||
</modules>
|
||||
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-actuator</artifactId>
|
||||
<name>spring-actuator</name>
|
||||
<packaging>war</packaging>
|
||||
<description>Demo project for Spring Boot Actuator without Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-6</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-spring-6</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
<version>5.0.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
<!-- Jetty embedded -->
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-servlet</artifactId>
|
||||
<version>${jetty.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>3.3.2</version>
|
||||
</plugin>
|
||||
<!-- to run the server, use mvn jetty:run -->
|
||||
<plugin>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-maven-plugin</artifactId>
|
||||
<version>${jetty.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<spring-boot.version>3.0.6</spring-boot.version>
|
||||
<jetty.version>11.0.15</jetty.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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
|
Loading…
Reference in New Issue