Initialize ConfigClientProperties instead of using whats in bootstrap. (#2282)
Fixes #2281 Co-authored-by: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com>
This commit is contained in:
@@ -22,7 +22,6 @@ import java.util.Properties;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.boot.BootstrapRegistry;
|
||||
import org.springframework.boot.BootstrapRegistry.InstanceSupplier;
|
||||
import org.springframework.boot.ConfigurableBootstrapContext;
|
||||
@@ -74,8 +73,21 @@ public class ConfigServerConfigDataLocationResolver
|
||||
configClientProperties = binder
|
||||
.bind(ConfigClientProperties.PREFIX, Bindable.of(ConfigClientProperties.class), bindHandler)
|
||||
.orElseGet(ConfigClientProperties::new);
|
||||
BeanUtils.copyProperties(context.getBootstrapContext().get(ConfigClientProperties.class),
|
||||
configClientProperties);
|
||||
boolean discoveryEnabled = context.getBinder()
|
||||
.bind(CONFIG_DISCOVERY_ENABLED, Bindable.of(Boolean.class), getBindHandler(context)).orElse(false);
|
||||
// In the case where discovery is enabled we need to extract the config server
|
||||
// uris, username, and password
|
||||
// from the properties from the context. These are set in
|
||||
// ConfigServerInstanceMonitor.refresh which will only
|
||||
// be called the first time we fetch configuration.
|
||||
if (discoveryEnabled) {
|
||||
ConfigClientProperties bootstrapConfigClientProperties = context.getBootstrapContext()
|
||||
.get(ConfigClientProperties.class);
|
||||
|
||||
configClientProperties.setUri(bootstrapConfigClientProperties.getUri());
|
||||
configClientProperties.setPassword(bootstrapConfigClientProperties.getPassword());
|
||||
configClientProperties.setUsername(bootstrapConfigClientProperties.getUsername());
|
||||
}
|
||||
}
|
||||
else {
|
||||
configClientProperties = binder
|
||||
|
||||
@@ -167,22 +167,6 @@ public class ConfigServerConfigDataLocationResolverTests {
|
||||
assertThat(resource.getProperties().getUri()).containsExactly(locationUri.split(","));
|
||||
}
|
||||
|
||||
@Test
|
||||
void useExistingConfigClientPropertiesInBootstrapContext() {
|
||||
ConfigurableBootstrapContext bootstrapContext = mock(ConfigurableBootstrapContext.class);
|
||||
when(bootstrapContext.isRegistered(eq(ConfigClientProperties.class))).thenReturn(true);
|
||||
ConfigClientProperties configClientProperties = new ConfigClientProperties();
|
||||
configClientProperties.setUri(new String[] { "http://myuri" });
|
||||
when(bootstrapContext.get(eq(ConfigClientProperties.class))).thenReturn(configClientProperties);
|
||||
when(context.getBootstrapContext()).thenReturn(bootstrapContext);
|
||||
List<ConfigServerConfigDataResource> resources = this.resolver.resolveProfileSpecific(context,
|
||||
ConfigDataLocation.of("configserver:"), mock(Profiles.class));
|
||||
assertThat(resources).hasSize(1);
|
||||
verify(bootstrapContext, times(1)).get(eq(ConfigClientProperties.class));
|
||||
ConfigServerConfigDataResource resource = resources.get(0);
|
||||
assertThat(resource.getProperties().getUri()).isEqualTo(new String[] { "http://myuri" });
|
||||
}
|
||||
|
||||
@Test
|
||||
void createNewConfigClientPropertiesInBootstrapContext() {
|
||||
ConfigurableBootstrapContext bootstrapContext = mock(ConfigurableBootstrapContext.class);
|
||||
@@ -213,7 +197,6 @@ public class ConfigServerConfigDataLocationResolverTests {
|
||||
ConfigDataLocation.of("configserver:http://urlNo2"), mock(Profiles.class));
|
||||
assertThat(resources1).hasSize(1);
|
||||
assertThat(resources2).hasSize(1);
|
||||
verify(bootstrapContext, times(2)).get(eq(ConfigClientProperties.class));
|
||||
ConfigServerConfigDataResource resource1 = resources1.get(0);
|
||||
assertThat(resource1.getProperties().getUri()).isEqualTo(new String[] { "http://urlNo1" });
|
||||
ConfigServerConfigDataResource resource2 = resources2.get(0);
|
||||
|
||||
@@ -50,7 +50,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.verify;
|
||||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
|
||||
|
||||
@SpringBootTest(classes = HttpClientSupportTest.TestConfiguration.class,
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class HttpClientSupportTest {
|
||||
|
||||
@LocalServerPort
|
||||
@@ -71,7 +71,7 @@ public class HttpClientSupportTest {
|
||||
@EnabledOnJre(JRE.JAVA_8)
|
||||
public void httpsProxy() throws GeneralSecurityException, IOException {
|
||||
WireMockServer wireMockProxyServer = new WireMockServer(
|
||||
options().httpDisabled(true).dynamicHttpsPort().enableBrowserProxying(true).trustAllProxyTargets(true));
|
||||
options().httpDisabled(true).dynamicHttpsPort().enableBrowserProxying(true).trustAllProxyTargets(true));
|
||||
WireMockServer wireMockServer = new WireMockServer(options().httpDisabled(true).dynamicHttpsPort());
|
||||
wireMockProxyServer.start();
|
||||
wireMockServer.start();
|
||||
@@ -91,7 +91,7 @@ public class HttpClientSupportTest {
|
||||
try {
|
||||
httpClient = HttpClientSupport.builder(properties).build();
|
||||
response = httpClient
|
||||
.execute(new HttpGet("https://localhost:" + wireMockServer.httpsPort() + "/test/proxy"));
|
||||
.execute(new HttpGet("https://localhost:" + wireMockServer.httpsPort() + "/test/proxy"));
|
||||
}
|
||||
finally {
|
||||
if (response != null) {
|
||||
|
||||
Reference in New Issue
Block a user