Skip to content

Commit

Permalink
Add ServiceDiscovery based on ZK
Browse files Browse the repository at this point in the history
  • Loading branch information
yiming187 committed Mar 17, 2016
1 parent 047000d commit ac0e32e
Show file tree
Hide file tree
Showing 27 changed files with 347 additions and 26 deletions.
13 changes: 13 additions & 0 deletions apollo-configserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-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-data-jpa</artifactId>
Expand All @@ -40,6 +48,11 @@
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.ctrip.apollo.server;
package com.ctrip.apollo.configserver;

import com.jcraft.jsch.JSch;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {

public static void main(String[] args) {
Expand Down
2 changes: 2 additions & 0 deletions apollo-configserver/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
spring:
application:
name: apollo-configserver
cloud:
config:
server:
Expand Down
5 changes: 3 additions & 2 deletions apollo-configserver/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
spring:
application:
name: apollo-server
cloud:
zookeeper:
connectString: 10.3.2.57:2181,10.3.2.58:2181,10.3.2.59:2181
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ConfigServerApplicationTestConfiguration.class)
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ctrip.apollo.configserver;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({

})
public class AllTests {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ctrip.apollo.configserver;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigServerApplicationTestConfiguration {

}
3 changes: 0 additions & 3 deletions apollo-configserver/src/test/resources/application.properties

This file was deleted.

14 changes: 14 additions & 0 deletions apollo-configserver/src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
server:
port: 8888

logging:
level:
org.springframework.cloud: 'DEBUG'

spring:
profiles:
active: native
datasource:
url: jdbc:h2:file:~/fxapolloconfigdb;DB_CLOSE_ON_EXIT=FALSE
username: sa
password:
7 changes: 7 additions & 0 deletions apollo-configserver/src/test/resources/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spring:
application:
name: apollo-configserver

cloud:
zookeeper:
connectString:localhost:2181
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ctrip.apollo.core;

public class ServiceIdConsts {

public static final String APOLLO_METASERVER = "apollo-metaserver";

public static final String APOLLO_CONFIGSERVER = "apollo-configserver";

public static final String APOLLO_PORTAL = "apollo-portal";
}
16 changes: 14 additions & 2 deletions apollo-metaserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,25 @@
<groupId>com.ctrip.apollo</groupId>
<artifactId>apollo-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package com.ctrip.apollo.metaserver;

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ApplicationContext;

import com.ctrip.apollo.metaserver.service.DiscoveryService;

@SpringBootApplication
@EnableDiscoveryClient
public class MetaServerApplication {

public static void main(String[] args) {
SpringApplication.run(MetaServerApplication.class, args);
ApplicationContext context = SpringApplication.run(MetaServerApplication.class, args);
List<ServiceInstance> metaServerServiceInstances =
context.getBean(DiscoveryService.class).getMetaServerServiceInstances();
System.out.println(metaServerServiceInstances);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.ctrip.apollo.metaserver.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;
}
}
8 changes: 8 additions & 0 deletions apollo-metaserver/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
server:
port: 80

spring:
application:
name: apollo-metaserver
profiles:
active: native
4 changes: 4 additions & 0 deletions apollo-metaserver/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring:
cloud:
zookeeper:
connectString: 10.3.2.57:2181,10.3.2.58:2181,10.3.2.59:2181
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MetaServerApplicationTestConfiguration.class)
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ctrip.apollo.metaserver;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({

})
public class AllTests {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ctrip.apollo.metaserver;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
//@EnableDiscoveryClient
public class MetaServerApplicationTestConfiguration {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.ctrip.apollo.metaserver.service;

import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;

import com.ctrip.apollo.metaserver.AbstractMetaServerTest;

public class ServiceDiscoveryTest extends AbstractMetaServerTest {

@Autowired
private DiscoveryService discoveryService;

@Test
public void testGetLocalMetaServerServices() {
List<ServiceInstance> instances = discoveryService.getMetaServerServiceInstances();
System.out.println(instances);
}
}
8 changes: 8 additions & 0 deletions apollo-metaserver/src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
server:
port: 80

spring:
application:
name: apollo-metaserver
profiles:
active: native
4 changes: 4 additions & 0 deletions apollo-metaserver/src/test/resources/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring:
cloud:
zookeeper:
connectString: localhost:2181
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public interface PrivilegeRepository extends PagingAndSortingRepository<Privileg

List<Privilege> findByAppId(String appId);

Privilege findByAppIdAndPrivilType(String appId, String privilType);
List<Privilege> findByAppIdAndPrivilType(String appId, String privilType);

Privilege findByAppIdAndNameAndPrivilType(String appId, String name, String privilType);
}
Loading

0 comments on commit ac0e32e

Please sign in to comment.