Sfoglia il codice sorgente

revert to eureka client implementation to get the correct home page url

Jason Song 4 anni fa
parent
commit
5e0ab3cc02

+ 16 - 18
apollo-configservice/src/main/java/com/ctrip/framework/apollo/metaservice/service/DefaultDiscoveryService.java

@@ -3,47 +3,45 @@ package com.ctrip.framework.apollo.metaservice.service;
 import com.ctrip.framework.apollo.common.condition.ConditionalOnMissingProfile;
 import com.ctrip.framework.apollo.core.dto.ServiceDTO;
 import com.ctrip.framework.apollo.tracer.Tracer;
+import com.netflix.appinfo.InstanceInfo;
+import com.netflix.discovery.EurekaClient;
+import com.netflix.discovery.shared.Application;
 import java.util.Collections;
 import java.util.List;
 import java.util.function.Function;
 import java.util.stream.Collectors;
-import org.springframework.cloud.client.ServiceInstance;
-import org.springframework.cloud.client.discovery.DiscoveryClient;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
+/**
+ * Default discovery service for Eureka
+ */
 @Service
 @ConditionalOnMissingProfile({"kubernetes"})
 public class DefaultDiscoveryService implements DiscoveryService {
 
-  private final DiscoveryClient discoveryClient;
+  private final EurekaClient eurekaClient;
 
-  public DefaultDiscoveryService(final DiscoveryClient discoveryClient) {
-    this.discoveryClient = discoveryClient;
+  public DefaultDiscoveryService(final EurekaClient eurekaClient) {
+    this.eurekaClient = eurekaClient;
   }
 
   @Override
   public List<ServiceDTO> getServiceInstances(String serviceId) {
-    List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
-    if (CollectionUtils.isEmpty(instances)) {
+    Application application = eurekaClient.getApplication(serviceId);
+    if (application == null || CollectionUtils.isEmpty(application.getInstances())) {
       Tracer.logEvent("Apollo.Discovery.NotFound", serviceId);
       return Collections.emptyList();
     }
-    return instances.stream().map(instanceInfoToServiceDTOFunc)
+    return application.getInstances().stream().map(instanceInfoToServiceDTOFunc)
         .collect(Collectors.toList());
   }
 
-  private static Function<ServiceInstance, ServiceDTO> instanceInfoToServiceDTOFunc = instance -> {
+  private static final Function<InstanceInfo, ServiceDTO> instanceInfoToServiceDTOFunc = instance -> {
     ServiceDTO service = new ServiceDTO();
-    service.setAppName(instance.getServiceId());
-    service.setInstanceId(
-        String.format("%s:%s:%s", instance.getHost(), instance.getServiceId(), instance.getPort()));
-    String uri = instance.getUri().toString();
-    if (!uri.endsWith("/")) {
-      uri += "/";
-    }
-    service.setHomepageUrl(uri);
+    service.setAppName(instance.getAppName());
+    service.setInstanceId(instance.getInstanceId());
+    service.setHomepageUrl(instance.getHomePageUrl());
     return service;
   };
-
 }

+ 30 - 35
apollo-configservice/src/test/java/com/ctrip/framework/apollo/metaservice/service/DefaultDiscoveryServiceTest.java

@@ -6,7 +6,9 @@ import static org.mockito.Mockito.when;
 
 import com.ctrip.framework.apollo.core.dto.ServiceDTO;
 import com.google.common.collect.Lists;
-import java.net.URI;
+import com.netflix.appinfo.InstanceInfo;
+import com.netflix.discovery.EurekaClient;
+import com.netflix.discovery.shared.Application;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.List;
@@ -15,14 +17,15 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
-import org.springframework.cloud.client.ServiceInstance;
-import org.springframework.cloud.client.discovery.DiscoveryClient;
 
 @RunWith(MockitoJUnitRunner.class)
 public class DefaultDiscoveryServiceTest {
 
   @Mock
-  private DiscoveryClient discoveryClient;
+  private EurekaClient eurekaClient;
+
+  @Mock
+  private Application someApplication;
 
   private DefaultDiscoveryService defaultDiscoveryService;
 
@@ -30,68 +33,60 @@ public class DefaultDiscoveryServiceTest {
 
   @Before
   public void setUp() throws Exception {
-    defaultDiscoveryService = new DefaultDiscoveryService(discoveryClient);
+    defaultDiscoveryService = new DefaultDiscoveryService(eurekaClient);
 
     someServiceId = "someServiceId";
   }
 
   @Test
   public void testGetServiceInstancesWithNullInstances() {
-    when(discoveryClient.getInstances(someServiceId)).thenReturn(null);
+    when(eurekaClient.getApplication(someServiceId)).thenReturn(null);
 
     assertTrue(defaultDiscoveryService.getServiceInstances(someServiceId).isEmpty());
   }
 
   @Test
   public void testGetServiceInstancesWithEmptyInstances() {
-    when(discoveryClient.getInstances(someServiceId)).thenReturn(new ArrayList<>());
+    when(eurekaClient.getApplication(someServiceId)).thenReturn(someApplication);
+    when(someApplication.getInstances()).thenReturn(new ArrayList<>());
 
     assertTrue(defaultDiscoveryService.getServiceInstances(someServiceId).isEmpty());
   }
 
   @Test
   public void testGetServiceInstances() throws URISyntaxException {
-    String someHost = "1.2.3.4";
-    int somePort = 8080;
-    String someUri = String.format("http://%s:%s/some-path/", someHost, somePort);
-    ServiceInstance someServiceInstance = mockServiceInstance(someServiceId, someHost, somePort,
+    String someUri = "http://1.2.3.4:8080/some-path/";
+    String someInstanceId = "someInstanceId";
+    InstanceInfo someServiceInstance = mockServiceInstance(someServiceId, someInstanceId,
         someUri);
 
-    String anotherHost = "2.3.4.5";
-    int anotherPort = 9090;
-    String anotherUri = String.format("http://%s:%s/some-path-with-no-slash", anotherHost, anotherPort);
-    ServiceInstance anotherServiceInstance = mockServiceInstance(someServiceId, anotherHost, anotherPort,
+    String anotherUri = "http://2.3.4.5:9090/anotherPath";
+    String anotherInstanceId = "anotherInstanceId";
+    InstanceInfo anotherServiceInstance = mockServiceInstance(someServiceId, anotherInstanceId,
         anotherUri);
 
-    when(discoveryClient.getInstances(someServiceId))
+    when(eurekaClient.getApplication(someServiceId)).thenReturn(someApplication);
+    when(someApplication.getInstances())
         .thenReturn(Lists.newArrayList(someServiceInstance, anotherServiceInstance));
 
     List<ServiceDTO> serviceDTOList = defaultDiscoveryService.getServiceInstances(someServiceId);
 
     assertEquals(2, serviceDTOList.size());
-    check(someServiceInstance, serviceDTOList.get(0), false);
-    check(anotherServiceInstance, serviceDTOList.get(1), true);
+    check(someServiceInstance, serviceDTOList.get(0));
+    check(anotherServiceInstance, serviceDTOList.get(1));
   }
 
-  private void check(ServiceInstance serviceInstance, ServiceDTO serviceDTO, boolean appendSlashToUri) {
-    assertEquals(serviceInstance.getServiceId(), serviceDTO.getAppName());
-    assertEquals(serviceDTO.getInstanceId(), String
-        .format("%s:%s:%s", serviceInstance.getHost(), serviceInstance.getServiceId(),
-            serviceInstance.getPort()));
-    if (appendSlashToUri) {
-      assertEquals(serviceInstance.getUri().toString() + "/", serviceDTO.getHomepageUrl());
-    } else {
-      assertEquals(serviceInstance.getUri().toString(), serviceDTO.getHomepageUrl());
-    }
+  private void check(InstanceInfo serviceInstance, ServiceDTO serviceDTO) {
+    assertEquals(serviceInstance.getAppName(), serviceDTO.getAppName());
+    assertEquals(serviceInstance.getInstanceId(), serviceDTO.getInstanceId());
+    assertEquals(serviceInstance.getHomePageUrl(), serviceDTO.getHomepageUrl());
   }
 
-  private ServiceInstance mockServiceInstance(String serviceId, String host, int port, String uri)
-      throws URISyntaxException {
-    ServiceInstance serviceInstance = mock(ServiceInstance.class);
-    when(serviceInstance.getServiceId()).thenReturn(serviceId);
-    when(serviceInstance.getHost()).thenReturn(host);
-    when(serviceInstance.getPort()).thenReturn(port);
-    when(serviceInstance.getUri()).thenReturn(new URI(uri));
+  private InstanceInfo mockServiceInstance(String serviceId, String instanceId, String homePageUrl) {
+    InstanceInfo serviceInstance = mock(InstanceInfo.class);
+    when(serviceInstance.getAppName()).thenReturn(serviceId);
+    when(serviceInstance.getInstanceId()).thenReturn(instanceId);
+    when(serviceInstance.getHomePageUrl()).thenReturn(homePageUrl);
 
     return serviceInstance;
   }