Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 18d5b85

Browse files
committed
Add support for Actuator management context
This commit adds support for the separate application context that Spring Boot creates when a management port is set. In AOT mode, the context is processed at build time, exactly as the maine one is, and the ManagementContextFactory implementation is replaced by an AOT-specific one that loads the context from the generated code. Closes gh-1146
1 parent a5399a8 commit 18d5b85

File tree

25 files changed

+961
-0
lines changed

25 files changed

+961
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Spring Boot project with Spring WebFlux and Spring Boot actuators running on a separate port.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
${PWD%/*samples/*}/scripts/compileWithMaven.sh $* && ${PWD%/*samples/*}/scripts/test.sh $*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.experimental</groupId>
8+
<artifactId>spring-native-sample-parent</artifactId>
9+
<version>0.11.0-SNAPSHOT</version>
10+
<relativePath>../maven-parent/pom.xml</relativePath>
11+
</parent>
12+
<groupId>com.example</groupId>
13+
<artifactId>actuator-webflux-management-port</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<name>actuator</name>
16+
<description>Actuator sample with WebFlux and separate management port</description>
17+
18+
<properties>
19+
<java.version>11</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.experimental</groupId>
25+
<artifactId>spring-native</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-actuator</artifactId>
30+
<exclusions>
31+
<!-- Exclude metrics when not used in order to reduce significantly the memory footprint -->
32+
<exclusion>
33+
<groupId>io.micrometer</groupId>
34+
<artifactId>micrometer-core</artifactId>
35+
</exclusion>
36+
</exclusions>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-webflux</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.projectreactor</groupId>
50+
<artifactId>reactor-test</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
</dependencies>
54+
55+
<!-- Check out the parent POM for the plugins configuration -->
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.springframework.experimental</groupId>
60+
<artifactId>spring-aot-maven-plugin</artifactId>
61+
</plugin>
62+
<plugin>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-maven-plugin</artifactId>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2019-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.actuator;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
22+
@SpringBootApplication
23+
public class ActuatorApplication {
24+
25+
public static void main(String[] args) {
26+
SpringApplication.run(ActuatorApplication.class, args);
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2019-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.actuator;
18+
19+
import org.springframework.web.bind.annotation.GetMapping;
20+
import org.springframework.web.bind.annotation.RestController;
21+
22+
@RestController
23+
public class TestController {
24+
25+
@GetMapping("/hello")
26+
public String hello() {
27+
return "Hello World";
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
management.server.port=8081
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
source ${PWD%/*samples/*}/scripts/wait.sh
3+
RC=0
4+
5+
wait_log target/native/test-output.txt "Started ActuatorApplication in" || RC=$?
6+
wait_http localhost:8080/hello "Hello World" || RC=$?
7+
wait_http localhost:8081/actuator/health "UP" || RC=$?
8+
9+
exit $RC
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Spring Boot project with Spring MVC and Spring Boot actuators running on a separate port.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
${PWD%/*samples/*}/scripts/compileWithMaven.sh $* && ${PWD%/*samples/*}/scripts/test.sh $*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.experimental</groupId>
8+
<artifactId>spring-native-sample-parent</artifactId>
9+
<version>0.11.0-SNAPSHOT</version>
10+
<relativePath>../maven-parent/pom.xml</relativePath>
11+
</parent>
12+
<groupId>com.example</groupId>
13+
<artifactId>actuator-webmvc-management-port</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<name>actuator</name>
16+
<description>Actuator sample with WebMVC and separate management port</description>
17+
18+
<properties>
19+
<java.version>11</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.experimental</groupId>
25+
<artifactId>spring-native</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-actuator</artifactId>
30+
<exclusions>
31+
<!-- Exclude metrics when not used in order to reduce significantly the memory footprint -->
32+
<exclusion>
33+
<groupId>io.micrometer</groupId>
34+
<artifactId>micrometer-core</artifactId>
35+
</exclusion>
36+
</exclusions>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-web</artifactId>
41+
<exclusions>
42+
<exclusion>
43+
<groupId>org.apache.tomcat.embed</groupId>
44+
<artifactId>tomcat-embed-core</artifactId>
45+
</exclusion>
46+
<exclusion>
47+
<groupId>org.apache.tomcat.embed</groupId>
48+
<artifactId>tomcat-embed-websocket</artifactId>
49+
</exclusion>
50+
</exclusions>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>org.apache.tomcat.experimental</groupId>
55+
<artifactId>tomcat-embed-programmatic</artifactId>
56+
<version>${tomcat.version}</version>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-starter-test</artifactId>
62+
<scope>test</scope>
63+
</dependency>
64+
</dependencies>
65+
66+
<!-- Check out the parent POM for the plugins configuration -->
67+
<build>
68+
<plugins>
69+
<plugin>
70+
<groupId>org.springframework.experimental</groupId>
71+
<artifactId>spring-aot-maven-plugin</artifactId>
72+
</plugin>
73+
<plugin>
74+
<groupId>org.springframework.boot</groupId>
75+
<artifactId>spring-boot-maven-plugin</artifactId>
76+
</plugin>
77+
</plugins>
78+
</build>
79+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.actuator;
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+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2019-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.actuator;
18+
19+
import org.springframework.web.bind.annotation.GetMapping;
20+
import org.springframework.web.bind.annotation.RestController;
21+
22+
@RestController
23+
public class TestController {
24+
25+
@GetMapping("/hello")
26+
public String hello() {
27+
return "Hello World";
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
management.server.port=8081
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
source ${PWD%/*samples/*}/scripts/wait.sh
3+
RC=0
4+
5+
wait_log target/native/test-output.txt "Started ActuatorApplication in" || RC=$?
6+
wait_http localhost:8080/hello "Hello World" || RC=$?
7+
wait_http localhost:8081/actuator/health "UP" || RC=$?
8+
9+
exit $RC

spring-aot/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@
9191
<artifactId>javax.servlet-api</artifactId>
9292
<scope>test</scope>
9393
</dependency>
94+
<dependency>
95+
<groupId>org.apache.tomcat.embed</groupId>
96+
<artifactId>tomcat-embed-core</artifactId>
97+
<scope>test</scope>
98+
</dependency>
9499
<dependency>
95100
<groupId>org.assertj</groupId>
96101
<artifactId>assertj-core</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.springframework.boot.actuate.autoconfigure.web;
2+
3+
import com.squareup.javapoet.ClassName;
4+
import com.squareup.javapoet.CodeBlock.Builder;
5+
6+
import org.springframework.aot.boot.actuate.web.AotManagementContextFactory;
7+
import org.springframework.aot.context.bootstrap.generator.ApplicationContextAotProcessor;
8+
import org.springframework.aot.context.bootstrap.generator.bean.BeanRegistrationWriter;
9+
import org.springframework.aot.context.bootstrap.generator.bean.DefaultBeanRegistrationWriter;
10+
import org.springframework.aot.context.bootstrap.generator.bean.descriptor.BeanInstanceDescriptor;
11+
import org.springframework.aot.context.bootstrap.generator.infrastructure.BootstrapWriterContext;
12+
import org.springframework.beans.factory.config.BeanDefinition;
13+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
14+
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextSuppliers;
15+
import org.springframework.context.support.GenericApplicationContext;
16+
17+
/**
18+
* A {@link BeanRegistrationWriter} that generates the child context that a
19+
* {@link ManagementContextFactory} handles.
20+
*
21+
* @author Stephane Nicoll
22+
*/
23+
class ManagementContextBeanRegistrationWriter implements BeanRegistrationWriter {
24+
25+
private static final String MANAGEMENT_BOOSTRAP_CLASS_NAME = "ManagementContextBoostrapInitializer";
26+
27+
private final GenericApplicationContext parent;
28+
29+
private final String beanName;
30+
31+
private final boolean reactive;
32+
33+
private final BeanDefinition beanDefinition;
34+
35+
private final BeanInstanceDescriptor beanInstanceDescriptor;
36+
37+
ManagementContextBeanRegistrationWriter(GenericApplicationContext parent, String beanName, boolean reactive) {
38+
this.parent = parent;
39+
this.beanName = beanName;
40+
this.reactive = reactive;
41+
this.beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(AotManagementContextFactory.class).getBeanDefinition();
42+
this.beanInstanceDescriptor = BeanInstanceDescriptor.of(beanDefinition.getResolvableType()).build();
43+
}
44+
45+
@Override
46+
public void writeBeanRegistration(BootstrapWriterContext context, Builder code) {
47+
ClassName managementBootstrapType = processManagementContext(context);
48+
new DefaultBeanRegistrationWriter(this.beanName, this.beanDefinition, this.beanInstanceDescriptor) {
49+
50+
@Override
51+
protected void writeInstanceSupplier(Builder code) {
52+
code.add("() -> new $T(", AotManagementContextFactory.class);
53+
code.add("() -> new $T(), $L", managementBootstrapType, reactive);
54+
code.add(")");
55+
}
56+
}.writeBeanRegistration(context, code);
57+
}
58+
59+
/**
60+
* Process the dedicated management context using the specified {@code context}.
61+
* @param context the writer context to use
62+
*/
63+
private ClassName processManagementContext(BootstrapWriterContext context) {
64+
GenericApplicationContext managementContext = createManagementContext();
65+
ApplicationContextAotProcessor processor = new ApplicationContextAotProcessor(this.parent.getClassLoader());
66+
BootstrapWriterContext writerContext = context.fork(MANAGEMENT_BOOSTRAP_CLASS_NAME);
67+
processor.process(managementContext, writerContext);
68+
return writerContext.getMainBootstrapClass().getClassName();
69+
}
70+
71+
@Override
72+
public BeanInstanceDescriptor getBeanInstanceDescriptor() {
73+
return this.beanInstanceDescriptor;
74+
}
75+
76+
private GenericApplicationContext createManagementContext() {
77+
return this.reactive ? ManagementContextSuppliers.Reactive.createManagementContext(this.parent)
78+
: ManagementContextSuppliers.Servlet.createManagementContext(this.parent);
79+
}
80+
81+
}

0 commit comments

Comments
 (0)