Explorar o código

Add Ctrip Titan DataSource Support

Yiming Liu %!s(int64=9) %!d(string=hai) anos
pai
achega
fe7cd26906

+ 47 - 32
apollo-biz/pom.xml

@@ -1,35 +1,50 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0"
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-    <parent>
-        <artifactId>apollo</artifactId>
-        <groupId>com.ctrip.apollo</groupId>
-        <version>0.0.1-SNAPSHOT</version>
-    </parent>
-    <modelVersion>4.0.0</modelVersion>
-    <artifactId>apollo-biz</artifactId>
-    <name>Apollo Biz</name>
-    <packaging>jar</packaging>
-
-    <dependencies>
-        <dependency>
-            <groupId>com.ctrip.apollo</groupId>
-            <artifactId>apollo-core</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-data-jpa</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>com.h2database</groupId>
-            <artifactId>h2</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>mysql</groupId>
-            <artifactId>mysql-connector-java</artifactId>
-            <scope>runtime</scope>
-        </dependency>
-    </dependencies>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<parent>
+		<artifactId>apollo</artifactId>
+		<groupId>com.ctrip.apollo</groupId>
+		<version>0.0.1-SNAPSHOT</version>
+	</parent>
+	<modelVersion>4.0.0</modelVersion>
+	<artifactId>apollo-biz</artifactId>
+	<name>Apollo Biz</name>
+	<packaging>jar</packaging>
 
+	<dependencies>
+		<dependency>
+			<groupId>com.ctrip.apollo</groupId>
+			<artifactId>apollo-core</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-data-jpa</artifactId>
+			<exclusions>
+				<exclusion>
+					<artifactId>logback-classic</artifactId>
+					<groupId>ch.qos.logback</groupId>
+				</exclusion>
+			</exclusions>
+		</dependency>
+	</dependencies>
+	<profiles>
+		<profile>
+			<id>local</id>
+			<dependencies>
+				<dependency>
+					<groupId>com.h2database</groupId>
+					<artifactId>h2</artifactId>
+				</dependency>
+			</dependencies>
+		</profile>
+		<profile>
+			<id>ctrip</id>
+			<dependencies>
+				<dependency>
+					<groupId>mysql</groupId>
+					<artifactId>mysql-connector-java</artifactId>
+				</dependency>
+			</dependencies>
+		</profile>
+	</profiles>
 </project>

+ 19 - 0
apollo-biz/src/main/java/com/ctrip/apollo/biz/datasource/TitanCondition.java

@@ -0,0 +1,19 @@
+package com.ctrip.apollo.biz.datasource;
+
+import org.springframework.context.annotation.Condition;
+import org.springframework.context.annotation.ConditionContext;
+import org.springframework.core.type.AnnotatedTypeMetadata;
+
+import com.ctrip.apollo.core.utils.StringUtils;
+
+public class TitanCondition implements Condition {
+
+  @Override
+  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
+    if (!StringUtils.isEmpty(context.getEnvironment().getProperty("titan.url"))) {
+      return true;
+    }
+    return false;
+  }
+
+}

+ 29 - 0
apollo-biz/src/main/java/com/ctrip/apollo/biz/datasource/TitanEntityManager.java

@@ -0,0 +1,29 @@
+package com.ctrip.apollo.biz.datasource;
+
+import java.lang.reflect.Method;
+
+import javax.sql.DataSource;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Conditional;
+import org.springframework.stereotype.Component;
+
+@Component
+@Conditional(TitanCondition.class)
+public class TitanEntityManager {
+
+  @Autowired
+  private TitanSettings settings;
+
+  @SuppressWarnings({"rawtypes", "unchecked"})
+  @Bean
+  public DataSource datasource() throws Exception {
+    Class clazz = Class.forName("com.ctrip.datasource.configure.DalDataSourceFactory");
+    Object obj = clazz.newInstance();
+    Method method = clazz.getMethod("createDataSource", new Class[] {String.class, String.class});
+    return ((DataSource) method.invoke(obj,
+        new String[] {settings.getTitanDbname(), settings.getTitanUrl()}));
+  }
+
+}

+ 30 - 0
apollo-biz/src/main/java/com/ctrip/apollo/biz/datasource/TitanSettings.java

@@ -0,0 +1,30 @@
+package com.ctrip.apollo.biz.datasource;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+@Component
+public class TitanSettings {
+
+  @Value("${titan.url:}")
+  private String titanUrl;
+
+  @Value("${titan.dbname:}")
+  private String titanDbname;
+
+  public String getTitanUrl() {
+    return titanUrl;
+  }
+
+  public void setTitanUrl(String titanUrl) {
+    this.titanUrl = titanUrl;
+  }
+
+  public String getTitanDbname() {
+    return titanDbname;
+  }
+
+  public void setTitanDbname(String titanDbname) {
+    this.titanDbname = titanDbname;
+  }
+}

+ 4 - 2
apollo-configservice/src/main/java/com/ctrip/apollo/ServerApplication.java

@@ -3,6 +3,7 @@ package com.ctrip.apollo;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.builder.SpringApplicationBuilder;
 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
+import org.springframework.context.ConfigurableApplicationContext;
 
 /**
  * Spring boot application entry point
@@ -13,8 +14,9 @@ import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 @EnableEurekaServer
 public class ServerApplication {
 
-  public static void main(String[] args) {
-    new SpringApplicationBuilder(ServerApplication.class).web(true).run(args);
+  public static void main(String[] args) throws Exception {
+    ConfigurableApplicationContext context =
+        new SpringApplicationBuilder(ServerApplication.class).web(true).run(args);
   }
 
 }

+ 38 - 25
apollo-configservice/src/main/java/com/ctrip/apollo/metaservice/controller/ServiceController.java

@@ -8,8 +8,9 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.util.ArrayList;
 import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
 @RestController
 @RequestMapping("/services")
@@ -22,42 +23,54 @@ public class ServiceController {
   @RequestMapping("/meta")
   public List<ServiceDTO> getMetaService() {
     List<InstanceInfo> instances = discoveryService.getMetaServiceInstances();
-    List<ServiceDTO> result = new ArrayList<ServiceDTO>();
-    for (InstanceInfo instance : instances) {
-      ServiceDTO service = new ServiceDTO();
-      service.setAppName(instance.getAppName());
-      service.setInstanceId(instance.getInstanceId());
-      service.setHomepageUrl(instance.getHomePageUrl());
-      result.add(service);
-    }
+    List<ServiceDTO> result = instances.stream().map(new Function<InstanceInfo, ServiceDTO>() {
+
+      @Override
+      public ServiceDTO apply(InstanceInfo instance) {
+        ServiceDTO service = new ServiceDTO();
+        service.setAppName(instance.getAppName());
+        service.setInstanceId(instance.getInstanceId());
+        service.setHomepageUrl(instance.getHomePageUrl());
+        return service;
+      }
+
+    }).collect(Collectors.toList());
     return result;
   }
 
   @RequestMapping("/config")
   public List<ServiceDTO> getConfigService() {
     List<InstanceInfo> instances = discoveryService.getConfigServiceInstances();
-    List<ServiceDTO> result = new ArrayList<ServiceDTO>();
-    for (InstanceInfo instance : instances) {
-      ServiceDTO service = new ServiceDTO();
-      service.setAppName(instance.getAppName());
-      service.setInstanceId(instance.getInstanceId());
-      service.setHomepageUrl(instance.getHomePageUrl());
-      result.add(service);
-    }
+    List<ServiceDTO> result = instances.stream().map(new Function<InstanceInfo, ServiceDTO>() {
+
+      @Override
+      public ServiceDTO apply(InstanceInfo instance) {
+        ServiceDTO service = new ServiceDTO();
+        service.setAppName(instance.getAppName());
+        service.setInstanceId(instance.getInstanceId());
+        service.setHomepageUrl(instance.getHomePageUrl());
+        return service;
+      }
+
+    }).collect(Collectors.toList());
     return result;
   }
 
   @RequestMapping("/admin")
   public List<ServiceDTO> getAdminService() {
     List<InstanceInfo> instances = discoveryService.getAdminServiceInstances();
-    List<ServiceDTO> result = new ArrayList<ServiceDTO>();
-    for (InstanceInfo instance : instances) {
-      ServiceDTO service = new ServiceDTO();
-      service.setAppName(instance.getAppName());
-      service.setInstanceId(instance.getInstanceId());
-      service.setHomepageUrl(instance.getHomePageUrl());
-      result.add(service);
-    }
+    List<ServiceDTO> result = instances.stream().map(new Function<InstanceInfo, ServiceDTO>() {
+
+      @Override
+      public ServiceDTO apply(InstanceInfo instance) {
+        ServiceDTO service = new ServiceDTO();
+        service.setAppName(instance.getAppName());
+        service.setInstanceId(instance.getInstanceId());
+        service.setHomepageUrl(instance.getHomePageUrl());
+        return service;
+      }
+
+    }).collect(Collectors.toList());
     return result;
   }
 }

+ 30 - 0
pom.xml

@@ -274,6 +274,36 @@
 				</plugins>
 			</build>
 		</profile>
+		<profile>
+			<id>local</id>
+			<activation>
+				<activeByDefault>true</activeByDefault>
+			</activation>
+		</profile>
+		<profile>
+			<id>ctrip</id>
+			<dependencies>
+				<dependency>
+					<groupId>com.ctrip.platform</groupId>
+					<artifactId>ctrip-dal-client</artifactId>
+					<version>1.0.1.6</version>
+					<exclusions>
+						<exclusion>
+							<artifactId>logback-core</artifactId>
+							<groupId>ch.qos.logback</groupId>
+						</exclusion>
+						<exclusion>
+							<artifactId>logback-classic</artifactId>
+							<groupId>ch.qos.logback</groupId>
+						</exclusion>
+					</exclusions>
+				</dependency>
+				<dependency>
+					<groupId>ch.qos.logback</groupId>
+					<artifactId>logback-classic</artifactId>
+				</dependency>
+			</dependencies>
+		</profile>
 	</profiles>
 
 	<repositories>