nutzam / nutzboot

NutzBoot,简称NB,是可靠的企业级微服务框架,提供自动配置,嵌入式web服务,分布式会话,服务治理,负载均衡,hystrix,RPC等一篮子解决方案
https://nutz.io
Apache License 2.0
501 stars 138 forks source link

提一个多数据源的想法 #116

Closed swtseaman closed 6 years ago

swtseaman commented 6 years ago

今天想了一个多数据源的事情,不知道是否可行 是看了现在的NB的一主多从想出来的,按照一个标准配置来即可 比如我希望注入dbADao、dbBDao、dbCDao那么我的配置文件这样写

# dbADao Master
jdbc.dbADao.url=****
jdbc.dbADao.username=****
jdbc.dbADao.password=****
# dbADao Slaves
jdbc.dbADao.slave.A.url=****
jdbc.dbADao.slave.A.username=****
jdbc.dbADao.slave.A.password=****
jdbc.dbADao.slave.B.url=****
jdbc.dbADao.slave.B.username=****
jdbc.dbADao.slave.B.password=****
########################################
# dbBDao Master
jdbc.dbBDao.url=****
jdbc.dbBDao.username=****
jdbc.dbBDao.password=****
# dbBDao Slaves
jdbc.dbBDao.slave.A.url=****
jdbc.dbBDao.slave.A.username=****
jdbc.dbBDao.slave.A.password=****
jdbc.dbBDao.slave.B.url=****
jdbc.dbBDao.slave.B.username=****
jdbc.dbBDao.slave.B.password=****
########################################
# dbCDao Master
jdbc.dbCDao.url=****
jdbc.dbCDao.username=****
jdbc.dbCDao.password=****
# dbCDao Slaves
jdbc.dbCDao.slave.A.url=****
jdbc.dbCDao.slave.A.username=****
jdbc.dbCDao.slave.A.password=****
jdbc.dbCDao.slave.B.url=****
jdbc.dbCDao.slave.B.username=****
jdbc.dbCDao.slave.B.password=****
########################################

以上方式配置好后,在程序内我们就可以按规则注入使用了,如:

@Inject
protected Dao dbADao;
@Inject
protected Dao dbBDao;
@Inject
protected Dao dbCDao;

@At("/count")
@Ok("json")
public NutMap getCount() {
    int aCount = dbADao.count("table");
    int bCount = dbBDao.count("table");
    int cCount = dbCDao.count("table");
    NutMap result = new NutMap();
    result.put("acount", aCount);
    result.put("bcount", bCount);
    result.put("count", cCount);
    return result;
}
EggsBlue commented 6 years ago

神州行,我看行。

swtseaman commented 6 years ago

哈哈,你是在oschina上给我点赞的蛋蛋吧

tiankongkm commented 6 years ago

很少人用properties来配置ioc对象吧,而且有实现PropertiesIocLoader

wendal commented 6 years ago

能搞但没必要吧,一般就连接单个数据源, 更多的数据源, 可以通过 ioc js 或者@IocBean来声明

swtseaman commented 6 years ago

使用DaoUp来解决吗

wendal commented 6 years ago
@IocBean
public class MyBeans {

        @Inject
        PropertiesProxy conf;

        @IocBean(name="aDao")
        public Dao abcDao() {
                 return new NutDao(conf.make(DruidDataSource.class, "jdbcA."));
        }
        @IocBean(name="bDao")
        public Dao abcDao() {
                 return new NutDao(conf.make(DruidDataSource.class, "jdbcB."));
        }
        @IocBean(name="cDao")
        public Dao cDao() {
                 return new NutDao(conf.make(DruidDataSource.class, "jdbcC."));
        }

}
swtseaman commented 6 years ago

这种写法确实方便,但不知道是否支持主从

wendal commented 6 years ago

NutDao本身就支持,看NutDaoStarter里面的逻辑

swtseaman commented 6 years ago

但是NutDao是org.nutz.dao.impl.NutDao里面的,跟NutDaoStarter能联系起来吗?

wendal commented 6 years ago

为啥不能联系起来?我的意思是去看看里面的逻辑

swtseaman commented 6 years ago

我看了

// 看看是不是需要注入从数据库
        if (Lang.loadClassQuite("org.nutz.boot.starter.jdbc.DataSourceStarter") != null) {
            DataSource slaveDataSource = DataSourceStarter.getSlaveDataSource(ioc, conf);
            if (slaveDataSource != null) {
                NutDaoRunner runner = new NutDaoRunner();
                runner.setSlaveDataSource(slaveDataSource);
                dao.setRunner(runner);
            }
        }

我的意思是你上面写的MyBeans里面是new出来的NutDao,会经过NutDaoStarter的处理吗? 而且NutDaoStarter里面的处理只处理一主一从,我想实现NutzBoot你最新支持的这种一主多从

wendal commented 6 years ago

^_^ 如果getSlaveDataSource也能传前缀,问题就解决了?

swtseaman commented 6 years ago

比如getSlaveDataSources返回一个DataSource数组,嘿嘿

swtseaman commented 6 years ago

@wendal 能否解说下,new NutDao的时候,NutDaoStarter是什么时候介入的

swtseaman commented 6 years ago

我debug先看看执行过程

wendal commented 6 years ago
        @IocBean(name="bDao")
        public Dao abcDao() {
                 NutDao dao = new NutDao(conf.make(DruidDataSource.class, "jdbcB."));
                 DataSource slave = DataSourceStarter.getSlaveDataSource(ioc, conf, "jdbcB.slave.");
                 if (slave != null) {
                     NutDaoRunner runner = new NutDaoRunner();
                     runner.setSlaveDataSource(slave);
                     dao.setRunner(slave);
                 }
                 return dao;
        }
wendal commented 6 years ago

我的本意只是让你去看看NutDaoStarter的逻辑...

swtseaman commented 6 years ago

额,理解了,getSlaveDataSource已经实现了多从库加入,thanks

swtseaman commented 6 years ago

坐等快照更新,哈哈!