Consider spring.profiles.active when resolving service ID. (#1064)
New constant IdUtils.DEFAULT_SERVICE_ID_WITH_ACTIVE_PROFILES_STRING refers to profiles in second position. IdUtils.getResolvedServiceId chooses new constant as template only if profiles are active, and thus remains backwards-compatible. Add unit tests for new behaviour. Fixes gh-930.
This commit is contained in:
@@ -29,6 +29,7 @@ public final class IdUtils {
|
||||
// @checkstyle:off
|
||||
public static final String DEFAULT_SERVICE_ID_STRING = "${vcap.application.name:${spring.application.name:application}}:${vcap.application.instance_index:${spring.application.index:${local.server.port:${server.port:0}}}}:${vcap.application.instance_id:${cachedrandom.${vcap.application.name:${spring.application.name:application}}.value}}";
|
||||
|
||||
public static final String DEFAULT_SERVICE_ID_WITH_ACTIVE_PROFILES_STRING = "${vcap.application.name:${spring.application.name:application}:${spring.profiles.active}}:${vcap.application.instance_index:${spring.application.index:${local.server.port:${server.port:0}}}}:${vcap.application.instance_id:${cachedrandom.${vcap.application.name:${spring.application.name:application}}.value}}";
|
||||
// @checkstyle:on
|
||||
|
||||
private IdUtils() {
|
||||
@@ -64,17 +65,35 @@ public final class IdUtils {
|
||||
* @return A unique id that can be used to uniquely identify a service
|
||||
*/
|
||||
public static String getResolvedServiceId(PropertyResolver resolver) {
|
||||
return resolver.resolvePlaceholders(getUnresolvedServiceId());
|
||||
final String unresolvedServiceId;
|
||||
// addition of active profiles at the 2nd position of the service ID breaks backwards-compatibility,
|
||||
// so we fall back to the old implementation in case no profiles are active
|
||||
if (StringUtils.hasText(resolver.getProperty("spring.profiles.active"))) {
|
||||
unresolvedServiceId = getUnresolvedServiceIdWithActiveProfiles();
|
||||
} else {
|
||||
unresolvedServiceId = getUnresolvedServiceId();
|
||||
}
|
||||
return resolver.resolvePlaceholders(unresolvedServiceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an the unresolved service id.
|
||||
* @return The combination of properties to create a unique service id
|
||||
* Gets the unresolved template for the service id <i>without active profiles.</i>
|
||||
*
|
||||
* @return The combination of properties to create a unique service id.
|
||||
*/
|
||||
public static String getUnresolvedServiceId() {
|
||||
return DEFAULT_SERVICE_ID_STRING;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the unresolved template for the service id including active profiles.
|
||||
*
|
||||
* @return The combination of properties to create a unique service id.
|
||||
*/
|
||||
public static String getUnresolvedServiceIdWithActiveProfiles() {
|
||||
return DEFAULT_SERVICE_ID_WITH_ACTIVE_PROFILES_STRING;
|
||||
}
|
||||
|
||||
public static String combineParts(String firstPart, String separator, String secondPart) {
|
||||
String combined = null;
|
||||
if (firstPart != null && secondPart != null) {
|
||||
|
||||
@@ -114,6 +114,11 @@ public class IdUtilsTests {
|
||||
then(IdUtils.DEFAULT_SERVICE_ID_STRING).isEqualTo(IdUtils.getUnresolvedServiceId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnresolvedServiceIdWithActiveProfiles() {
|
||||
then(IdUtils.DEFAULT_SERVICE_ID_WITH_ACTIVE_PROFILES_STRING).isEqualTo(IdUtils.getUnresolvedServiceIdWithActiveProfiles());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServiceIdDefaults() {
|
||||
this.env.setProperty("cachedrandom.application.value", "123abc");
|
||||
@@ -142,6 +147,36 @@ public class IdUtilsTests {
|
||||
env.setProperty("server.port", "1234");
|
||||
env.setProperty("cachedrandom.springname.value", "123abc");
|
||||
then("springname:1234:123abc").isEqualTo(IdUtils.getResolvedServiceId(env));
|
||||
|
||||
// ensure that for spring.profiles.active, empty string value is equivalent to not being set at all
|
||||
env.setProperty("spring.profiles.active", "");
|
||||
then("springname:1234:123abc").isEqualTo(IdUtils.getResolvedServiceId(env));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVCAPServiceIdWithActiveProfile() {
|
||||
env.setProperty("vcap.application.name", "vcapname");
|
||||
env.setProperty("vcap.application.instance_index", "vcapindex");
|
||||
env.setProperty("vcap.application.instance_id", "vcapid");
|
||||
env.setProperty("spring.profiles.active", "123profile");
|
||||
then("vcapname:vcapindex:vcapid").isEqualTo(IdUtils.getResolvedServiceId(env));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpringServiceIdWithActiveProfile() {
|
||||
env.setProperty("spring.application.name", "springname");
|
||||
env.setProperty("spring.application.index", "springindex");
|
||||
env.setProperty("cachedrandom.springname.value", "123abc");
|
||||
env.setProperty("spring.profiles.active", "123profile");
|
||||
then("springname:123profile:springindex:123abc").isEqualTo(IdUtils.getResolvedServiceId(env));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServerPortServiceIdWithActiveProfile() {
|
||||
env.setProperty("spring.application.name", "springname");
|
||||
env.setProperty("server.port", "1234");
|
||||
env.setProperty("cachedrandom.springname.value", "123abc");
|
||||
env.setProperty("spring.profiles.active", "123profile");
|
||||
then("springname:123profile:1234:123abc").isEqualTo(IdUtils.getResolvedServiceId(env));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user