From 8f368db4631d5c628bf5ed8cb68eb06e6543a57f Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Fri, 27 Jan 2017 10:02:28 -0700 Subject: [PATCH] Work around bootstrap starting reactive server --- .../PostBootstrapApplicationListener.java | 38 +++++++++++++++++++ .../PreBootstrapApplicationListener.java | 37 ++++++++++++++++++ src/main/resources/META-INF/spring.factories | 5 +++ .../gateway/test/GatewayIntegrationTests.java | 3 +- .../gateway/test/GatewayTestApplication.java | 1 - 5 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/springframework/cloud/gateway/config/PostBootstrapApplicationListener.java create mode 100644 src/main/java/org/springframework/cloud/gateway/config/PreBootstrapApplicationListener.java diff --git a/src/main/java/org/springframework/cloud/gateway/config/PostBootstrapApplicationListener.java b/src/main/java/org/springframework/cloud/gateway/config/PostBootstrapApplicationListener.java new file mode 100644 index 00000000..79317c82 --- /dev/null +++ b/src/main/java/org/springframework/cloud/gateway/config/PostBootstrapApplicationListener.java @@ -0,0 +1,38 @@ +package org.springframework.cloud.gateway.config; + +import java.util.Collections; +import java.util.Map; + +import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.core.Ordered; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; + +import static org.springframework.cloud.bootstrap.BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME; +import static org.springframework.cloud.bootstrap.BootstrapApplicationListener.DEFAULT_ORDER; + +/** + * Reenables reactive http server after bootstrap. + * TODO: remove when boot 2.0 handles the NONE web case + * @author Spencer Gibb + */ +public class PostBootstrapApplicationListener + implements ApplicationListener, Ordered { + @Override + public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { + ConfigurableEnvironment environment = event.getEnvironment(); + // don't listen to events in a bootstrap context + if (environment.getPropertySources().contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) { + return; + } + final Map map = Collections.singletonMap("spring.reactive.enabled", "true"); + environment.getPropertySources().addBefore("bootstrap-web-disabled", + new MapPropertySource("bootstrap-web-reenable", map)); + } + + @Override + public int getOrder() { + return DEFAULT_ORDER+1; + } +} diff --git a/src/main/java/org/springframework/cloud/gateway/config/PreBootstrapApplicationListener.java b/src/main/java/org/springframework/cloud/gateway/config/PreBootstrapApplicationListener.java new file mode 100644 index 00000000..7d574be8 --- /dev/null +++ b/src/main/java/org/springframework/cloud/gateway/config/PreBootstrapApplicationListener.java @@ -0,0 +1,37 @@ +package org.springframework.cloud.gateway.config; + +import java.util.Collections; +import java.util.Map; + +import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.core.Ordered; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; + +import static org.springframework.cloud.bootstrap.BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME; +import static org.springframework.cloud.bootstrap.BootstrapApplicationListener.DEFAULT_ORDER; + +/** + * Disables reactive http server just before bootstrap. + * TODO: remove when boot 2.0 handles the NONE web case + * @author Spencer Gibb + */ +public class PreBootstrapApplicationListener + implements ApplicationListener, Ordered { + @Override + public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { + ConfigurableEnvironment environment = event.getEnvironment(); + // don't listen to events in a bootstrap context + if (environment.getPropertySources().contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) { + return; + } + final Map map = Collections.singletonMap("spring.reactive.enabled", "false"); + environment.getPropertySources().addLast(new MapPropertySource("bootstrap-web-disabled", map)); + } + + @Override + public int getOrder() { + return DEFAULT_ORDER-1; + } +} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index 19b1d797..2b713156 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -5,3 +5,8 @@ org.springframework.cloud.gateway.config.GatewayAutoConfiguration # Environment Post Processors org.springframework.boot.env.EnvironmentPostProcessor=\ org.springframework.cloud.gateway.config.GatewayEnvironmentPostProcessor + +# Application Listeners +org.springframework.context.ApplicationListener=\ +org.springframework.cloud.gateway.config.PreBootstrapApplicationListener,\ +org.springframework.cloud.gateway.config.PostBootstrapApplicationListener diff --git a/src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java b/src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java index 56d47929..2a1dc046 100644 --- a/src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java +++ b/src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java @@ -38,8 +38,7 @@ import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = RANDOM_PORT, - properties = "spring.cloud.bootstrap.enabled=false") +@SpringBootTest(webEnvironment = RANDOM_PORT) @SuppressWarnings("unchecked") public class GatewayIntegrationTests { diff --git a/src/test/java/org/springframework/cloud/gateway/test/GatewayTestApplication.java b/src/test/java/org/springframework/cloud/gateway/test/GatewayTestApplication.java index 47ea6b14..86017a18 100644 --- a/src/test/java/org/springframework/cloud/gateway/test/GatewayTestApplication.java +++ b/src/test/java/org/springframework/cloud/gateway/test/GatewayTestApplication.java @@ -9,7 +9,6 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; public class GatewayTestApplication { public static void main(String[] args) { - System.setProperty("spring.cloud.bootstrap.enabled", "false"); //TODO: fix bootstrap SpringApplication.run(GatewayTestApplication.class, args); } }