Merge pull request #26 from nutzam/support_HikariCP

add: starter-jdbc添加并默认使用hikariCP
This commit is contained in:
Wendal Chen 2017-11-27 12:37:33 +08:00 committed by GitHub
commit 465a706bfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 47 additions and 79 deletions

View File

@ -38,6 +38,11 @@
<artifactId>nutzboot-starter-jdbc</artifactId> <artifactId>nutzboot-starter-jdbc</artifactId>
<version>${nutzboot.version}</version> <version>${nutzboot.version}</version>
</dependency> </dependency>
<dependency th:if="${params.jdbc.enable}">
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.5</version>
</dependency>
<dependency th:if="${params.redis.enable}"> <dependency th:if="${params.redis.enable}">
<groupId>org.nutz</groupId> <groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-redis</artifactId> <artifactId>nutzboot-starter-redis</artifactId>

View File

@ -1,7 +1,2 @@
jetty.port=8080 jetty.port=8080
jetty.host=127.0.0.1 jetty.host=127.0.0.1
jdbc.type=druid
jdbc.url=jdbc:h2:mem:~
jdbc.driverClassName=org.h2.Driver

View File

@ -1,7 +1,4 @@
jetty.port=8080 jetty.port=8080
jetty.host=127.0.0.1 jetty.host=127.0.0.1
jdbc.type=druid
jdbc.url=jdbc:h2:mem:~ jdbc.url=jdbc:h2:mem:~
jdbc.driverClassName=org.h2.Driver

View File

@ -1,7 +1,2 @@
jetty.port=8080 jetty.port=8080
jetty.host=127.0.0.1 jetty.host=127.0.0.1
jdbc.type=druid
jdbc.url=jdbc:h2:mem:~
jdbc.driverClassName=org.h2.Driver

View File

@ -2,6 +2,4 @@ jetty.port=8080
jetty.host=127.0.0.1 jetty.host=127.0.0.1
jdbc.type=druid
jdbc.url=jdbc:h2:mem:~ jdbc.url=jdbc:h2:mem:~
jdbc.driverClassName=org.h2.Driver

View File

@ -1,38 +0,0 @@
<?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/maven-v4_0_0.xsd">
<parent>
<artifactId>nutzboot-demo-simple</artifactId>
<groupId>org.nutz</groupId>
<version>2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nutzboot-demo-simple-redis</artifactId>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer />
<transformer>
<resource>META-INF/nutz/org.nutz.boot.starter.NbStarter</resource>
</transformer>
<transformer>
<mainClass>io.nutz.demo.simple.MainLauncher</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -10,7 +10,7 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.nutz</groupId> <groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-nutz</artifactId> <artifactId>nutzboot-starter-nutz-mvc</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>
@ -23,11 +23,6 @@
<artifactId>nutzboot-starter-redis</artifactId> <artifactId>nutzboot-starter-redis</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency> <dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>

View File

@ -3,9 +3,7 @@ package io.nutz.demo.simple;
import static org.nutz.integration.jedis.RedisInterceptor.jedis; import static org.nutz.integration.jedis.RedisInterceptor.jedis;
import org.nutz.boot.NbApp; import org.nutz.boot.NbApp;
import org.nutz.dao.Dao;
import org.nutz.ioc.aop.Aop; import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean; import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log; import org.nutz.log.Log;
import org.nutz.log.Logs; import org.nutz.log.Logs;
@ -17,9 +15,6 @@ public class MainLauncher {
private static final Log log = Logs.get(); private static final Log log = Logs.get();
@Inject
Dao dao;
@Ok("raw") @Ok("raw")
@At("/redis/info") @At("/redis/info")
@Aop("redis") @Aop("redis")

View File

@ -1,7 +1,2 @@
jetty.port=8080 jetty.port=8080
jetty.host=127.0.0.1 jetty.host=127.0.0.1
jdbc.type=druid
jdbc.url=jdbc:h2:mem:~
jdbc.driverClassName=org.h2.Driver

View File

@ -56,6 +56,7 @@
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>druid</artifactId> <artifactId>druid</artifactId>
<version>1.1.5</version> <version>1.1.5</version>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.nutz</groupId> <groupId>org.nutz</groupId>
@ -63,5 +64,10 @@
<version>2.0-SNAPSHOT</version> <version>2.0-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.7.4</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -2,6 +2,7 @@ package org.nutz.boot.starter.jdbc;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -16,6 +17,8 @@ import org.nutz.lang.Strings;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory; import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@IocBean @IocBean
public class DataSourceStarter { public class DataSourceStarter {
@ -38,7 +41,7 @@ public class DataSourceStarter {
@IocBean @IocBean
public DataSource getDataSource() throws Exception { public DataSource getDataSource() throws Exception {
switch (conf.get(PROP_TYPE, "druid")) { switch (conf.get(PROP_TYPE, "hikari")) {
case "simple": case "simple":
case "org.nutz.dao.impl.SimpleDataSource": case "org.nutz.dao.impl.SimpleDataSource":
SimpleDataSource simpleDataSource = new SimpleDataSource(); SimpleDataSource simpleDataSource = new SimpleDataSource();
@ -52,8 +55,10 @@ public class DataSourceStarter {
return simpleDataSource; return simpleDataSource;
case "druid": case "druid":
case "com.alibaba.druid.pool.DruidDataSource": case "com.alibaba.druid.pool.DruidDataSource":
return ioc.get(DruidDataSource.class); return ioc.get(DataSource.class, "druidDataSource");
// TODO 支持其他数据源 case "hikari":
case "com.zaxxer.hikari.HikariDataSource":
return ioc.get(DataSource.class, "hikariDataSource");
default: default:
break; break;
} }
@ -62,7 +67,7 @@ public class DataSourceStarter {
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({ "rawtypes", "unchecked" })
@IocBean(name = "druidDataSource", depose = "close") @IocBean(name = "druidDataSource", depose = "close")
public DruidDataSource createDruidDataSource() throws Exception { public DataSource createDruidDataSource() throws Exception {
if (Strings.isBlank(conf.get(PROP_URL))) { if (Strings.isBlank(conf.get(PROP_URL))) {
throw new RuntimeException("need jdbc.url"); throw new RuntimeException("need jdbc.url");
} }
@ -73,8 +78,28 @@ public class DataSourceStarter {
return dataSource; return dataSource;
} }
@IocBean(name = "hikariDataSource", depose = "close")
public DataSource createHikariCPDataSource() throws Exception {
if (Strings.isBlank(conf.get(PROP_URL))) {
throw new RuntimeException("need jdbc.url");
}
Properties properties = new Properties();
for (String key : conf.keys()) {
if (!key.startsWith("jdbc.") || key.equals("jdbc.type"))
continue;
if (key.equals("jdbc.url")) {
if (!conf.has("jdbc.jdbcUrl"))
properties.put("jdbcUrl", conf.get(key));
}
else {
properties.put(key.substring(5), conf.get(key));
}
}
return new HikariDataSource(new HikariConfig(properties));
}
protected static boolean isDruid(PropertiesProxy conf) { protected static boolean isDruid(PropertiesProxy conf) {
String type = conf.get(PROP_TYPE, "druid"); String type = conf.get(PROP_TYPE, "hikari");
return "druid".equals(type) || "com.alibaba.druid.pool.DruidDataSource".equals(type); return "druid".equals(type) || "com.alibaba.druid.pool.DruidDataSource".equals(type);
} }
} }