diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/IdUtils.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/IdUtils.java index 37e8c56a..e7bf3c10 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/IdUtils.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/IdUtils.java @@ -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 without active profiles. + * + * @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) { diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/util/IdUtilsTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/util/IdUtilsTests.java index 29adf3f5..0e5e7738 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/util/IdUtilsTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/util/IdUtilsTests.java @@ -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)); + } }