Default label on client is null (so server can set priority)
Fixes gh-108
This commit is contained in:
@@ -49,7 +49,7 @@ public class ConfigClientProperties {
|
||||
@Value("${spring.application.name:application}")
|
||||
private String name;
|
||||
|
||||
private String label = "master";
|
||||
private String label;
|
||||
|
||||
private String username;
|
||||
|
||||
@@ -239,7 +239,7 @@ public class ConfigClientProperties {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ConfigClientProperties [enabled=" + enabled + ", profile=" + profile
|
||||
+ ", name=" + name + ", label=" + label + ", username=" + username
|
||||
+ ", name=" + name + ", label=" + (label==null?"":label) + ", username=" + username
|
||||
+ ", password=" + password + ", uri=" + uri + ", discovery.enabled="
|
||||
+ discovery.enabled + ", failFast=" + failFast + "]";
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.client.HttpServerErrorException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@@ -65,10 +66,16 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator
|
||||
Exception error = null;
|
||||
String errorBody = null;
|
||||
try {
|
||||
Environment result = restTemplate.exchange(
|
||||
client.getRawUri() + "/{name}/{profile}/{label}", HttpMethod.GET,
|
||||
new HttpEntity<Void>((Void) null), Environment.class,
|
||||
client.getName(), client.getProfile(), client.getLabel()).getBody();
|
||||
Object[] args = new String[] { client.getName(), client.getProfile() };
|
||||
String path = "/{name}/{profile}";
|
||||
if (StringUtils.hasText(client.getLabel())) {
|
||||
args = new String[] { client.getName(), client.getProfile(),
|
||||
client.getLabel() };
|
||||
path = path + "/{label}";
|
||||
}
|
||||
Environment result = restTemplate.exchange(client.getRawUri() + path,
|
||||
HttpMethod.GET, new HttpEntity<Void>((Void) null), Environment.class,
|
||||
args).getBody();
|
||||
for (PropertySource source : result.getPropertySources()) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> map = (Map<String, Object>) source.getSource();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.cloud.autoconfigure;
|
||||
package org.springframework.cloud.config.client;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.cloud.autoconfigure;
|
||||
package org.springframework.cloud.config.client;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.mockito.Matchers;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.cloud.config.client.ConfigClientProperties;
|
||||
import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator;
|
||||
import org.springframework.cloud.config.environment.Environment;
|
||||
@@ -44,14 +45,25 @@ public class ConfigServicePropertySourceLocatorTests {
|
||||
@Test
|
||||
public void sunnyDay() {
|
||||
Environment body = new Environment("app", "master");
|
||||
mockRequestResponse(new ResponseEntity<Environment>(body, HttpStatus.OK));
|
||||
mockRequestResponseWithoutLabel(new ResponseEntity<Environment>(body,
|
||||
HttpStatus.OK));
|
||||
locator.setRestTemplate(restTemplate);
|
||||
assertNotNull(locator.locate(environment));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sunnyDayWithLabel() {
|
||||
Environment body = new Environment("app", "master");
|
||||
mockRequestResponseWithLabel(new ResponseEntity<Environment>(body,
|
||||
HttpStatus.OK), "v1.0.0");
|
||||
locator.setRestTemplate(restTemplate);
|
||||
EnvironmentTestUtils.addEnvironment(environment, "spring.cloud.config.label:v1.0.0");
|
||||
assertNotNull(locator.locate(environment));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void failsQuietly() {
|
||||
mockRequestResponse(new ResponseEntity<String>("Wah!",
|
||||
mockRequestResponseWithoutLabel(new ResponseEntity<String>("Wah!",
|
||||
HttpStatus.INTERNAL_SERVER_ERROR));
|
||||
locator.setRestTemplate(restTemplate);
|
||||
assertNull(locator.locate(environment));
|
||||
@@ -75,21 +87,32 @@ public class ConfigServicePropertySourceLocatorTests {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
Mockito.when(response.getHeaders()).thenReturn(headers);
|
||||
Mockito.when(response.getStatusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
Mockito.when(response.getBody()).thenReturn(new ByteArrayInputStream("{}".getBytes()));
|
||||
Mockito.when(response.getStatusCode()).thenReturn(
|
||||
HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
Mockito.when(response.getBody()).thenReturn(
|
||||
new ByteArrayInputStream("{}".getBytes()));
|
||||
locator.setRestTemplate(restTemplate);
|
||||
expected.expectCause(IsInstanceOf.<Throwable>instanceOf(HttpServerErrorException.class));
|
||||
expected.expectCause(IsInstanceOf
|
||||
.<Throwable> instanceOf(HttpServerErrorException.class));
|
||||
expected.expectMessage("fail fast property is set");
|
||||
assertNull(locator.locate(environment));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void mockRequestResponse(ResponseEntity<?> response) {
|
||||
private void mockRequestResponseWithLabel(ResponseEntity<?> response, String label) {
|
||||
Mockito.when(
|
||||
restTemplate.exchange(Mockito.any(String.class),
|
||||
Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class),
|
||||
Mockito.any(Class.class), Matchers.anyString(),
|
||||
Matchers.anyString(), Matchers.anyString())).thenReturn(response);
|
||||
Matchers.anyString(), Matchers.eq(label))).thenReturn(response);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void mockRequestResponseWithoutLabel(ResponseEntity<?> response) {
|
||||
Mockito.when(
|
||||
restTemplate.exchange(Mockito.any(String.class),
|
||||
Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class),
|
||||
Mockito.any(Class.class), Matchers.anyString(),
|
||||
Matchers.anyString())).thenReturn(response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import org.springframework.cloud.config.client.ConfigClientProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Bootstrap configuration to fetch external configuration from a (possibly remote)
|
||||
@@ -45,10 +46,15 @@ public class ConfigServerBootstrapConfiguration {
|
||||
@Autowired
|
||||
private ConfigClientProperties client;
|
||||
|
||||
@Autowired
|
||||
private ConfigServerProperties server;
|
||||
|
||||
@Bean
|
||||
public EnvironmentRepositoryPropertySourceLocator environmentRepositoryPropertySourceLocator() {
|
||||
String label = StringUtils.hasText(client.getLabel()) ? client.getLabel()
|
||||
: server.getDefaultLabel();
|
||||
return new EnvironmentRepositoryPropertySourceLocator(repository,
|
||||
client.getName(), client.getProfile(), client.getLabel());
|
||||
client.getName(), client.getProfile(), label);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user