Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ public class ConfigClientProperties {
*/
public static final String PREFIX = "spring.cloud.config";

/**
* Default application name.
*/
public static final String DEFAULT_APPLICATION = "application";

/**
* Placeholder string that allows ${spring.cloud.config.name} to override
* ${spring.application.name:application}.
*/
public static final String NAME_PLACEHOLDER = "${" + ConfigClientProperties.PREFIX
+ ".name:${spring.application.name:application}}";
+ ".name:${spring.application.name:" + DEFAULT_APPLICATION + "}}";

/**
* Name of config discovery enabled property.
Expand Down Expand Up @@ -94,12 +99,12 @@ public class ConfigClientProperties {
/**
* Name of application used to fetch remote properties.
*/
@Value("${spring.application.name:application}")
@Value("${spring.application.name:" + DEFAULT_APPLICATION + "}")
private String name;

/**
* The label name to use to pull remote configuration properties. The default is set
* on the server (generally "master" for a git based server).
* on the server (generally "main" for a git based server).
*/
private String label;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.cloud.config.client;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
Expand All @@ -25,7 +26,13 @@
import org.springframework.boot.context.config.ConfigDataResource;
import org.springframework.boot.context.config.Profiles;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import static org.springframework.cloud.config.client.ConfigClientProperties.DEFAULT_APPLICATION;
import static org.springframework.cloud.config.client.ConfigClientProperties.DEFAULT_PROFILE;

public class ConfigServerConfigDataResource extends ConfigDataResource {

Expand Down Expand Up @@ -95,6 +102,41 @@ public void setRetryProperties(RetryProperties retryProperties) {
this.retryProperties = retryProperties;
}

private String getApplicationName() {
return ObjectUtils.isEmpty(this.properties.getName()) ? DEFAULT_APPLICATION : this.getProperties().getName();
}

private String getProfilesForEquals() {
return ObjectUtils.isEmpty(this.getProfiles()) ? DEFAULT_PROFILE : this.getProfiles();
}

private boolean urisEqual(String[] thatUris) {
if (this.properties.getUri().length != thatUris.length) {
return false;
}
for (String uri : this.properties.getUri()) {
if (Arrays.stream(thatUris).noneMatch(thatUri -> uriEqual(uri, thatUri))) {
return false;
}
}
return true;
}

private boolean uriEqual(String thisUriString, String thatUriString) {
UriComponents thisUri = UriComponentsBuilder.fromHttpUrl(thisUriString).build();
UriComponents thatUri = UriComponentsBuilder.fromHttpUrl(thatUriString).build();
return Objects.equals(thisUri.getHost(), thatUri.getHost())
&& Objects.equals(thisUri.getPort(), thatUri.getPort())
&& Objects.equals(thisUri.getPath(), thatUri.getPath());
}

private int urisHashCode(String[] uris) {
return Arrays.stream(uris).mapToInt(uriString -> {
UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(uriString).build();
return Objects.hash(uriComponents.getHost(), uriComponents.getPath(), uriComponents.getPort());
}).sum();
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -104,19 +146,25 @@ public boolean equals(Object o) {
return false;
}
ConfigServerConfigDataResource that = (ConfigServerConfigDataResource) o;
return Objects.equals(this.properties, that.properties) && Objects.equals(this.optional, that.optional)
&& Objects.equals(this.profiles, that.profiles);
return urisEqual(that.properties.getUri())
&& Objects.equals(this.getApplicationName(), that.getApplicationName())
&& Objects.equals(this.properties.getLabel(), that.properties.getLabel())
&& Objects.equals(this.getProfilesForEquals(), that.getProfilesForEquals())
&& Objects.equals(this.optional, that.optional);
}

@Override
public int hashCode() {
return Objects.hash(this.properties, this.optional, this.profiles);
String[] uris = properties.getUri();
String name = properties.getName();
String label = properties.getLabel();
return Objects.hash(urisHashCode(uris), name, label, optional, getProfilesForEquals());
}

@Override
public String toString() {
return new ToStringCreator(this).append("uris", properties.getUri()).append("optional", optional)
.append("profiles", getAcceptedProfiles()).toString();
.append("profiles", getProfiles()).toString();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* spring.application.name will be set.
*
* At this point Boot has collected all active profiles so it will load all
* spring.config.import statements again with active profiles. The subsequent 2 calls then
* spring.config.import statements again with active profiles. The subsequent call then
* will not have spring.application.name set in the context so the config server config
* data loader will make a request to the config server with the correct application name.
*
Expand All @@ -73,7 +73,7 @@ void context() {
verify(rest).exchange(eq("http://localhost:8888/{name}/{profile}"), eq(HttpMethod.GET),
ArgumentMatchers.any(HttpEntity.class), eq(Environment.class), eq("application"),
ArgumentMatchers.<String>any());
verify(rest, times(2)).exchange(eq("http://localhost:8888/{name}/{profile}"), eq(HttpMethod.GET),
verify(rest, times(1)).exchange(eq("http://localhost:8888/{name}/{profile}"), eq(HttpMethod.GET),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, we were testing for the bug 🤦🏻‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well to be fair, we didn't know how this should have worked ;)

ArgumentMatchers.any(HttpEntity.class), eq(Environment.class), eq("foo"),
ArgumentMatchers.<String>any());
}
Expand Down
Loading