Use BindHandler in ConsulConfigServerBootstrapper
Fixes gh-688
This commit is contained in:
@@ -20,6 +20,8 @@ import com.ecwid.consul.v1.ConsulClient;
|
||||
|
||||
import org.springframework.boot.BootstrapRegistry;
|
||||
import org.springframework.boot.Bootstrapper;
|
||||
import org.springframework.boot.context.properties.bind.BindHandler;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.commons.util.InetUtilsProperties;
|
||||
@@ -41,7 +43,8 @@ public class ConsulConfigServerBootstrapper implements Bootstrapper {
|
||||
// create consul client
|
||||
registry.registerIfAbsent(ConsulProperties.class, context -> {
|
||||
Binder binder = context.get(Binder.class);
|
||||
return binder.bind(ConsulProperties.PREFIX, ConsulProperties.class).orElseGet(ConsulProperties::new);
|
||||
return binder.bind(ConsulProperties.PREFIX, Bindable.of(ConsulProperties.class), getBindHandler(context))
|
||||
.orElseGet(ConsulProperties::new);
|
||||
});
|
||||
registry.registerIfAbsent(ConsulClient.class, context -> {
|
||||
ConsulProperties consulProperties = context.get(ConsulProperties.class);
|
||||
@@ -54,7 +57,8 @@ public class ConsulConfigServerBootstrapper implements Bootstrapper {
|
||||
}
|
||||
ConsulClient consulClient = context.get(ConsulClient.class);
|
||||
ConsulDiscoveryProperties properties = binder
|
||||
.bind(ConsulDiscoveryProperties.PREFIX, ConsulDiscoveryProperties.class)
|
||||
.bind(ConsulDiscoveryProperties.PREFIX, Bindable.of(ConsulDiscoveryProperties.class),
|
||||
getBindHandler(context))
|
||||
.orElseGet(() -> new ConsulDiscoveryProperties(new InetUtils(new InetUtilsProperties())));
|
||||
return new ConsulDiscoveryClient(consulClient, properties);
|
||||
});
|
||||
@@ -76,6 +80,10 @@ public class ConsulConfigServerBootstrapper implements Bootstrapper {
|
||||
|
||||
}
|
||||
|
||||
private BindHandler getBindHandler(org.springframework.boot.BootstrapContext context) {
|
||||
return context.getOrElse(BindHandler.class, null);
|
||||
}
|
||||
|
||||
private boolean isDiscoveryEnabled(Binder binder) {
|
||||
return binder.bind(ConfigClientProperties.CONFIG_DISCOVERY_ENABLED, Boolean.class).orElse(false);
|
||||
}
|
||||
|
||||
@@ -20,9 +20,15 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.BootstrapRegistry;
|
||||
import org.springframework.boot.Bootstrapper;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.context.properties.bind.BindContext;
|
||||
import org.springframework.boot.context.properties.bind.BindHandler;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
|
||||
import org.springframework.cloud.config.client.ConfigServerInstanceProvider;
|
||||
import org.springframework.cloud.consul.discovery.ConsulDiscoveryClient;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@@ -46,9 +52,12 @@ public class ConsulConfigServerBootstrapperTests {
|
||||
@Test
|
||||
public void enabledAddsInstanceProviderFn() {
|
||||
AtomicReference<ConsulDiscoveryClient> bootstrapDiscoveryClient = new AtomicReference<>();
|
||||
BindHandlerBootstrapper bindHandlerBootstrapper = new BindHandlerBootstrapper();
|
||||
ConfigurableApplicationContext context = new SpringApplicationBuilder(TestConfig.class)
|
||||
.properties("--server.port=0", "spring.cloud.config.discovery.enabled=true",
|
||||
"spring.cloud.service-registry.auto-registration.enabled=false")
|
||||
"spring.cloud.service-registry.auto-registration.enabled=false",
|
||||
"spring.cloud.consul.host=localhost")
|
||||
.addBootstrapper(bindHandlerBootstrapper)
|
||||
.addBootstrapper(registry -> registry.addCloseListener(event -> {
|
||||
bootstrapDiscoveryClient.set(event.getBootstrapContext().get(ConsulDiscoveryClient.class));
|
||||
ConfigServerInstanceProvider.Function providerFn = event.getBootstrapContext()
|
||||
@@ -58,6 +67,7 @@ public class ConsulConfigServerBootstrapperTests {
|
||||
})).run();
|
||||
ConsulDiscoveryClient discoveryClient = context.getBean(ConsulDiscoveryClient.class);
|
||||
assertThat(discoveryClient == bootstrapDiscoveryClient.get()).isTrue();
|
||||
assertThat(bindHandlerBootstrapper.onSuccessCount).isGreaterThan(0);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@@ -67,4 +77,22 @@ public class ConsulConfigServerBootstrapperTests {
|
||||
|
||||
}
|
||||
|
||||
static class BindHandlerBootstrapper implements Bootstrapper {
|
||||
|
||||
private int onSuccessCount = 0;
|
||||
|
||||
@Override
|
||||
public void intitialize(BootstrapRegistry registry) {
|
||||
registry.register(BindHandler.class, context -> new BindHandler() {
|
||||
@Override
|
||||
public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context,
|
||||
Object result) {
|
||||
onSuccessCount++;
|
||||
return result;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user