Skip to content

Commit 1d4fbef

Browse files
author
Ryan Baxter
committed
Add utility method to provide a service if to IdUtils
1 parent bea81f7 commit 1d4fbef

File tree

2 files changed

+59
-0
lines changed
  • spring-cloud-commons/src

2 files changed

+59
-0
lines changed

spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/IdUtils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public final class IdUtils {
2626

2727
private static final String SEPARATOR = ":";
2828

29+
// @checkstyle:off
30+
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}}";
31+
32+
// @checkstyle:on
33+
2934
private IdUtils() {
3035
throw new IllegalStateException("Can't instantiate a utility class");
3136
}
@@ -55,6 +60,23 @@ public static String getDefaultInstanceId(PropertyResolver resolver,
5560
return combineParts(namePart, SEPARATOR, indexPart);
5661
}
5762

63+
/**
64+
* Gets the resolved service id.
65+
* @param resolver A property resolved
66+
* @return A unique id that can be used to uniquely identify a service
67+
*/
68+
public static String getResolvedServiceId(PropertyResolver resolver) {
69+
return resolver.resolvePlaceholders(getUnresolvedServiceId());
70+
}
71+
72+
/**
73+
* Gets an the unresolved service id.
74+
* @return The combination of properties to create a unique service id
75+
*/
76+
public static String getUnresolvedServiceId() {
77+
return DEFAULT_SERVICE_ID_STRING;
78+
}
79+
5880
public static String combineParts(String firstPart, String separator,
5981
String secondPart) {
6082
String combined = null;

spring-cloud-commons/src/test/java/org/springframework/cloud/commons/util/IdUtilsTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,41 @@ public void fullWorks() {
111111
.as("instanceId was wrong");
112112
}
113113

114+
@Test
115+
public void testUnresolvedServiceId() {
116+
then(IdUtils.DEFAULT_SERVICE_ID_STRING)
117+
.isEqualTo(IdUtils.getUnresolvedServiceId());
118+
}
119+
120+
@Test
121+
public void testServiceIdDefaults() {
122+
this.env.setProperty("cachedrandom.application.value", "123abc");
123+
then("application:0:123abc").isEqualTo(IdUtils.getResolvedServiceId(this.env));
124+
}
125+
126+
@Test
127+
public void testVCAPServiceId() {
128+
env.setProperty("vcap.application.name", "vcapname");
129+
env.setProperty("vcap.application.instance_index", "vcapindex");
130+
env.setProperty("vcap.application.instance_id", "vcapid");
131+
then("vcapname:vcapindex:vcapid").isEqualTo(IdUtils.getResolvedServiceId(env));
132+
}
133+
134+
@Test
135+
public void testSpringServiceId() {
136+
env.setProperty("spring.application.name", "springname");
137+
env.setProperty("spring.application.index", "springindex");
138+
env.setProperty("cachedrandom.springname.value", "123abc");
139+
then("springname:springindex:123abc")
140+
.isEqualTo(IdUtils.getResolvedServiceId(env));
141+
}
142+
143+
@Test
144+
public void testServerPortServiceId() {
145+
env.setProperty("spring.application.name", "springname");
146+
env.setProperty("server.port", "1234");
147+
env.setProperty("cachedrandom.springname.value", "123abc");
148+
then("springname:1234:123abc").isEqualTo(IdUtils.getResolvedServiceId(env));
149+
}
150+
114151
}

0 commit comments

Comments
 (0)