Spencer Gibb
9 years ago
2 changed files with 138 additions and 0 deletions
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
package org.springframework.cloud.util; |
||||
|
||||
import org.springframework.boot.bind.RelaxedPropertyResolver; |
||||
import org.springframework.core.env.PropertyResolver; |
||||
import org.springframework.util.StringUtils; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
public class IdUtils { |
||||
|
||||
private static final String SEPARATOR = ":"; |
||||
|
||||
public static String getDefaultInstanceId(PropertyResolver resolver) { |
||||
RelaxedPropertyResolver relaxed = new RelaxedPropertyResolver(resolver); |
||||
String vcapInstanceId = relaxed.getProperty("vcap.application.instance_id"); |
||||
if (StringUtils.hasText(vcapInstanceId)) { |
||||
return vcapInstanceId; |
||||
} |
||||
|
||||
String hostname = relaxed.getProperty("spring.cloud.client.hostname"); |
||||
String appName = relaxed.getProperty("spring.application.name"); |
||||
|
||||
String namePart = combineParts(hostname, SEPARATOR, appName); |
||||
|
||||
String indexPart = relaxed.getProperty("spring.application.instance_id", |
||||
relaxed.getProperty("server.port")); |
||||
|
||||
return combineParts(namePart, SEPARATOR, indexPart); |
||||
} |
||||
|
||||
public static String combineParts(String firstPart, String separator, String secondPart) { |
||||
String combined = null; |
||||
if (firstPart != null && secondPart != null) { |
||||
combined = firstPart + separator + secondPart; |
||||
} else if (firstPart != null) { |
||||
combined = firstPart; |
||||
} else if (secondPart != null) { |
||||
combined = secondPart; |
||||
} |
||||
return combined; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,94 @@
@@ -0,0 +1,94 @@
|
||||
package org.springframework.cloud.util; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
import org.junit.After; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.springframework.mock.env.MockEnvironment; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
public class IdUtilsTests { |
||||
|
||||
public static final String DEFAULT_ID = "id1"; |
||||
private MockEnvironment env; |
||||
|
||||
@Before |
||||
public void setup() { |
||||
env = new MockEnvironment(); |
||||
} |
||||
|
||||
@After |
||||
public void destroy() { |
||||
env = null; |
||||
} |
||||
|
||||
@Test |
||||
public void emptyEnvironmentWorks() { |
||||
String instanceId = IdUtils.getDefaultInstanceId(env); |
||||
assertNull("instanceId was not null", instanceId); |
||||
} |
||||
|
||||
@Test |
||||
public void vcapInstanceIdWorks() { |
||||
env.setProperty("vcap.application.instance_id", DEFAULT_ID); |
||||
String instanceId = IdUtils.getDefaultInstanceId(env); |
||||
assertEquals("instanceId was wrong", DEFAULT_ID, instanceId); |
||||
} |
||||
|
||||
@Test |
||||
public void hostnameWorks() { |
||||
env.setProperty("spring.cloud.client.hostname", DEFAULT_ID); |
||||
String instanceId = IdUtils.getDefaultInstanceId(env); |
||||
assertEquals("instanceId was wrong", DEFAULT_ID, instanceId); |
||||
} |
||||
|
||||
@Test |
||||
public void appNameWorks() { |
||||
env.setProperty("spring.application.name", DEFAULT_ID); |
||||
String instanceId = IdUtils.getDefaultInstanceId(env); |
||||
assertEquals("instanceId was wrong", DEFAULT_ID, instanceId); |
||||
} |
||||
|
||||
@Test |
||||
public void hostnameAndAppNameWorks() { |
||||
env.setProperty("spring.application.name", DEFAULT_ID); |
||||
env.setProperty("spring.cloud.client.hostname", DEFAULT_ID+"2"); |
||||
String instanceId = IdUtils.getDefaultInstanceId(env); |
||||
assertEquals("instanceId was wrong", DEFAULT_ID+"2"+":"+DEFAULT_ID, instanceId); |
||||
} |
||||
|
||||
@Test |
||||
public void instanceIdWorks() { |
||||
env.setProperty("spring.cloud.client.hostname", DEFAULT_ID); |
||||
String instanceId = IdUtils.getDefaultInstanceId(env); |
||||
assertEquals("instanceId was wrong", DEFAULT_ID, instanceId); |
||||
} |
||||
|
||||
@Test |
||||
public void portWorks() { |
||||
env.setProperty("spring.application.name", DEFAULT_ID); |
||||
String instanceId = IdUtils.getDefaultInstanceId(env); |
||||
assertEquals("instanceId was wrong", DEFAULT_ID, instanceId); |
||||
} |
||||
|
||||
@Test |
||||
public void appNameAndPortWorks() { |
||||
env.setProperty("spring.application.name", DEFAULT_ID); |
||||
env.setProperty("server.port", "80"); |
||||
String instanceId = IdUtils.getDefaultInstanceId(env); |
||||
assertEquals("instanceId was wrong", DEFAULT_ID+":80", instanceId); |
||||
} |
||||
|
||||
@Test |
||||
public void fullWorks() { |
||||
env.setProperty("spring.cloud.client.hostname", "myhost"); |
||||
env.setProperty("spring.application.name", DEFAULT_ID); |
||||
env.setProperty("server.port", "80"); |
||||
String instanceId = IdUtils.getDefaultInstanceId(env); |
||||
assertEquals("instanceId was wrong", "myhost:"+DEFAULT_ID+":80", instanceId); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue