提交 ac77c8f8 编写于 作者: Y Yiming Liu

Fix Eureka Service Discovery

上级 fcd61233
package com.ctrip.apollo.metaserver;
import java.util.List;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
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
@EnableEurekaServer
......@@ -17,10 +11,6 @@ import com.netflix.appinfo.InstanceInfo;
public class MetaServerApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context =
new SpringApplicationBuilder(MetaServerApplication.class).web(true).run(args);
DiscoveryService discoveryService = context.getBean(DiscoveryService.class);
List<InstanceInfo> instances = discoveryService.getMetaServerServiceInstance();
System.out.println(instances);
new SpringApplicationBuilder(MetaServerApplication.class).web(true).run(args);
}
}
package com.ctrip.apollo.metaserver.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -18,12 +19,12 @@ public class DiscoveryService {
public List<InstanceInfo> getConfigServerServiceInstance() {
Application application = eurekaClient.getApplication(ServiceIdConsts.APOLLO_CONFIGSERVER);
return application.getInstances();
return application != null ? application.getInstances() : new ArrayList<>();
}
public List<InstanceInfo> getMetaServerServiceInstance() {
Application application = eurekaClient.getApplication(ServiceIdConsts.APOLLO_METASERVER);
return application.getInstances();
return application != null ? application.getInstances() : new ArrayList<>();
}
}
......@@ -4,10 +4,8 @@ server:
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
healthcheck:
enabled: true
\ No newline at end of file
enabled: true
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:
eureka:
instance:
hostname: localhost
preferIpAddress: true
leaseRenewalIntervalInSeconds: 1
client:
fetchRegistry: false
# initialInstanceInfoReplicationIntervalSeconds: 1
registryFetchIntervalSeconds: 2
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://${eureka.instance.hostname}:${server.port:8761}/eureka/
healthcheck:
enabled: true
\ No newline at end of file
......@@ -26,7 +26,7 @@ public class AppControllerTest extends AbstractPortalTest {
@Autowired
AppRepository appRepository;
@Value("${server.port}")
@Value("${local.server.port}")
String serverPort;
@Test
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册