Skip to content

Commit d74c882

Browse files
authored
Merge pull request eugenp#3369 from CalamarBicefalo/master
BAEL-1430: Add Spring Boot 2 actuator module
2 parents 07750c2 + 6d6cc2d commit d74c882

7 files changed

Lines changed: 145 additions & 1 deletion

File tree

‎spring-5/pom.xml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@
3939
<groupId>org.springframework.boot</groupId>
4040
<artifactId>spring-boot-starter-webflux</artifactId>
4141
</dependency>
42-
<dependency>
42+
<dependency>
4343
<groupId>org.springframework.boot</groupId>
4444
<artifactId>spring-boot-starter-hateoas</artifactId>
4545
</dependency>
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-actuator</artifactId>
49+
</dependency>
4650
<dependency>
4751
<groupId>org.projectreactor</groupId>
4852
<artifactId>reactor-spring</artifactId>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.actuator;
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 com.baeldung.actuator;
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 com.baeldung.actuator;
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+
}

‎spring-5/src/main/java/com/baeldung/security/SecurityConfig.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class SecurityConfig {
1717
public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) {
1818
return http.authorizeExchange()
1919
.pathMatchers("/admin").hasAuthority("ROLE_ADMIN")
20+
.pathMatchers("/actuator/**").permitAll()
2021
.anyExchange().authenticated()
2122
.and().formLogin()
2223
.and().build();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
server.port=8081
2+
management.endpoints.web.expose=*
3+
info.app.name=Spring Boot 2 actuator Application
24

35
logging.level.root=INFO
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.actuator;
2+
3+
import com.baeldung.jsonb.Spring5Application;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.boot.test.web.client.TestRestTemplate;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.http.ResponseEntity;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
13+
import java.io.IOException;
14+
15+
import static org.junit.Assert.assertEquals;
16+
17+
@RunWith(SpringRunner.class)
18+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Spring5Application.class)
19+
public class ActuatorInfoIntegrationTest {
20+
21+
@Autowired
22+
private TestRestTemplate restTemplate;
23+
24+
@Test
25+
public void whenGetInfo_thenReturns200() throws IOException {
26+
final ResponseEntity<String> responseEntity = this.restTemplate.getForEntity("/actuator/info", String.class);
27+
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
28+
}
29+
30+
@Test
31+
public void whenFeatures_thenReturns200() throws IOException {
32+
final ResponseEntity<String> responseEntity = this.restTemplate.getForEntity("/actuator/features", String.class);
33+
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
34+
}
35+
}

0 commit comments

Comments
 (0)