Commit c5d37036 by Yiming Liu

Add Eureka Service Discovery

parent ac0e32ed
...@@ -21,25 +21,13 @@ ...@@ -21,25 +21,13 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-zookeeper-discovery</artifactId> <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>
</dependency> </dependency>
...@@ -48,11 +36,6 @@ ...@@ -48,11 +36,6 @@
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
......
package com.ctrip.apollo.configserver.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.core.ServiceIdConsts;
@Service
public class DiscoveryService {
@Autowired
private DiscoveryClient discoveryClient;
public List<ServiceInstance> getConfigServerServiceInstances() {
List<ServiceInstance> instances =
discoveryClient.getInstances(ServiceIdConsts.APOLLO_CONFIGSERVER);
return instances;
}
public List<ServiceInstance> getMetaServerServiceInstances() {
List<ServiceInstance> instances =
discoveryClient.getInstances(ServiceIdConsts.APOLLO_METASERVER);
return instances;
}
}
spring: eureka:
cloud: instance:
zookeeper: hostname: localhost
connectString: 10.3.2.57:2181,10.3.2.58:2181,10.3.2.59:2181 preferIpAddress: true
\ No newline at end of file client:
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
healthcheck:
enabled: true
\ No newline at end of file
package com.ctrip.apollo.configserver; package com.ctrip.apollo.configserver;
import java.io.IOException;
import org.apache.curator.test.TestingServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ConfigServerApplicationTestConfiguration.class) @SpringApplicationConfiguration(classes = ConfigServerApplication.class)
public abstract class AbstractConfigServerTest { public abstract class AbstractConfigServerTest {
private static TestingServer zkTestServer;
@BeforeClass
public static void beforeClass() throws Exception {
zkTestServer = new TestingServer(2181, false);
zkTestServer.start();
System.out.format("embedded zookeeper is up %s%n", zkTestServer.getConnectString());
}
@AfterClass
public static void afterClass() throws IOException {
if (zkTestServer != null) {
zkTestServer.close();
}
}
} }
package com.ctrip.apollo.configserver;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigServerApplicationTestConfiguration {
}
package com.ctrip.apollo.metaserver.service; package com.ctrip.apollo.configserver.service;
import java.util.List; import java.util.List;
...@@ -6,9 +6,9 @@ import org.junit.Test; ...@@ -6,9 +6,9 @@ import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
import com.ctrip.apollo.metaserver.AbstractMetaServerTest; import com.ctrip.apollo.configserver.AbstractConfigServerTest;
public class ServiceDiscoveryTest extends AbstractMetaServerTest { public class DiscoveryServiceTest extends AbstractConfigServerTest {
@Autowired @Autowired
private DiscoveryService discoveryService; private DiscoveryService discoveryService;
......
...@@ -6,8 +6,8 @@ logging: ...@@ -6,8 +6,8 @@ logging:
org.springframework.cloud: 'DEBUG' org.springframework.cloud: 'DEBUG'
spring: spring:
profiles: application:
active: native name: apollo-configserver
datasource: datasource:
url: jdbc:h2:file:~/fxapolloconfigdb;DB_CLOSE_ON_EXIT=FALSE url: jdbc:h2:file:~/fxapolloconfigdb;DB_CLOSE_ON_EXIT=FALSE
username: sa username: sa
......
spring: eureka:
application: instance:
name: apollo-configserver hostname: localhost
preferIpAddress: true
cloud: client:
zookeeper: fetchRegistry: false
connectString:localhost:2181 serviceUrl:
\ No newline at end of file defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
healthcheck:
enabled: true
\ No newline at end of file
...@@ -17,24 +17,7 @@ ...@@ -17,24 +17,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId> <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -2,22 +2,25 @@ package com.ctrip.apollo.metaserver; ...@@ -2,22 +2,25 @@ package com.ctrip.apollo.metaserver;
import java.util.List; import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.ApplicationContext; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.ConfigurableApplicationContext;
import com.ctrip.apollo.metaserver.service.DiscoveryService; import com.ctrip.apollo.metaserver.service.DiscoveryService;
import com.netflix.appinfo.InstanceInfo;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableEurekaServer
@EnableEurekaClient
public class MetaServerApplication { public class MetaServerApplication {
public static void main(String[] args) { public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(MetaServerApplication.class, args); ConfigurableApplicationContext context =
List<ServiceInstance> metaServerServiceInstances = new SpringApplicationBuilder(MetaServerApplication.class).web(true).run(args);
context.getBean(DiscoveryService.class).getMetaServerServiceInstances(); DiscoveryService discoveryService = context.getBean(DiscoveryService.class);
System.out.println(metaServerServiceInstances); List<InstanceInfo> instances = discoveryService.getMetaServerServiceInstance();
System.out.println(instances);
} }
} }
package com.ctrip.apollo.metaserver.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.metaserver.service.DiscoveryService;
import com.netflix.appinfo.InstanceInfo;
@RestController
@RequestMapping("/services")
public class ServiceController {
@Autowired
private DiscoveryService discoveryService;
@RequestMapping("/meta")
public List<InstanceInfo> metaServer() {
return discoveryService.getMetaServerServiceInstance();
}
@RequestMapping("/config")
public List<InstanceInfo> configServer() {
return discoveryService.getConfigServerServiceInstance();
}
}
...@@ -3,27 +3,27 @@ package com.ctrip.apollo.metaserver.service; ...@@ -3,27 +3,27 @@ package com.ctrip.apollo.metaserver.service;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.ctrip.apollo.core.ServiceIdConsts; import com.ctrip.apollo.core.ServiceIdConsts;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.shared.Application;
@Service @Service
public class DiscoveryService { public class DiscoveryService {
@Autowired @Autowired
private DiscoveryClient discoveryClient; private EurekaClient eurekaClient;
public List<ServiceInstance> getConfigServerServiceInstances() { public List<InstanceInfo> getConfigServerServiceInstance() {
List<ServiceInstance> instances = Application application = eurekaClient.getApplication(ServiceIdConsts.APOLLO_CONFIGSERVER);
discoveryClient.getInstances(ServiceIdConsts.APOLLO_CONFIGSERVER); return application.getInstances();
return instances;
} }
public List<ServiceInstance> getMetaServerServiceInstances() { public List<InstanceInfo> getMetaServerServiceInstance() {
List<ServiceInstance> instances = Application application = eurekaClient.getApplication(ServiceIdConsts.APOLLO_METASERVER);
discoveryClient.getInstances(ServiceIdConsts.APOLLO_METASERVER); return application.getInstances();
return instances;
} }
} }
server:
port: 80
spring: spring:
application: application:
name: apollo-metaserver name: apollo-metaserver
profiles: profiles:
active: native active: native
logging:
level:
\ No newline at end of file
spring: server:
cloud: port: 8761
zookeeper:
connectString: 10.3.2.57:2181,10.3.2.58:2181,10.3.2.59:2181 eureka:
\ No newline at end of file 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
package com.ctrip.apollo.metaserver; package com.ctrip.apollo.metaserver;
import java.io.IOException;
import org.apache.curator.test.TestingServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MetaServerApplicationTestConfiguration.class) @SpringApplicationConfiguration(classes = MetaServerApplication.class)
public abstract class AbstractMetaServerTest { public abstract class AbstractMetaServerTest {
private static TestingServer zkTestServer;
@BeforeClass
public static void beforeClass() throws Exception {
zkTestServer = new TestingServer(2181, false);
zkTestServer.start();
System.out.format("embedded zookeeper is up %s%n", zkTestServer.getConnectString());
}
@AfterClass
public static void afterClass() throws IOException {
if (zkTestServer != null) {
zkTestServer.close();
}
}
} }
package com.ctrip.apollo.metaserver;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
//@EnableDiscoveryClient
public class MetaServerApplicationTestConfiguration {
}
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);
}
}
server:
port: 80
spring: spring:
application: application:
name: apollo-metaserver name: apollo-metaserver
......
spring: server:
cloud: port: 8761
zookeeper:
connectString: localhost:2181 eureka:
\ No newline at end of file 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
...@@ -16,8 +16,8 @@ ...@@ -16,8 +16,8 @@
<artifactId>apollo-core</artifactId> <artifactId>apollo-core</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
......
spring.datasource.url = jdbc:h2:file:~/fxapolloportaldb
spring.datasource.username = sa
spring.datasource.password = sa
server:
port: 80
spring:
application:
name: apollo-portal
datasource:
url: jdbc:h2:file:~/fxapolloportaldb
username: sa
password: sa
...@@ -5,7 +5,7 @@ import org.springframework.boot.test.SpringApplicationConfiguration; ...@@ -5,7 +5,7 @@ import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = PortalApplicationTestConfiguration.class) @SpringApplicationConfiguration(classes = PortalApplication.class)
public abstract class AbstractPortalTest { public abstract class AbstractPortalTest {
} }
package com.ctrip.apollo.portal;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PortalApplicationTestConfiguration {
}
...@@ -6,6 +6,7 @@ import java.net.URISyntaxException; ...@@ -6,6 +6,7 @@ import java.net.URISyntaxException;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.TestRestTemplate; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.WebIntegrationTest; import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
...@@ -25,6 +26,9 @@ public class AppControllerTest extends AbstractPortalTest { ...@@ -25,6 +26,9 @@ public class AppControllerTest extends AbstractPortalTest {
@Autowired @Autowired
AppRepository appRepository; AppRepository appRepository;
@Value("${server.port}")
String serverPort;
@Test @Test
public void testCreate() throws URISyntaxException { public void testCreate() throws URISyntaxException {
App newApp = new App(); App newApp = new App();
...@@ -32,7 +36,7 @@ public class AppControllerTest extends AbstractPortalTest { ...@@ -32,7 +36,7 @@ public class AppControllerTest extends AbstractPortalTest {
newApp.setName("new app " + System.currentTimeMillis()); newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis()); newApp.setOwner("owner " + System.currentTimeMillis());
URI uri = new URI("http://localhost:8080/apps"); URI uri = new URI("http://localhost:" + serverPort + "/apps");
App createdApp = restTemplate.postForObject(uri, newApp, App.class); App createdApp = restTemplate.postForObject(uri, newApp, App.class);
Assert.assertEquals(newApp.getAppId(), createdApp.getAppId()); Assert.assertEquals(newApp.getAppId(), createdApp.getAppId());
...@@ -52,7 +56,7 @@ public class AppControllerTest extends AbstractPortalTest { ...@@ -52,7 +56,7 @@ public class AppControllerTest extends AbstractPortalTest {
newApp.setOwner("owner " + System.currentTimeMillis()); newApp.setOwner("owner " + System.currentTimeMillis());
appRepository.save(newApp); appRepository.save(newApp);
URI uri = new URI("http://localhost:8080/apps"); URI uri = new URI("http://localhost:" + serverPort + "/apps");
App[] apps = restTemplate.getForObject(uri, App[].class); App[] apps = restTemplate.getForObject(uri, App[].class);
Assert.assertEquals(1, apps.length); Assert.assertEquals(1, apps.length);
...@@ -67,7 +71,7 @@ public class AppControllerTest extends AbstractPortalTest { ...@@ -67,7 +71,7 @@ public class AppControllerTest extends AbstractPortalTest {
newApp.setOwner("owner " + System.currentTimeMillis()); newApp.setOwner("owner " + System.currentTimeMillis());
appRepository.save(newApp); appRepository.save(newApp);
URI uri = new URI("http://localhost:8080/apps?page=2"); URI uri = new URI("http://localhost:" + serverPort + "/apps?page=2");
ResponseEntity<App[]> entity = restTemplate.getForEntity(uri, App[].class); ResponseEntity<App[]> entity = restTemplate.getForEntity(uri, App[].class);
Assert.assertEquals(HttpStatus.NOT_FOUND, entity.getStatusCode()); Assert.assertEquals(HttpStatus.NOT_FOUND, entity.getStatusCode());
......
spring.datasource.url = jdbc:h2:mem:fxapolloportaldb
spring.datasource.username = sa
spring.datasource.password =
spring.jpa.show-sql: true
\ No newline at end of file
server:
port: 80
spring:
datasource:
url: jdbc:h2:file:~/fxapolloportaldb
username: sa
password: sa
jpa:
show-sql: true
...@@ -118,8 +118,8 @@ ...@@ -118,8 +118,8 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-zookeeper</artifactId> <artifactId>spring-cloud-netflix</artifactId>
<version>1.0.0.M5</version> <version>1.1.0.M5</version>
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
......
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