-
Notifications
You must be signed in to change notification settings - Fork 1k
Add Spring Boot xDS integration module boot4-xds
#6891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jrhee17
wants to merge
2
commits into
line:main
Choose a base branch
from
jrhee17:poc/spring-cloud-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| dependencies { | ||
| implementation project(':spring:boot4-xds') | ||
| implementation libs.spring.cloud.config.server | ||
| testImplementation libs.spring.boot4.starter.test | ||
| } | ||
|
|
||
| tasks.register('runConfigServer', JavaExec) { | ||
| classpath = sourceSets.main.runtimeClasspath | ||
| mainClass = 'example.springframework.boot.xds.cloudconfig.server.ConfigServerMain' | ||
| } | ||
|
|
||
| tasks.register('runClient', JavaExec) { | ||
| classpath = sourceSets.main.runtimeClasspath | ||
| mainClass = 'example.springframework.boot.xds.cloudconfig.client.ClientMain' | ||
| } |
49 changes: 49 additions & 0 deletions
49
...fig-xds/src/main/java/example/springframework/boot/xds/cloudconfig/client/ClientMain.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package example.springframework.boot.xds.cloudconfig.client; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import com.linecorp.armeria.client.WebClient; | ||
| import com.linecorp.armeria.common.AggregatedHttpResponse; | ||
|
|
||
| /** | ||
| * A Spring Boot client application that loads xDS resources from a | ||
| * Spring Cloud Config Server. Properties are loaded from {@code application-client.yml}. | ||
| * | ||
| * <p>Exposes a {@code GET /relay?path=...} endpoint that forwards the request to | ||
| * the upstream resolved by the xDS listener and returns the response. | ||
| */ | ||
| @SpringBootApplication | ||
| @RestController | ||
| public class ClientMain { | ||
|
|
||
| private final WebClient xdsWebClient; | ||
|
|
||
| public ClientMain(WebClient xdsWebClient) { | ||
| this.xdsWebClient = xdsWebClient; | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| createApplication().run(args); | ||
| } | ||
|
|
||
| public static SpringApplication createApplication() { | ||
| final SpringApplication app = new SpringApplication(ClientMain.class); | ||
| app.setAdditionalProfiles("client"); | ||
| // spring-cloud-config-server on the classpath disables the config client | ||
| // via ConfigServerBootstrapApplicationListener; re-enable it explicitly. | ||
| app.setDefaultProperties(Map.of("spring.cloud.config.enabled", "true")); | ||
| return app; | ||
| } | ||
|
|
||
| @GetMapping("/relay") | ||
| String relay(@RequestParam(defaultValue = "/actuator/health") String path) { | ||
| final AggregatedHttpResponse response = xdsWebClient.get(path).aggregate().join(); | ||
| return response.status() + "\n" + response.contentUtf8(); | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
...ds/src/main/java/example/springframework/boot/xds/cloudconfig/client/XdsClientConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package example.springframework.boot.xds.cloudconfig.client; | ||
|
|
||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| import com.linecorp.armeria.client.WebClient; | ||
| import com.linecorp.armeria.xds.XdsBootstrap; | ||
| import com.linecorp.armeria.xds.client.endpoint.XdsHttpPreprocessor; | ||
|
|
||
| @Configuration | ||
| class XdsClientConfig { | ||
|
|
||
| @Bean | ||
| XdsHttpPreprocessor xdsHttpPreprocessor(XdsBootstrap xdsBootstrap) { | ||
| return XdsHttpPreprocessor.ofListener("test-listener", xdsBootstrap); | ||
| } | ||
|
|
||
| @Bean | ||
| WebClient xdsWebClient(XdsHttpPreprocessor xdsHttpPreprocessor) { | ||
| return WebClient.of(xdsHttpPreprocessor); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
...s/src/main/java/example/springframework/boot/xds/cloudconfig/server/ConfigServerMain.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package example.springframework.boot.xds.cloudconfig.server; | ||
|
|
||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| import org.springframework.cloud.config.server.EnableConfigServer; | ||
|
|
||
| import com.linecorp.armeria.spring.xds.SpringXdsAutoConfiguration; | ||
|
|
||
| /** | ||
| * A Spring Cloud Config Server that serves xDS cluster definitions from a native | ||
| * (file-based) repository. | ||
| */ | ||
| @EnableConfigServer | ||
| @SpringBootApplication(exclude = SpringXdsAutoConfiguration.class) | ||
| public class ConfigServerMain { | ||
|
|
||
| public static void main(String[] args) { | ||
| createApplication().run(args); | ||
| } | ||
|
|
||
| public static SpringApplication createApplication() { | ||
| final SpringApplication app = new SpringApplication(ConfigServerMain.class); | ||
| app.setAdditionalProfiles("server", "native"); | ||
| return app; | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
examples/spring-cloud-config-xds/src/main/resources/config-repo/application.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| armeria: | ||
| xds: | ||
| listener: | ||
| test-listener: | | ||
| name: test-listener | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question) Could we use nested YAML syntax here instead? |
||
| api_listener: | ||
| api_listener: | ||
| "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager | ||
| codec_type: AUTO | ||
| stat_prefix: ingress_http | ||
| route_config: | ||
| name: local_route | ||
| virtual_hosts: | ||
| - name: local_service | ||
| domains: ["*"] | ||
| routes: | ||
| - match: | ||
| prefix: / | ||
| route: | ||
| cluster: test-cluster | ||
| http_filters: | ||
| - name: envoy.filters.http.router | ||
| typed_config: | ||
| "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router | ||
| cluster: | ||
| test-cluster: | | ||
| name: test-cluster | ||
| type: STATIC | ||
| load_assignment: | ||
| cluster_name: test-cluster | ||
| endpoints: | ||
| - lb_endpoints: | ||
| - endpoint: | ||
| address: | ||
| socket_address: | ||
| address: 127.0.0.1 | ||
| port_value: 8888 | ||
6 changes: 6 additions & 0 deletions
6
examples/spring-cloud-config-xds/src/main/resources/config/application-client.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| spring: | ||
| config: | ||
| "import": "configserver:" | ||
| cloud: | ||
| config: | ||
| uri: http://localhost:8888 |
9 changes: 9 additions & 0 deletions
9
examples/spring-cloud-config-xds/src/main/resources/config/application-server.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| server: | ||
| port: 8888 | ||
|
|
||
| spring: | ||
| cloud: | ||
| config: | ||
| server: | ||
| native: | ||
| searchLocations: classpath:/config-repo/ |
128 changes: 128 additions & 0 deletions
128
...st/java/example/springframework/boot/xds/cloudconfig/SpringCloudConfigXdsExampleTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package example.springframework.boot.xds.cloudconfig; | ||
|
jrhee17 marked this conversation as resolved.
|
||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.awaitility.Awaitility.await; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.boot.web.server.context.WebServerApplicationContext; | ||
| import org.springframework.cloud.context.environment.EnvironmentChangeEvent; | ||
| import org.springframework.context.ApplicationContext; | ||
| import org.springframework.context.ConfigurableApplicationContext; | ||
| import org.springframework.core.env.ConfigurableEnvironment; | ||
| import org.springframework.core.env.MapPropertySource; | ||
|
|
||
| import com.linecorp.armeria.client.WebClient; | ||
| import com.linecorp.armeria.common.AggregatedHttpResponse; | ||
| import com.linecorp.armeria.common.HttpStatus; | ||
| import com.linecorp.armeria.xds.ClusterSnapshot; | ||
| import com.linecorp.armeria.xds.XdsBootstrap; | ||
|
|
||
| import example.springframework.boot.xds.cloudconfig.client.ClientMain; | ||
| import example.springframework.boot.xds.cloudconfig.server.ConfigServerMain; | ||
|
|
||
| @SpringBootTest( | ||
| classes = ClientMain.class, | ||
| properties = { | ||
| "spring.config.import=configserver:", | ||
| "spring.cloud.config.enabled=true" | ||
| }) | ||
| class SpringCloudConfigXdsExampleTest { | ||
|
|
||
| private static ConfigurableApplicationContext server; | ||
| private static int configPort; | ||
|
|
||
| @BeforeAll | ||
| static void startConfigServer() { | ||
| server = ConfigServerMain.createApplication().run("--server.port=0"); | ||
|
|
||
| configPort = ((WebServerApplicationContext) server).getWebServer().getPort(); | ||
| System.setProperty("spring.cloud.config.uri", "http://localhost:" + configPort); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void stopConfigServer() { | ||
| System.clearProperty("spring.cloud.config.uri"); | ||
|
jrhee17 marked this conversation as resolved.
|
||
| if (server != null) { | ||
| server.close(); | ||
| } | ||
| } | ||
|
|
||
| @Autowired | ||
| WebClient xdsWebClient; | ||
|
|
||
| @Autowired | ||
| XdsBootstrap xdsBootstrap; | ||
|
|
||
| @Autowired | ||
| ConfigurableEnvironment environment; | ||
|
|
||
| @Autowired | ||
| ApplicationContext applicationContext; | ||
|
|
||
| @Test | ||
| void webClientViaXdsPreprocessor() { | ||
| // Verify the initial cluster snapshot loaded from config server (port 8888) | ||
| final AtomicReference<ClusterSnapshot> snapshotRef = new AtomicReference<>(); | ||
| xdsBootstrap.clusterRoot("test-cluster") | ||
| .addSnapshotWatcher((snapshot, error) -> { | ||
| if (snapshot != null) { | ||
| snapshotRef.set(snapshot); | ||
| } | ||
| }); | ||
| await().untilAsserted(() -> { | ||
| assertThat(snapshotRef.get()).isNotNull(); | ||
| assertThat(endpointPort(snapshotRef.get())).isEqualTo(8888); | ||
| }); | ||
|
|
||
| // Override the cluster endpoint to point at the config server's actual port | ||
| environment.getPropertySources() | ||
| .addFirst(new MapPropertySource("test", | ||
| Map.of("armeria.xds.cluster.test-cluster", clusterYaml(configPort)))); | ||
| applicationContext.publishEvent( | ||
| new EnvironmentChangeEvent(Set.of("armeria.xds.cluster.test-cluster"))); | ||
|
|
||
| // Wait for the xDS update to propagate | ||
| await().untilAsserted(() -> | ||
| assertThat(endpointPort(snapshotRef.get())).isEqualTo(configPort)); | ||
|
|
||
| // Call the config server's actuator health endpoint via the xDS-resolved client | ||
| final AggregatedHttpResponse response = | ||
| xdsWebClient.get("/actuator/health").aggregate().join(); | ||
| assertThat(response.status()).isEqualTo(HttpStatus.OK); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| private static int endpointPort(ClusterSnapshot snapshot) { | ||
| return snapshot.xdsResource().resource() | ||
| .getLoadAssignment() | ||
| .getEndpoints(0) | ||
| .getLbEndpoints(0) | ||
| .getEndpoint() | ||
| .getAddress() | ||
| .getSocketAddress() | ||
| .getPortValue(); | ||
| } | ||
|
|
||
| private static String clusterYaml(int port) { | ||
| return """ | ||
| name: test-cluster | ||
| type: STATIC | ||
| load_assignment: | ||
| cluster_name: test-cluster | ||
| endpoints: | ||
| - lb_endpoints: | ||
| - endpoint: | ||
| address: | ||
| socket_address: | ||
| address: 127.0.0.1 | ||
| port_value: %d | ||
| """.formatted(port); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| dependencies { | ||
| api project(':xds') | ||
| api libs.spring.boot4.autoconfigure | ||
| api libs.spring.cloud.context | ||
|
|
||
| annotationProcessor libs.spring.boot4.configuration.processor | ||
|
|
||
| testImplementation libs.spring.boot4.starter.test | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.