已合并
!15 fix: 去掉manyDao对dao的强依赖

文涛:devNutz:dev

文涛 创建于: 2018-09-12 23:25

暂无描述。

0 条评论, 1 人参与

展开详细操作日志 收起操作日志
Wendal 合并了 Pull Request 2018-09-12 23:40

登录 后才可以发表评论

@@ -124,7 +124,7 @@ public class MainLauncher {
* [haoqoo](https://github.com/haoqoo)(提交velocity)
* [鱼夫](https://gitee.com/yustory)(正在踩NB+U家三剑客的坑)
* [幸福的旁边](https://github.com/happyday517)(提交caffeine方法缓存)
-* [文涛](https://github.com/swtseaman) (新增支持加载外部配置文件)
+* [文涛](https://gitee.com/wentao0291) (新增支持加载外部配置文件,新增多数据库连接支持)
* [zjSniper](https://gitee.com/zjSniper) (优化starter-tio的逻辑)
* [tasdingoo](https://github.com/tasdingoo)(issue@github 122)
* [csl_slchia](https://gitee.com/csl_slchia)(issue@gitee II92L)
nutzboot-starter/nutzboot-starter-jdbc/src/main/java/org/nutz/boot/starter/jdbc/DataSourceStarter.java
@@ -111,6 +111,22 @@ public class DataSourceStarter {
throw new RuntimeException("not supported jdbc.type=" + conf.get("jdbc.type"));
}
 
+ public static DataSource createManyDataSource(Ioc ioc, PropertiesProxy conf, String prefix) {
+ try {
+ String key = prefix + "type";
+ if (!conf.has(key)) {
+ throw new RuntimeException(key + " is null");
+ }
+ String type = conf.get(key);
+ if (!type.equals("druid")) {
+ throw new RuntimeException(key + "is not druid");
+ }
+ return createDruidDataSource(conf, prefix);
+ } catch (Exception e) {
+ throw new RuntimeException("datasource init error");
+ }
+ }
+
public static DataSource createSlaveDataSource(Ioc ioc, PropertiesProxy conf, String prefix) throws Exception {
switch (conf.get(prefix + "type", "druid")) {
case "simple":
@@ -165,34 +181,46 @@ public class DataSourceStarter {
if (ioc.has("slaveDataSource")) {
return ioc.get(DataSource.class, "slaveDataSource");
} else {
- // 看看有多少从数据库被定义了
- List<DataSource> slaveDataSources = new ArrayList<>();
- for (String key : conf.keys()) {
- if (key.startsWith(prefix) && key.endsWith(".url")) {
- String slaveName = key.substring(prefix.length(), key.length() - ".url".length());
- log.debug("found Slave DataSource name=" + slaveName);
- try {
- DataSource slaveDataSource = DataSourceStarter.createSlaveDataSource(ioc, conf, prefix + slaveName + ".");
- slaveDataSources.add(slaveDataSource);
- slaves.add(slaveDataSource);
- }
- catch (Exception e) {
- throw new RuntimeException(e);
- }
+ return _getSlaveDataSource(ioc, conf, prefix);
+ }
+ }
+
+ public static DataSource getManySlaveDataSource(Ioc ioc, PropertiesProxy conf, String prefix) {
+ if (ioc.has(prefix + "slaveDataSource")) {
+ return ioc.get(DataSource.class, prefix + "slaveDataSource");
+ } else {
+ return _getSlaveDataSource(ioc, conf, prefix);
+ }
+ }
+
+ private static DataSource _getSlaveDataSource(Ioc ioc, PropertiesProxy conf, String prefix) {
+ // 看看有多少从数据库被定义了
+ List<DataSource> slaveDataSources = new ArrayList<>();
+ for (String key : conf.keys()) {
+ if (key.startsWith(prefix) && key.endsWith(".url")) {
+ String slaveName = key.substring(prefix.length(), key.length() - ".url".length());
+ log.debug("found Slave DataSource name=" + slaveName);
+ try {
+ DataSource slaveDataSource = DataSourceStarter.createSlaveDataSource(ioc, conf, prefix + slaveName + ".");
+ slaveDataSources.add(slaveDataSource);
+ slaves.add(slaveDataSource);
}
- }
- // 如果的确定义了从数据库集合
- if (slaveDataSources.size() > 0) {
- if (slaveDataSources.size() == 1) {
- // 单个? 那就直接set吧
- return slaveDataSources.get(0);
- } else {
- // 多个从数据源,使用DynaDataSource进行随机挑选
- // TODO 更多更精细的挑选策略(轮训/随机/可用性...)
- return new DynaDataSource(new DynaDataSourceSeletor(slaveDataSources));
+ catch (Exception e) {
+ throw new RuntimeException(e);
}
}
}
+ // 如果的确定义了从数据库集合
+ if (slaveDataSources.size() > 0) {
+ if (slaveDataSources.size() == 1) {
+ // 单个? 那就直接set吧
+ return slaveDataSources.get(0);
+ } else {
+ // 多个从数据源,使用DynaDataSource进行随机挑选
+ // TODO 更多更精细的挑选策略(轮训/随机/可用性...)
+ return new DynaDataSource(new DynaDataSourceSeletor(slaveDataSources));
+ }
+ }
return null;
}
 
@@ -213,7 +241,7 @@ public class DataSourceStarter {
}
 
public void close() throws IOException {
- for (DataSource dataSource : slaves) {
+ for (DataSource dataSource : ds) {
try {
if (dataSource instanceof Closeable)
((Closeable) dataSource).close();
nutzboot-starter/nutzboot-starter-nutz-dao/src/main/java/org/nutz/boot/starter/nutz/dao/NutDaoStarter.java
@@ -35,7 +35,7 @@ import org.nutz.plugins.cache.dao.impl.provider.RedisDaoCacheProvider;
import net.sf.ehcache.CacheManager;
import redis.clients.jedis.JedisPool;
 
-@IocBean
+@IocBean(create="init")
public class NutDaoStarter {
 
private static final Log log = Logs.get();
@@ -83,6 +83,10 @@ public class NutDaoStarter {
@Inject("refer:$ioc")
protected Ioc ioc;
 
+ public void init() {
+ injectManyDao();
+ }
+
@IocBean
public SqlManager getSqlManager() {
return new FileSqlManager(conf.get("nutz.dao.sqls.path", "sqls/"));
@@ -186,7 +190,6 @@ public class NutDaoStarter {
dao.setRunner(runner);
}
}
- injectManyDao();
return dao;
}
 
@@ -200,12 +203,12 @@ public class NutDaoStarter {
// 获取数据库名称
String name = match.group(1);
String prefix_name = "jdbc.many." + name + ".";
- DataSource manyDataSource = DataSourceStarter.createDataSource(ioc, conf, prefix_name);
+ DataSource manyDataSource = DataSourceStarter.createManyDataSource(ioc, conf, prefix_name);
NutDao nutDao = new NutDao();
nutDao.setDataSource(manyDataSource);
// 处理对应的从库
String slave_prefix = prefix_name + "slave.";
- DataSource slaveDataSource = DataSourceStarter.getSlaveDataSource(ioc, conf, slave_prefix);
+ DataSource slaveDataSource = DataSourceStarter.getManySlaveDataSource(ioc, conf, slave_prefix);
if(slaveDataSource != null) {
NutDaoRunner runner = new NutDaoRunner();
runner.setSlaveDataSource(slaveDataSource);