Skip to content

Add a utility method to provide a default service id #721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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 @@ -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");
}
Expand Down Expand Up @@ -55,6 +60,23 @@ public static String getDefaultInstanceId(PropertyResolver resolver,
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,41 @@ public void fullWorks() {
.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));
Copy link
Member

Choose a reason for hiding this comment

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

This will help when server.port=0.

}

}