Skip to content

Commit

Permalink
revert to eureka client implementation to get the correct home page url
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodyiam committed Jul 11, 2020
1 parent 52b3f6f commit 8604965
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,45 @@
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;
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

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;
Expand All @@ -15,83 +17,76 @@
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;

private String someServiceId;

@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;
}
Expand Down

0 comments on commit 8604965

Please sign in to comment.