Commit fe7cd269 by Yiming Liu

Add Ctrip Titan DataSource Support

parent 6888ff73
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent>
<parent> <artifactId>apollo</artifactId>
<artifactId>apollo</artifactId> <groupId>com.ctrip.apollo</groupId>
<groupId>com.ctrip.apollo</groupId> <version>0.0.1-SNAPSHOT</version>
<version>0.0.1-SNAPSHOT</version> </parent>
</parent> <modelVersion>4.0.0</modelVersion>
<modelVersion>4.0.0</modelVersion> <artifactId>apollo-biz</artifactId>
<artifactId>apollo-biz</artifactId> <name>Apollo Biz</name>
<name>Apollo Biz</name> <packaging>jar</packaging>
<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>
<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> </project>
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;
}
}
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()}));
}
}
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;
}
}
...@@ -3,6 +3,7 @@ package com.ctrip.apollo; ...@@ -3,6 +3,7 @@ package com.ctrip.apollo;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.ConfigurableApplicationContext;
/** /**
* Spring boot application entry point * Spring boot application entry point
...@@ -13,8 +14,9 @@ import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; ...@@ -13,8 +14,9 @@ import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer @EnableEurekaServer
public class ServerApplication { public class ServerApplication {
public static void main(String[] args) { public static void main(String[] args) throws Exception {
new SpringApplicationBuilder(ServerApplication.class).web(true).run(args); ConfigurableApplicationContext context =
new SpringApplicationBuilder(ServerApplication.class).web(true).run(args);
} }
} }
...@@ -8,8 +8,9 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -8,8 +8,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
@RestController @RestController
@RequestMapping("/services") @RequestMapping("/services")
...@@ -22,42 +23,54 @@ public class ServiceController { ...@@ -22,42 +23,54 @@ public class ServiceController {
@RequestMapping("/meta") @RequestMapping("/meta")
public List<ServiceDTO> getMetaService() { public List<ServiceDTO> getMetaService() {
List<InstanceInfo> instances = discoveryService.getMetaServiceInstances(); List<InstanceInfo> instances = discoveryService.getMetaServiceInstances();
List<ServiceDTO> result = new ArrayList<ServiceDTO>(); List<ServiceDTO> result = instances.stream().map(new Function<InstanceInfo, ServiceDTO>() {
for (InstanceInfo instance : instances) {
ServiceDTO service = new ServiceDTO(); @Override
service.setAppName(instance.getAppName()); public ServiceDTO apply(InstanceInfo instance) {
service.setInstanceId(instance.getInstanceId()); ServiceDTO service = new ServiceDTO();
service.setHomepageUrl(instance.getHomePageUrl()); service.setAppName(instance.getAppName());
result.add(service); service.setInstanceId(instance.getInstanceId());
} service.setHomepageUrl(instance.getHomePageUrl());
return service;
}
}).collect(Collectors.toList());
return result; return result;
} }
@RequestMapping("/config") @RequestMapping("/config")
public List<ServiceDTO> getConfigService() { public List<ServiceDTO> getConfigService() {
List<InstanceInfo> instances = discoveryService.getConfigServiceInstances(); List<InstanceInfo> instances = discoveryService.getConfigServiceInstances();
List<ServiceDTO> result = new ArrayList<ServiceDTO>(); List<ServiceDTO> result = instances.stream().map(new Function<InstanceInfo, ServiceDTO>() {
for (InstanceInfo instance : instances) {
ServiceDTO service = new ServiceDTO(); @Override
service.setAppName(instance.getAppName()); public ServiceDTO apply(InstanceInfo instance) {
service.setInstanceId(instance.getInstanceId()); ServiceDTO service = new ServiceDTO();
service.setHomepageUrl(instance.getHomePageUrl()); service.setAppName(instance.getAppName());
result.add(service); service.setInstanceId(instance.getInstanceId());
} service.setHomepageUrl(instance.getHomePageUrl());
return service;
}
}).collect(Collectors.toList());
return result; return result;
} }
@RequestMapping("/admin") @RequestMapping("/admin")
public List<ServiceDTO> getAdminService() { public List<ServiceDTO> getAdminService() {
List<InstanceInfo> instances = discoveryService.getAdminServiceInstances(); List<InstanceInfo> instances = discoveryService.getAdminServiceInstances();
List<ServiceDTO> result = new ArrayList<ServiceDTO>(); List<ServiceDTO> result = instances.stream().map(new Function<InstanceInfo, ServiceDTO>() {
for (InstanceInfo instance : instances) {
ServiceDTO service = new ServiceDTO(); @Override
service.setAppName(instance.getAppName()); public ServiceDTO apply(InstanceInfo instance) {
service.setInstanceId(instance.getInstanceId()); ServiceDTO service = new ServiceDTO();
service.setHomepageUrl(instance.getHomePageUrl()); service.setAppName(instance.getAppName());
result.add(service); service.setInstanceId(instance.getInstanceId());
} service.setHomepageUrl(instance.getHomePageUrl());
return service;
}
}).collect(Collectors.toList());
return result; return result;
} }
} }
...@@ -274,6 +274,36 @@ ...@@ -274,6 +274,36 @@
</plugins> </plugins>
</build> </build>
</profile> </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> </profiles>
<repositories> <repositories>
......
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