Skip to content

Commit f59f7b2

Browse files
BAEL-1430: Add Spring Boot 2 actuator module
1 parent 0ef4a4a commit f59f7b2

11 files changed

Lines changed: 356 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
<module>spring-batch</module>
151151
<module>spring-bom</module>
152152
<module>spring-boot</module>
153+
<module>spring-boot-2-actuator</module>
153154
<module>spring-boot-keycloak</module>
154155
<module>spring-boot-bootstrap</module>
155156
<module>spring-cloud-data-flow</module>

‎spring-boot-2-actuator/.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/target/
2+
.settings/
3+
.classpath
4+
.project

‎spring-boot-2-actuator/README.MD‎

Whitespace-only changes.

‎spring-boot-2-actuator/pom.xml‎

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-boot-actuator</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<name>spring-boot</name>
9+
<description>This is simple boot application for Spring boot actuator test</description>
10+
11+
<!-- Inherit defaults from Spring Boot -->
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>2.0.0.M7</version>
16+
<relativePath /> <!-- lookup parent from repository -->
17+
</parent>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-actuator</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-security</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-webflux</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>io.projectreactor</groupId>
40+
<artifactId>reactor-test</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.security</groupId>
45+
<artifactId>spring-security-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<finalName>spring-boot-2-actuator</finalName>
52+
<resources>
53+
<resource>
54+
<directory>src/main/resources</directory>
55+
<filtering>true</filtering>
56+
</resource>
57+
</resources>
58+
59+
<plugins>
60+
61+
<plugin>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-maven-plugin</artifactId>
64+
</plugin>
65+
66+
<plugin>
67+
<groupId>org.apache.maven.plugins</groupId>
68+
<artifactId>maven-compiler-plugin</artifactId>
69+
<version>3.7.0</version>
70+
<configuration>
71+
<source>1.8</source>
72+
<target>1.8</target>
73+
</configuration>
74+
</plugin>
75+
76+
<plugin>
77+
<groupId>org.apache.maven.plugins</groupId>
78+
<artifactId>maven-surefire-plugin</artifactId>
79+
<configuration>
80+
<excludes>
81+
<exclude>**/*IntegrationTest.java</exclude>
82+
</excludes>
83+
</configuration>
84+
</plugin>
85+
86+
</plugins>
87+
88+
</build>
89+
90+
<profiles>
91+
<profile>
92+
<id>integration</id>
93+
<build>
94+
<plugins>
95+
<plugin>
96+
<groupId>org.apache.maven.plugins</groupId>
97+
<artifactId>maven-surefire-plugin</artifactId>
98+
<executions>
99+
<execution>
100+
<phase>integration-test</phase>
101+
<goals>
102+
<goal>test</goal>
103+
</goals>
104+
<configuration>
105+
<includes>
106+
<include>**/*IntegrationTest.java</include>
107+
</includes>
108+
</configuration>
109+
</execution>
110+
</executions>
111+
<configuration>
112+
<systemPropertyVariables>
113+
<test.mime>json</test.mime>
114+
</systemPropertyVariables>
115+
</configuration>
116+
</plugin>
117+
</plugins>
118+
</build>
119+
</profile>
120+
</profiles>
121+
122+
<repositories>
123+
<repository>
124+
<id>spring-snapshots</id>
125+
<name>Spring Snapshots</name>
126+
<url>https://repo.spring.io/snapshot</url>
127+
<snapshots>
128+
<enabled>true</enabled>
129+
</snapshots>
130+
</repository>
131+
<repository>
132+
<id>spring-milestones</id>
133+
<name>Spring Milestones</name>
134+
<url>https://repo.spring.io/milestone</url>
135+
<snapshots>
136+
<enabled>false</enabled>
137+
</snapshots>
138+
</repository>
139+
</repositories>
140+
141+
<pluginRepositories>
142+
<pluginRepository>
143+
<id>spring-snapshots</id>
144+
<name>Spring Snapshots</name>
145+
<url>https://repo.spring.io/snapshot</url>
146+
<snapshots>
147+
<enabled>true</enabled>
148+
</snapshots>
149+
</pluginRepository>
150+
<pluginRepository>
151+
<id>spring-milestones</id>
152+
<name>Spring Milestones</name>
153+
<url>https://repo.spring.io/milestone</url>
154+
<snapshots>
155+
<enabled>false</enabled>
156+
</snapshots>
157+
</pluginRepository>
158+
</pluginRepositories>
159+
160+
<properties>
161+
<start-class>org.baeldung.ActuatorApplication</start-class>
162+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
163+
<java.version>1.8</java.version>
164+
</properties>
165+
166+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ActuatorApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ActuatorApplication.class, args);
11+
}
12+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung;
2+
3+
import org.springframework.boot.actuate.health.Health;
4+
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
5+
import org.springframework.stereotype.Component;
6+
import reactor.core.publisher.Mono;
7+
8+
@Component
9+
public class DownstreamServiceReactiveHealthIndicator implements ReactiveHealthIndicator {
10+
11+
@Override
12+
public Mono<Health> health() {
13+
return checkDownstreamServiceHealth().onErrorResume(
14+
ex -> Mono.just(new Health.Builder().down(ex).build())
15+
);
16+
}
17+
18+
private Mono<Health> checkDownstreamServiceHealth() {
19+
// we could use WebClient to check health reactively
20+
return Mono.just(new Health.Builder().up().build());
21+
}
22+
23+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.baeldung;
2+
3+
import org.springframework.boot.actuate.endpoint.annotation.*;
4+
import org.springframework.stereotype.Component;
5+
6+
import java.util.Map;
7+
import java.util.concurrent.ConcurrentHashMap;
8+
9+
@Component
10+
@Endpoint(id = "features", enableByDefault = true)
11+
public class FeaturesEndpoint {
12+
13+
private Map<String, Feature> features = new ConcurrentHashMap<>();
14+
15+
@ReadOperation
16+
public Map<String, Feature> features() {
17+
return features;
18+
}
19+
20+
@ReadOperation
21+
public Feature feature(@Selector String name) {
22+
return features.get(name);
23+
}
24+
25+
@WriteOperation
26+
public void configureFeature(@Selector String name, Feature feature) {
27+
features.put(name, feature);
28+
}
29+
30+
@DeleteOperation
31+
public void deleteFeature(@Selector String name) {
32+
features.remove(name);
33+
}
34+
35+
public static class Feature {
36+
private Boolean enabled;
37+
38+
public Boolean getEnabled() {
39+
return enabled;
40+
}
41+
42+
public void setEnabled(Boolean enabled) {
43+
this.enabled = enabled;
44+
}
45+
}
46+
47+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.baeldung;
2+
3+
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
4+
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
5+
import org.springframework.boot.actuate.endpoint.web.annotation.EndpointWebExtension;
6+
import org.springframework.boot.actuate.info.InfoEndpoint;
7+
import org.springframework.stereotype.Component;
8+
9+
import java.util.Map;
10+
11+
@Component
12+
@EndpointWebExtension(endpoint = InfoEndpoint.class)
13+
public class InfoWebEndpointExtension {
14+
15+
private final InfoEndpoint delegate;
16+
17+
public InfoWebEndpointExtension(InfoEndpoint infoEndpoint) {
18+
this.delegate = infoEndpoint;
19+
}
20+
21+
@ReadOperation
22+
public WebEndpointResponse<Map> info() {
23+
Map<String, Object> info = this.delegate.info();
24+
Integer status = getStatus(info);
25+
return new WebEndpointResponse<>(info, status);
26+
}
27+
28+
private Integer getStatus(Map<String, Object> info) {
29+
// return 5xx if this is a snapshot
30+
return 200;
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.baeldung.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
5+
import org.springframework.security.config.web.server.ServerHttpSecurity;
6+
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
7+
import org.springframework.security.core.userdetails.User;
8+
import org.springframework.security.core.userdetails.UserDetails;
9+
import org.springframework.security.web.server.SecurityWebFilterChain;
10+
11+
@EnableWebFluxSecurity
12+
public class SecurityConfig {
13+
14+
@Bean
15+
public SecurityWebFilterChain securityWebFilterChain(
16+
ServerHttpSecurity http) {
17+
return http.authorizeExchange()
18+
// We'll open up all actuator endpoints for demo purposes
19+
.pathMatchers("/actuator/**").permitAll()
20+
.anyExchange().authenticated()
21+
.and().build();
22+
}
23+
24+
@Bean
25+
public MapReactiveUserDetailsService userDetailsService() {
26+
UserDetails user = User.withDefaultPasswordEncoder()
27+
.username("user").password("password").roles("USER")
28+
.build();
29+
return new MapReactiveUserDetailsService(user);
30+
}
31+
32+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
management.endpoints.web.expose=*
2+
3+
info.app.name=Spring Boot 2 actuator Application

0 commit comments

Comments
 (0)