Commit ac77c8f8 authored by Yiming Liu's avatar Yiming Liu

Fix Eureka Service Discovery

parent fcd61233
package com.ctrip.apollo.metaserver; package com.ctrip.apollo.metaserver;
import java.util.List;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.ConfigurableApplicationContext;
import com.ctrip.apollo.metaserver.service.DiscoveryService;
import com.netflix.appinfo.InstanceInfo;
@SpringBootApplication @SpringBootApplication
@EnableEurekaServer @EnableEurekaServer
...@@ -17,10 +11,6 @@ import com.netflix.appinfo.InstanceInfo; ...@@ -17,10 +11,6 @@ import com.netflix.appinfo.InstanceInfo;
public class MetaServerApplication { public class MetaServerApplication {
public static void main(String[] args) { public static void main(String[] args) {
ConfigurableApplicationContext context =
new SpringApplicationBuilder(MetaServerApplication.class).web(true).run(args); new SpringApplicationBuilder(MetaServerApplication.class).web(true).run(args);
DiscoveryService discoveryService = context.getBean(DiscoveryService.class);
List<InstanceInfo> instances = discoveryService.getMetaServerServiceInstance();
System.out.println(instances);
} }
} }
package com.ctrip.apollo.metaserver.service; package com.ctrip.apollo.metaserver.service;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -18,12 +19,12 @@ public class DiscoveryService { ...@@ -18,12 +19,12 @@ public class DiscoveryService {
public List<InstanceInfo> getConfigServerServiceInstance() { public List<InstanceInfo> getConfigServerServiceInstance() {
Application application = eurekaClient.getApplication(ServiceIdConsts.APOLLO_CONFIGSERVER); Application application = eurekaClient.getApplication(ServiceIdConsts.APOLLO_CONFIGSERVER);
return application.getInstances(); return application != null ? application.getInstances() : new ArrayList<>();
} }
public List<InstanceInfo> getMetaServerServiceInstance() { public List<InstanceInfo> getMetaServerServiceInstance() {
Application application = eurekaClient.getApplication(ServiceIdConsts.APOLLO_METASERVER); Application application = eurekaClient.getApplication(ServiceIdConsts.APOLLO_METASERVER);
return application.getInstances(); return application != null ? application.getInstances() : new ArrayList<>();
} }
} }
...@@ -4,9 +4,7 @@ server: ...@@ -4,9 +4,7 @@ server:
eureka: eureka:
instance: instance:
hostname: localhost hostname: localhost
preferIpAddress: true
client: client:
fetchRegistry: false
serviceUrl: serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
healthcheck: healthcheck:
......
package com.ctrip.apollo.metaserver.controller;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.web.client.RestTemplate;
import com.ctrip.apollo.core.ServiceIdConsts;
import com.ctrip.apollo.metaserver.AbstractMetaServerTest;
import com.netflix.appinfo.InstanceInfo;
@WebIntegrationTest
public class DiscoveryControllerTest extends AbstractMetaServerTest {
RestTemplate restTemplate = new TestRestTemplate();
@Value("${local.server.port}")
String serverPort;
@Test
public void testGetMetaServerServices() throws InterruptedException, URISyntaxException {
// Wait Eureka Client to fresh meta
Thread.sleep(5000);
URI uri = new URI("http://localhost:" + serverPort + "/services/meta");
InstanceInfo[] serviceInstances = restTemplate.getForObject(uri, InstanceInfo[].class);
Assert.assertEquals(1, serviceInstances.length);
Assert.assertEquals(ServiceIdConsts.APOLLO_METASERVER,
serviceInstances[0].getAppName().toLowerCase());
}
}
package com.ctrip.apollo.metaserver.service;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctrip.apollo.metaserver.AbstractMetaServerTest;
import com.netflix.appinfo.InstanceInfo;
public class DiscoveryServiceTest extends AbstractMetaServerTest {
@Autowired
private DiscoveryService discoveryService;
@Test
public void testGetLocalMetaServerServices() {
List<InstanceInfo> instances = discoveryService.getMetaServerServiceInstance();
System.out.println(instances);
}
}
...@@ -4,10 +4,11 @@ server: ...@@ -4,10 +4,11 @@ server:
eureka: eureka:
instance: instance:
hostname: localhost hostname: localhost
preferIpAddress: true leaseRenewalIntervalInSeconds: 1
client: client:
fetchRegistry: false # initialInstanceInfoReplicationIntervalSeconds: 1
registryFetchIntervalSeconds: 2
serviceUrl: serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ defaultZone: http://${eureka.instance.hostname}:${server.port:8761}/eureka/
healthcheck: healthcheck:
enabled: true enabled: true
\ No newline at end of file
...@@ -26,7 +26,7 @@ public class AppControllerTest extends AbstractPortalTest { ...@@ -26,7 +26,7 @@ public class AppControllerTest extends AbstractPortalTest {
@Autowired @Autowired
AppRepository appRepository; AppRepository appRepository;
@Value("${server.port}") @Value("${local.server.port}")
String serverPort; String serverPort;
@Test @Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment