Work around bootstrap starting reactive server

This commit is contained in:
Spencer Gibb
2017-01-27 10:02:28 -07:00
parent 111cca2cb5
commit 8f368db463
5 changed files with 81 additions and 3 deletions

View File

@@ -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<ApplicationEnvironmentPreparedEvent>, 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<String, Object> 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;
}
}

View File

@@ -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<ApplicationEnvironmentPreparedEvent>, 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<String, Object> map = Collections.singletonMap("spring.reactive.enabled", "false");
environment.getPropertySources().addLast(new MapPropertySource("bootstrap-web-disabled", map));
}
@Override
public int getOrder() {
return DEFAULT_ORDER-1;
}
}

View File

@@ -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

View File

@@ -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 {

View File

@@ -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);
}
}