Skip to content

Commit

Permalink
Use config server uri from boostrap context if discovery is enabled. F…
Browse files Browse the repository at this point in the history
…ixes #2338 (#2343)

Co-authored-by: Ryan Baxter <[email protected]>
  • Loading branch information
Ryan Baxter and ryanjbaxter authored Nov 14, 2023
1 parent 10b5201 commit 25a3655
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,23 @@ protected Environment getRemoteEnvironment(ConfigDataLoaderContext context, Conf
String name = properties.getName();
String profile = resource.getProfiles();
String token = properties.getToken();
int noOfUrls = properties.getUri().length;
if (noOfUrls > 1) {
String[] uris;
boolean discoveryEnabled = properties.getDiscovery().isEnabled();
ConfigClientProperties bootstrapConfigClientProperties = context.getBootstrapContext()
.get(ConfigClientProperties.class);
// 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) {
uris = bootstrapConfigClientProperties.getUri();
}
else {
uris = properties.getUri();
}
int noOfUrls = uris.length;
if (uris.length > 1) {
logger.info("Multiple Config Server Urls found listed.");
}

Expand All @@ -284,10 +299,19 @@ protected Environment getRemoteEnvironment(ConfigDataLoaderContext context, Conf
.get(ConfigClientRequestTemplateFactory.class);

for (int i = 0; i < noOfUrls; i++) {
ConfigClientProperties.Credentials credentials = properties.getCredentials(i);
String uri = credentials.getUri();
String username = credentials.getUsername();
String password = credentials.getPassword();
String username;
String password;
String uri = uris[i];
if (discoveryEnabled) {
password = bootstrapConfigClientProperties.getPassword();
username = bootstrapConfigClientProperties.getUsername();
}
else {
ConfigClientProperties.Credentials credentials = properties.getCredentials(i);
uri = credentials.getUri();
username = credentials.getUsername();
password = credentials.getPassword();
}

logger.info("Fetching config from server at : " + uri);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,28 @@ void nonProfileSpecific() {

}

@Test
public void useDiscoveryUriIfEnabled() throws Exception {
String[] uris = new String[] { "http://uritest:8888" };
properties.setUri(uris);
ConfigClientProperties.Discovery discovery = new ConfigClientProperties.Discovery();
discovery.setEnabled(true);
discovery.setServiceId("configservice");
properties.setDiscovery(discovery);
this.loader = new ConfigServerConfigDataLoader(destination -> logger);
ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class);
RestTemplate restTemplate = new RestTemplate(requestFactory);
when(bootstrapContext.get(RestTemplate.class)).thenReturn(restTemplate);
ConfigClientProperties bootstrapConfigClientProperties = new ConfigClientProperties();
bootstrapConfigClientProperties.setDiscovery(discovery);
bootstrapConfigClientProperties.setUri(new String[] { "http://configservice:8888" });
when(bootstrapContext.get(ConfigClientProperties.class)).thenReturn(bootstrapConfigClientProperties);

mockRequestResponse(requestFactory, "http://configservice:8888", HttpStatus.OK);

assertThat(this.loader.load(context, resource)).isNotNull();
}

@Disabled
@Test
// TODO Enable once we have
Expand Down

0 comments on commit 25a3655

Please sign in to comment.