From 5ed5bc9253fedd5e60969a4b4324a5dbfcf57e90 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Thu, 2 Apr 2020 10:57:00 -0400 Subject: [PATCH] Add utility method to provide a service if to IdUtils (#721) --- .../cloud/commons/util/IdUtils.java | 22 +++++++++++ .../cloud/commons/util/IdUtilsTests.java | 37 +++++++++++++++++++ 2 files changed, 59 insertions(+) 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 dcfa1fc0..3a78f2f3 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 @@ -26,6 +26,11 @@ public final class IdUtils { private static final String SEPARATOR = ":"; + // @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}}"; + + // @checkstyle:on + private IdUtils() { throw new IllegalStateException("Can't instantiate a utility class"); } @@ -55,6 +60,23 @@ public final class IdUtils { return combineParts(namePart, SEPARATOR, indexPart); } + /** + * Gets the resolved service id. + * @param resolver A property resolved + * @return A unique id that can be used to uniquely identify a service + */ + public static String getResolvedServiceId(PropertyResolver resolver) { + return resolver.resolvePlaceholders(getUnresolvedServiceId()); + } + + /** + * Gets an the unresolved service id. + * @return The combination of properties to create a unique service id + */ + public static String getUnresolvedServiceId() { + return DEFAULT_SERVICE_ID_STRING; + } + public static String combineParts(String firstPart, String separator, String secondPart) { String combined = 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 751bcc4d..970760a9 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 @@ -111,4 +111,41 @@ public class IdUtilsTests { .as("instanceId was wrong"); } + @Test + public void testUnresolvedServiceId() { + then(IdUtils.DEFAULT_SERVICE_ID_STRING) + .isEqualTo(IdUtils.getUnresolvedServiceId()); + } + + @Test + public void testServiceIdDefaults() { + this.env.setProperty("cachedrandom.application.value", "123abc"); + then("application:0:123abc").isEqualTo(IdUtils.getResolvedServiceId(this.env)); + } + + @Test + public void testVCAPServiceId() { + env.setProperty("vcap.application.name", "vcapname"); + env.setProperty("vcap.application.instance_index", "vcapindex"); + env.setProperty("vcap.application.instance_id", "vcapid"); + then("vcapname:vcapindex:vcapid").isEqualTo(IdUtils.getResolvedServiceId(env)); + } + + @Test + public void testSpringServiceId() { + env.setProperty("spring.application.name", "springname"); + env.setProperty("spring.application.index", "springindex"); + env.setProperty("cachedrandom.springname.value", "123abc"); + then("springname:springindex:123abc") + .isEqualTo(IdUtils.getResolvedServiceId(env)); + } + + @Test + public void testServerPortServiceId() { + env.setProperty("spring.application.name", "springname"); + env.setProperty("server.port", "1234"); + env.setProperty("cachedrandom.springname.value", "123abc"); + then("springname:1234:123abc").isEqualTo(IdUtils.getResolvedServiceId(env)); + } + }