mirror of
https://github.com/spring-cloud/spring-cloud-netflix.git
synced 2026-09-17 15:49:00 +00:00
Added the instance id to the EurekaServiceInstance, EurekaRegistration and RibbonLoadBalancerClient (#3215)
* Added the instance id to the `EurekaServiceInstance`, `EurekaRegistration` and `RibbonLoadBalancerClient` * Fixed failing test
This commit is contained in:
+6
@@ -39,6 +39,7 @@ import static com.netflix.appinfo.InstanceInfo.PortType.SECURE;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
public class EurekaDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
@@ -85,6 +86,11 @@ public class EurekaDiscoveryClient implements DiscoveryClient {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInstanceId() {
|
||||
return this.instance.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return this.instance.getAppName();
|
||||
|
||||
+6
-2
@@ -17,8 +17,6 @@
|
||||
|
||||
package org.springframework.cloud.netflix.eureka.serviceregistry;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
@@ -44,6 +42,7 @@ import com.netflix.discovery.EurekaClientConfig;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
public class EurekaRegistration implements Registration {
|
||||
private static final Log log = LogFactory.getLog(EurekaRegistration.class);
|
||||
@@ -117,6 +116,11 @@ public class EurekaRegistration implements Registration {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInstanceId() {
|
||||
return this.instanceConfig.getInstanceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return this.instanceConfig.getAppname();
|
||||
|
||||
+21
@@ -42,6 +42,7 @@ import static org.junit.Assert.assertTrue;
|
||||
* @author Dave Syer
|
||||
* @author Spencer Gibb
|
||||
* @author Ryan Baxter
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
public class EurekaInstanceConfigBeanTests {
|
||||
|
||||
@@ -207,6 +208,26 @@ public class EurekaInstanceConfigBeanTests {
|
||||
assertEquals("default secure virtual hostname is wrong", "unknown", getInstanceConfig().getSecureVirtualHostName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomInstanceId() throws Exception {
|
||||
TestPropertyValues.of("eureka.instance.instanceId=myinstance").applyTo(this.context);
|
||||
setupContext();
|
||||
assertEquals("instance id is wrong", "myinstance", getInstanceConfig().getInstanceId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomInstanceIdWithMetadata() throws Exception {
|
||||
TestPropertyValues.of("eureka.instance.metadataMap.instanceId=myinstance").applyTo(this.context);
|
||||
setupContext();
|
||||
assertEquals("instance id is wrong", "myinstance", getInstanceConfig().getInstanceId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultInstanceId() throws Exception {
|
||||
setupContext();
|
||||
assertEquals("default instance id is wrong", null, getInstanceConfig().getInstanceId());
|
||||
}
|
||||
|
||||
private void setupContext() {
|
||||
this.context.register(PropertyPlaceholderAutoConfiguration.class,
|
||||
TestConfiguration.class);
|
||||
|
||||
+5
@@ -40,6 +40,7 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
public class EurekaServiceRegistryTests {
|
||||
|
||||
@@ -99,6 +100,8 @@ public class EurekaServiceRegistryTests {
|
||||
|
||||
Object status = registry.getStatus(registration);
|
||||
|
||||
assertThat(registration.getInstanceId()).isEqualTo("1234");
|
||||
|
||||
assertThat(status).isInstanceOf(Map.class);
|
||||
|
||||
Map<Object, Object> map = (Map<Object, Object>) status;
|
||||
@@ -133,6 +136,8 @@ public class EurekaServiceRegistryTests {
|
||||
|
||||
Object status = registry.getStatus(registration);
|
||||
|
||||
assertThat(registration.getInstanceId()).isEqualTo("1234");
|
||||
|
||||
assertThat(status).isInstanceOf(Map.class);
|
||||
|
||||
Map<Object, Object> map = (Map<Object, Object>) status;
|
||||
|
||||
+5
@@ -197,6 +197,11 @@ public class RibbonLoadBalancerClient implements LoadBalancerClient {
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInstanceId() {
|
||||
return this.server.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return this.serviceId;
|
||||
|
||||
+3
@@ -55,6 +55,7 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
public class RibbonLoadBalancerClientTests {
|
||||
|
||||
@@ -324,6 +325,8 @@ public class RibbonLoadBalancerClientTests {
|
||||
protected void assertServiceInstance(RibbonServer ribbonServer,
|
||||
ServiceInstance instance) {
|
||||
assertNotNull("instance was null", instance);
|
||||
assertEquals("instanceId was wrong", ribbonServer.getInstanceId(),
|
||||
instance.getInstanceId());
|
||||
assertEquals("serviceId was wrong", ribbonServer.getServiceId(),
|
||||
instance.getServiceId());
|
||||
assertEquals("host was wrong", ribbonServer.getHost(), instance.getHost());
|
||||
|
||||
Reference in New Issue
Block a user