diff --git a/docs/modules/ROOT/pages/client.adoc b/docs/modules/ROOT/pages/client.adoc index 4661f5b1..4dc9986a 100644 --- a/docs/modules/ROOT/pages/client.adoc +++ b/docs/modules/ROOT/pages/client.adoc @@ -133,6 +133,25 @@ Label can also be provided as a comma-separated list. This behavior can be useful when working on a feature branch. For instance, you might want to align the config label with your branch but make it optional (in that case, use `spring.cloud.config.label=myfeature,develop`). +[[requesting-multiple-labels]] +== Requesting Multiple Labels + +Prior to Spring Cloud Config 4.2.0, if you set `spring.cloud.config.label` to a comma-separated list of labels, the Config Client would +try each label by making a request to the Config Server until it found one that worked. This meant that if the first label was found, subsequent labels would not be tried. + +As of Spring Cloud Config 4.2.0 if you set `spring.cloud.config.label` to a comma-separated list of labels **AND** set +`spring.cloud.config.send-all-labels` the Config Client will make a single request to the Config Server with the comma-separated list of labels +and if **THE CONFIG SERVER IS USING VERSION 4.2.0 OR LATER** it will return a single response with property sources for all the labels. + +NOTE: Setting `spring.cloud-config.send-all-labels` to `true`, setting `spring.cloud.config.label` to a comma-separated list of labels, +and using a Config Server version prior to 4.2.0 will result in unexpected behavior because the Config Server will try and find a label +that matches the comma-separated list value and will not try and split apart the labels. + +By sending all labels in a single request you can reduce the number of requests made to the Config Server. + +`spring.cloud.config.send-all-labels` is set to `false` by default so the old behavior is still the default, and it also maintains +compatibility with older versions of the Config Server. + [[specifying-multiple-urls-for-the-config-server]] == Specifying Multiple URLs for the Config Server diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigClientProperties.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigClientProperties.java index 0f17b3f1..9a4d3c3b 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigClientProperties.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigClientProperties.java @@ -179,6 +179,13 @@ public class ConfigClientProperties { */ private Map headers = new HashMap<>(); + /** + * If set to true the client will send all labels to the server instead of sending one + * at a time. Support for this would require a config server version of 4.2.0 or + * higher. + */ + private boolean sendAllLabels = false; + ConfigClientProperties() { } @@ -338,6 +345,14 @@ public class ConfigClientProperties { this.headers = headers; } + public boolean isSendAllLabels() { + return sendAllLabels; + } + + public void setSendAllLabels(boolean sendAllLabels) { + this.sendAllLabels = sendAllLabels; + } + private Credentials extractCredentials(int index) { Credentials result = new Credentials(); int noOfUrl = this.uri.length; @@ -427,7 +442,7 @@ public class ConfigClientProperties { + Arrays.toString(this.uri) + ", mediaType=" + this.mediaType + ", discovery=" + this.discovery + ", failFast=" + this.failFast + ", token=" + this.token + ", requestConnectTimeout=" + this.requestConnectTimeout + ", requestReadTimeout=" + this.requestReadTimeout + ", sendState=" - + this.sendState + ", headers=" + this.headers + "]"; + + this.sendState + ", headers=" + this.headers + ", sendAllLabels=" + this.sendAllLabels + "]"; } /** diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataLoader.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataLoader.java index aa94f6a4..81fd7157 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataLoader.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataLoader.java @@ -113,12 +113,19 @@ public class ConfigServerConfigDataLoader implements ConfigDataLoader response = new TestRestTemplate() + .getForEntity("http://localhost:" + this.port + BASE_PATH + "/env/my.prop", Map.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + Map res = response.getBody(); + assertThat(res).containsKey("propertySources"); + Map property = (Map) res.get("property"); + assertThat(property).containsEntry("value", "my value from config server default profile"); + + response = new TestRestTemplate() + .getForEntity("http://localhost:" + this.port + BASE_PATH + "/env/my.prop.label1", Map.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + res = response.getBody(); + assertThat(res).containsKey("propertySources"); + property = (Map) res.get("property"); + assertThat(property).containsEntry("value", "my value from config server label1"); + + response = new TestRestTemplate() + .getForEntity("http://localhost:" + this.port + BASE_PATH + "/env/my.prop.label2", Map.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + res = response.getBody(); + assertThat(res).containsKey("propertySources"); + property = (Map) res.get("property"); + assertThat(property).containsEntry("value", "my value from config server label2"); + } + +} diff --git a/spring-cloud-config-sample/src/test/resources/config/label1/profilesample.yaml b/spring-cloud-config-sample/src/test/resources/config/label1/profilesample.yaml new file mode 100644 index 00000000..18590748 --- /dev/null +++ b/spring-cloud-config-sample/src/test/resources/config/label1/profilesample.yaml @@ -0,0 +1 @@ +my.prop.label1: my value from config server label1 diff --git a/spring-cloud-config-sample/src/test/resources/config/label2/profilesample.yaml b/spring-cloud-config-sample/src/test/resources/config/label2/profilesample.yaml new file mode 100644 index 00000000..fbbfbaaa --- /dev/null +++ b/spring-cloud-config-sample/src/test/resources/config/label2/profilesample.yaml @@ -0,0 +1 @@ +my.prop.label2: my value from config server label2