File tree Expand file tree Collapse file tree
test/java/com/baeldung/actuator Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 ();
Original file line number Diff line number Diff line change 11server.port =8081
2+ management.endpoints.web.expose =*
3+ info.app.name =Spring Boot 2 actuator Application
24
35logging.level.root =INFO
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments