diff --git a/apollo-configserver/pom.xml b/apollo-configserver/pom.xml index b0bb00ba6c8..6d0a754cb9a 100644 --- a/apollo-configserver/pom.xml +++ b/apollo-configserver/pom.xml @@ -19,6 +19,14 @@ org.springframework.cloud spring-cloud-config-server + + org.springframework.cloud + spring-cloud-zookeeper-discovery + + + org.apache.curator + curator-x-discovery + org.springframework.boot spring-boot-starter-data-jpa @@ -40,6 +48,11 @@ mysql-connector-java runtime + + org.apache.curator + curator-test + test + diff --git a/apollo-configserver/src/main/java/com/ctrip/apollo/server/ConfigServerApplication.java b/apollo-configserver/src/main/java/com/ctrip/apollo/configserver/ConfigServerApplication.java similarity index 78% rename from apollo-configserver/src/main/java/com/ctrip/apollo/server/ConfigServerApplication.java rename to apollo-configserver/src/main/java/com/ctrip/apollo/configserver/ConfigServerApplication.java index 5f93cc94f2f..1c7cf07ab33 100644 --- a/apollo-configserver/src/main/java/com/ctrip/apollo/server/ConfigServerApplication.java +++ b/apollo-configserver/src/main/java/com/ctrip/apollo/configserver/ConfigServerApplication.java @@ -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) { diff --git a/apollo-configserver/src/main/resources/application.yml b/apollo-configserver/src/main/resources/application.yml index eff7d870726..0cea26c5f89 100644 --- a/apollo-configserver/src/main/resources/application.yml +++ b/apollo-configserver/src/main/resources/application.yml @@ -1,4 +1,6 @@ spring: + application: + name: apollo-configserver cloud: config: server: diff --git a/apollo-configserver/src/main/resources/bootstrap.yml b/apollo-configserver/src/main/resources/bootstrap.yml index 196faa13a5a..796fc67bf4a 100644 --- a/apollo-configserver/src/main/resources/bootstrap.yml +++ b/apollo-configserver/src/main/resources/bootstrap.yml @@ -1,3 +1,4 @@ spring: - application: - name: apollo-server \ No newline at end of file + cloud: + zookeeper: + connectString: 10.3.2.57:2181,10.3.2.58:2181,10.3.2.59:2181 \ No newline at end of file diff --git a/apollo-configserver/src/test/java/com/ctrip/apollo/configserver/AbstractConfigServerTest.java b/apollo-configserver/src/test/java/com/ctrip/apollo/configserver/AbstractConfigServerTest.java new file mode 100644 index 00000000000..cb39b31192f --- /dev/null +++ b/apollo-configserver/src/test/java/com/ctrip/apollo/configserver/AbstractConfigServerTest.java @@ -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(); + } + } +} diff --git a/apollo-configserver/src/test/java/com/ctrip/apollo/configserver/AllTests.java b/apollo-configserver/src/test/java/com/ctrip/apollo/configserver/AllTests.java new file mode 100644 index 00000000000..7239a6c31f3 --- /dev/null +++ b/apollo-configserver/src/test/java/com/ctrip/apollo/configserver/AllTests.java @@ -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 { + +} diff --git a/apollo-configserver/src/test/java/com/ctrip/apollo/configserver/ConfigServerApplicationTestConfiguration.java b/apollo-configserver/src/test/java/com/ctrip/apollo/configserver/ConfigServerApplicationTestConfiguration.java new file mode 100644 index 00000000000..f537ed8b7b2 --- /dev/null +++ b/apollo-configserver/src/test/java/com/ctrip/apollo/configserver/ConfigServerApplicationTestConfiguration.java @@ -0,0 +1,8 @@ +package com.ctrip.apollo.configserver; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ConfigServerApplicationTestConfiguration { + +} diff --git a/apollo-configserver/src/test/resources/application.properties b/apollo-configserver/src/test/resources/application.properties deleted file mode 100644 index 02e9b4c6e72..00000000000 --- a/apollo-configserver/src/test/resources/application.properties +++ /dev/null @@ -1,3 +0,0 @@ -spring.datasource.url = jdbc:h2:file:~/fxapolloconfigdb;DB_CLOSE_ON_EXIT=FALSE -spring.datasource.username = sa -spring.datasource.password = sa diff --git a/apollo-configserver/src/test/resources/application.yml b/apollo-configserver/src/test/resources/application.yml new file mode 100644 index 00000000000..44848a4eba7 --- /dev/null +++ b/apollo-configserver/src/test/resources/application.yml @@ -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: diff --git a/apollo-configserver/src/test/resources/bootstrap.yml b/apollo-configserver/src/test/resources/bootstrap.yml new file mode 100644 index 00000000000..825b0e52511 --- /dev/null +++ b/apollo-configserver/src/test/resources/bootstrap.yml @@ -0,0 +1,7 @@ +spring: + application: + name: apollo-configserver + + cloud: + zookeeper: + connectString:localhost:2181 \ No newline at end of file diff --git a/apollo-core/src/main/java/com/ctrip/apollo/core/ServiceIdConsts.java b/apollo-core/src/main/java/com/ctrip/apollo/core/ServiceIdConsts.java new file mode 100644 index 00000000000..f770aed2e7e --- /dev/null +++ b/apollo-core/src/main/java/com/ctrip/apollo/core/ServiceIdConsts.java @@ -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"; +} diff --git a/apollo-metaserver/pom.xml b/apollo-metaserver/pom.xml index 6cc4574f017..4c82d38f49a 100644 --- a/apollo-metaserver/pom.xml +++ b/apollo-metaserver/pom.xml @@ -15,13 +15,25 @@ com.ctrip.apollo apollo-core + + org.springframework.cloud + spring-cloud-starter-config + + + org.springframework.cloud + spring-cloud-zookeeper-discovery + + + org.apache.curator + curator-x-discovery + org.springframework.boot spring-boot-starter-web - org.springframework.boot - spring-boot-starter-test + org.apache.curator + curator-test test diff --git a/apollo-metaserver/src/main/java/com/ctrip/apollo/metaserver/MetaServerApplication.java b/apollo-metaserver/src/main/java/com/ctrip/apollo/metaserver/MetaServerApplication.java index e9004b3533b..955a6029e2f 100644 --- a/apollo-metaserver/src/main/java/com/ctrip/apollo/metaserver/MetaServerApplication.java +++ b/apollo-metaserver/src/main/java/com/ctrip/apollo/metaserver/MetaServerApplication.java @@ -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 metaServerServiceInstances = + context.getBean(DiscoveryService.class).getMetaServerServiceInstances(); + System.out.println(metaServerServiceInstances); } } diff --git a/apollo-metaserver/src/main/java/com/ctrip/apollo/metaserver/service/DiscoveryService.java b/apollo-metaserver/src/main/java/com/ctrip/apollo/metaserver/service/DiscoveryService.java new file mode 100644 index 00000000000..2ff50deb065 --- /dev/null +++ b/apollo-metaserver/src/main/java/com/ctrip/apollo/metaserver/service/DiscoveryService.java @@ -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 getConfigServerServiceInstances() { + List instances = + discoveryClient.getInstances(ServiceIdConsts.APOLLO_CONFIGSERVER); + return instances; + } + + public List getMetaServerServiceInstances() { + List instances = + discoveryClient.getInstances(ServiceIdConsts.APOLLO_METASERVER); + return instances; + } +} diff --git a/apollo-metaserver/src/main/resources/application.yml b/apollo-metaserver/src/main/resources/application.yml new file mode 100644 index 00000000000..4fa665e660e --- /dev/null +++ b/apollo-metaserver/src/main/resources/application.yml @@ -0,0 +1,8 @@ +server: + port: 80 + +spring: + application: + name: apollo-metaserver + profiles: + active: native diff --git a/apollo-metaserver/src/main/resources/bootstrap.yml b/apollo-metaserver/src/main/resources/bootstrap.yml new file mode 100644 index 00000000000..33a37e8e6bb --- /dev/null +++ b/apollo-metaserver/src/main/resources/bootstrap.yml @@ -0,0 +1,4 @@ +spring: + cloud: + zookeeper: + connectString: 10.3.2.57:2181,10.3.2.58:2181,10.3.2.59:2181 \ No newline at end of file diff --git a/apollo-metaserver/src/test/java/com/ctrip/apollo/metaserver/AbstractMetaServerTest.java b/apollo-metaserver/src/test/java/com/ctrip/apollo/metaserver/AbstractMetaServerTest.java new file mode 100644 index 00000000000..b38ec5778ff --- /dev/null +++ b/apollo-metaserver/src/test/java/com/ctrip/apollo/metaserver/AbstractMetaServerTest.java @@ -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(); + } + } +} diff --git a/apollo-metaserver/src/test/java/com/ctrip/apollo/metaserver/AllTests.java b/apollo-metaserver/src/test/java/com/ctrip/apollo/metaserver/AllTests.java new file mode 100644 index 00000000000..122ef92ae8d --- /dev/null +++ b/apollo-metaserver/src/test/java/com/ctrip/apollo/metaserver/AllTests.java @@ -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 { + +} diff --git a/apollo-metaserver/src/test/java/com/ctrip/apollo/metaserver/MetaServerApplicationTestConfiguration.java b/apollo-metaserver/src/test/java/com/ctrip/apollo/metaserver/MetaServerApplicationTestConfiguration.java new file mode 100644 index 00000000000..a4848edfe50 --- /dev/null +++ b/apollo-metaserver/src/test/java/com/ctrip/apollo/metaserver/MetaServerApplicationTestConfiguration.java @@ -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 { + +} diff --git a/apollo-metaserver/src/test/java/com/ctrip/apollo/metaserver/service/ServiceDiscoveryTest.java b/apollo-metaserver/src/test/java/com/ctrip/apollo/metaserver/service/ServiceDiscoveryTest.java new file mode 100644 index 00000000000..77230544ff7 --- /dev/null +++ b/apollo-metaserver/src/test/java/com/ctrip/apollo/metaserver/service/ServiceDiscoveryTest.java @@ -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 instances = discoveryService.getMetaServerServiceInstances(); + System.out.println(instances); + } +} diff --git a/apollo-metaserver/src/test/resources/application.yml b/apollo-metaserver/src/test/resources/application.yml new file mode 100644 index 00000000000..4fa665e660e --- /dev/null +++ b/apollo-metaserver/src/test/resources/application.yml @@ -0,0 +1,8 @@ +server: + port: 80 + +spring: + application: + name: apollo-metaserver + profiles: + active: native diff --git a/apollo-metaserver/src/test/resources/bootstrap.yml b/apollo-metaserver/src/test/resources/bootstrap.yml new file mode 100644 index 00000000000..adcc2cb2966 --- /dev/null +++ b/apollo-metaserver/src/test/resources/bootstrap.yml @@ -0,0 +1,4 @@ +spring: + cloud: + zookeeper: + connectString: localhost:2181 \ No newline at end of file diff --git a/apollo-portal/src/main/java/com/ctrip/apollo/portal/repository/PrivilegeRepository.java b/apollo-portal/src/main/java/com/ctrip/apollo/portal/repository/PrivilegeRepository.java index 44bcc97cedd..d0ead17f61c 100644 --- a/apollo-portal/src/main/java/com/ctrip/apollo/portal/repository/PrivilegeRepository.java +++ b/apollo-portal/src/main/java/com/ctrip/apollo/portal/repository/PrivilegeRepository.java @@ -10,5 +10,7 @@ public interface PrivilegeRepository extends PagingAndSortingRepository findByAppId(String appId); - Privilege findByAppIdAndPrivilType(String appId, String privilType); + List findByAppIdAndPrivilType(String appId, String privilType); + + Privilege findByAppIdAndNameAndPrivilType(String appId, String name, String privilType); } diff --git a/apollo-portal/src/main/java/com/ctrip/apollo/portal/service/PrivilegeService.java b/apollo-portal/src/main/java/com/ctrip/apollo/portal/service/PrivilegeService.java index 074c0e3d77b..ef2cdfe0d17 100644 --- a/apollo-portal/src/main/java/com/ctrip/apollo/portal/service/PrivilegeService.java +++ b/apollo-portal/src/main/java/com/ctrip/apollo/portal/service/PrivilegeService.java @@ -6,6 +6,7 @@ import org.springframework.stereotype.Service; import com.ctrip.apollo.portal.entity.Privilege; +import com.ctrip.apollo.portal.exception.NotFoundException; import com.ctrip.apollo.portal.repository.PrivilegeRepository; @Service @@ -18,24 +19,32 @@ enum PrivilType { @Autowired private PrivilegeRepository privilRepo; + public Privilege addPrivilege(String appId, String name, PrivilType privilType) { + Privilege privil = privilRepo.findByAppIdAndNameAndPrivilType(appId, name, privilType.name()); + if (privil == null) { + privil = new Privilege(); + privil.setAppId(appId); + privil.setPrivilType(privilType.name()); + privil.setName(name); + privilRepo.save(privil); + } + return privil; + } + public boolean hasPrivilege(String appId, String name, PrivilType privilType) { - Privilege privil = privilRepo.findByAppIdAndPrivilType(appId, privilType.name()); - if (privil != null && privil.getName().equals(name)) return true; - return false; + Privilege privil = privilRepo.findByAppIdAndNameAndPrivilType(appId, name, privilType.name()); + return (privil != null) ? true : false; } public List listPrivileges(String appId) { return privilRepo.findByAppId(appId); } - public Privilege setPrivilege(String appId, String name, PrivilType privilType) { - Privilege privil = privilRepo.findByAppIdAndPrivilType(appId, privilType.name()); + public void removePrivilege(String appId, String name, PrivilType privilType) { + Privilege privil = privilRepo.findByAppIdAndNameAndPrivilType(appId, name, privilType.name()); if (privil == null) { - privil = new Privilege(); - privil.setAppId(appId); - privil.setPrivilType(privilType.name()); + throw new NotFoundException(); } - privil.setName(name); - return privilRepo.save(privil); + privilRepo.delete(privil); } } diff --git a/apollo-portal/src/test/java/com/ctrip/apollo/portal/service/PrivilegeServiceTest.java b/apollo-portal/src/test/java/com/ctrip/apollo/portal/service/PrivilegeServiceTest.java index 3f683136708..166cac5e03b 100644 --- a/apollo-portal/src/test/java/com/ctrip/apollo/portal/service/PrivilegeServiceTest.java +++ b/apollo-portal/src/test/java/com/ctrip/apollo/portal/service/PrivilegeServiceTest.java @@ -19,19 +19,24 @@ public class PrivilegeServiceTest extends AbstractPortalTest { PrivilegeService privilService; @Test - public void testAddPrivilege() { + public void testAddAndRemovePrivilege() { App newApp = new App(); newApp.setAppId(String.valueOf(System.currentTimeMillis())); newApp.setName("new app " + System.currentTimeMillis()); newApp.setOwner("owner " + System.currentTimeMillis()); appService.save(newApp); - privilService.setPrivilege(newApp.getAppId(), newApp.getOwner(), + privilService.addPrivilege(newApp.getAppId(), newApp.getOwner(), PrivilegeService.PrivilType.EDIT); List privileges = privilService.listPrivileges(newApp.getAppId()); Assert.assertEquals(1, privileges.size()); Assert.assertEquals(PrivilegeService.PrivilType.EDIT.name(), privileges.get(0).getPrivilType()); Assert.assertEquals(newApp.getOwner(), privileges.get(0).getName()); + + privilService.removePrivilege(newApp.getAppId(), newApp.getOwner(), + PrivilegeService.PrivilType.EDIT); + privileges = privilService.listPrivileges(newApp.getAppId()); + Assert.assertEquals(0, privileges.size()); } @Test @@ -42,7 +47,7 @@ public void testCheckPrivilege() { newApp.setOwner("owner " + System.currentTimeMillis()); appService.save(newApp); - privilService.setPrivilege(newApp.getAppId(), newApp.getOwner(), + privilService.addPrivilege(newApp.getAppId(), newApp.getOwner(), PrivilegeService.PrivilType.EDIT); Assert.assertTrue(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(), PrivilegeService.PrivilType.EDIT)); @@ -51,13 +56,13 @@ public void testCheckPrivilege() { Assert.assertFalse(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(), PrivilegeService.PrivilType.RELEASE)); - privilService.setPrivilege(newApp.getAppId(), "nobody", PrivilegeService.PrivilType.EDIT); + privilService.addPrivilege(newApp.getAppId(), "nobody", PrivilegeService.PrivilType.EDIT); Assert.assertTrue( privilService.hasPrivilege(newApp.getAppId(), "nobody", PrivilegeService.PrivilType.EDIT)); - Assert.assertFalse(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(), + Assert.assertTrue(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(), PrivilegeService.PrivilType.EDIT)); - privilService.setPrivilege(newApp.getAppId(), "nobody", PrivilegeService.PrivilType.RELEASE); + privilService.addPrivilege(newApp.getAppId(), "nobody", PrivilegeService.PrivilType.RELEASE); Assert.assertTrue(privilService.hasPrivilege(newApp.getAppId(), "nobody", PrivilegeService.PrivilType.RELEASE)); } diff --git a/framework-parent/pom.xml b/framework-parent/pom.xml index 0e6aeb8e940..8ad7ca9685b 100644 --- a/framework-parent/pom.xml +++ b/framework-parent/pom.xml @@ -15,7 +15,55 @@ 1.7 UTF-8 + + 4.12 + 19.0 + 1.7.19 + 4.5.2 + 4.4.4 + 1.2.8 + 4.0.34.Final + 1.10 + + + + junit + junit + ${junit.version} + + + com.google.guava + guava + ${guava.version} + + + org.slf4j + slf4j-api + ${slf4j-api.version} + + + org.apache.httpcomponents + httpclient + ${httpclient.version} + + + org.apache.httpcomponents + httpcore + ${httpcore.version} + + + com.alibaba + fastjson + ${fastjson.version} + + + io.netty + netty-all + ${netty-all.version} + + + diff --git a/pom.xml b/pom.xml index 03e8b5d3f79..4b192ad56b7 100644 --- a/pom.xml +++ b/pom.xml @@ -116,6 +116,13 @@ pom import + + org.springframework.cloud + spring-cloud-zookeeper + 1.0.0.M5 + pom + import +