NutzCN Logo
问答 nutzboot sharding-jdbc 怎么配置多个数据源?创建多个dao
发布于 2519天前 作者 qq_43bc7d85 5556 次浏览 复制 上一个帖子 下一个帖子
标签:

本人新手,单数据源分库分表已经可用,但不知道如何添加另外的数据源获取另一个dao。

37 回复

。。。 还有这种操作?

@IocBean
public class MyBeans{
	@Inject
	protected PropertiesProxy conf;

	@Inject
	protected AppContext appContext;

	@IocBean(name="dao2")
	publicDao getDao2() throws Exception {
		String path = "shardingjdbc.yaml2";
		InputStream ins = appContext.getResourceLoader().get(path);
		if (ins == null) {
			File f = new File(path);
			if (f.exists() && f.canRead()) {
				ins = new FileInputStream(f);
			} else {
				throw new RuntimeException("no such shardingjdbc configure file=" + path);
			}
		}
		return new NutDao(ShardingDataSourceFactory.createDataSource(Streams.readBytesAndClose(ins)));
	}
}

文件名写错,意会意会。。。

是再建一个shardingjdbc.yaml文件然后获取吗?我试试,谢谢大大

是的

其实呢... 很多starter的功能就是创建各种ioc bean ...

对了... 路径最好从conf.get("xxxx", "默认值") 取, 方便以后扩展

protected static String PRE = "shardingjdbc2.";
    @PropDoc(group = "shardingjdbc2", value = "配置文件路径", defaultValue = "shardingjdbc2.yaml")
    public static final String PROP_PATH = PRE + "path";

    @Inject
    protected PropertiesProxy conf;


    @Inject
    protected AppContext appContext;

    @IocBean(name="dao2")
    public  Dao getDao2() throws Exception {
        String path = conf.get(PROP_PATH, "shardingjdbc2.yaml");
        InputStream ins = appContext.getResourceLoader().get(path);
        if (ins == null) {
            File f = new File(path);
            if (f.exists() && f.canRead()) {
                ins = new FileInputStream(f);
            } else {
                throw new RuntimeException("no such shardingjdbc configure file=" + path);
            }
        }
        return new NutDao(ShardingDataSourceFactory.createDataSource(Streams.readBytesAndClose(ins)));
    }

这个写法有问题吗?为什么获取不到文件。

at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:240)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:270)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:116)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
	at org.nutz.mvc.WhaleFilter.doFilter(WhaleFilter.java:159)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
	at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:206)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.Server.handle(Server.java:530)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:347)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:256)
	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
	at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)
	at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:382)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:708)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:626)
	at java.lang.Thread.run(Thread.java:748)
Caused by: org.nutz.lang.born.BorningException: Fail to born or cast to 'com.bbieat.pay.module.zhangdanModule'because: java.lang.NullPointerException
	at org.nutz.lang.born.EmptyArgsConstructorBorning.born(EmptyArgsConstructorBorning.java:17)
	at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:78)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:117)
	... 46 more
Caused by: java.lang.NullPointerException
	at com.bbieat.pay.bean.MyBeans.getDao2(MyBeans.java:32)
	at com.bbieat.pay.module.zhangdanModule.<init>(zhangdanModule.java:33)
	at com.bbieat.pay.module.zhangdanModule$FC$62febe830a4a6764c634d9e263964cee.invoke(zhangdanModule.java)
	at org.nutz.lang.born.AbstractConstructorBorning.call(AbstractConstructorBorning.java:25)
	at org.nutz.lang.born.EmptyArgsConstructorBorning.born(EmptyArgsConstructorBorning.java:14)
	... 48 more

"com.bbieat.pay.bean.MyBeans.getDao2(MyBeans.java:32)" 具体是哪一行呢?

另外, 尽量不要在构造方法内获取其他bean

String path = conf.get(PROP_PATH, "shardingjdbc2.yaml");

贴一下zhangdanModule的构造方法, 你不会是直接new MyBeans的吧?

就是直接new MyBeans的...

其他方法怎么获取?

private Dao dao2= new MyBeans().getDao2();

@IocBean
public class zhangdanModule {

         @Inject
         public Dao dao2;

}

文件是放resources下的

像我写的那样,用@Inject

@Inject
         public Dao dao2;

这个我试过了,获取到的是我另一个dataSources

debug一下getDao2方法,看看有没有进入

debug看了下没有进入,难道是因为我另一个没写这类有影响?

"另外一个没写这个类"是什么意思??? 你把MyBeans放在什么package了? 我刚刚用 nutzboot-demo-sharding-jdbc 试了一下是ok的

MyBeans需要放在MainLauncher的package或者子package下面才能被扫描到的

我放到了bean下面了,难道这类要放到指定package里?

不是, 你是不是忘记写@IocBean了?

MyBeans类之上写@IocBean

@IocBean加上后dao2的配置文件是能获取到了两个都崩了,

dataSources:
ami: !!com.alibaba.druid.pool.DruidDataSource
    driverClassName: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@**.***.com:1522:ami
    username: *****
    password: *****
    filters: stat

这Oracle的配置格式是有问题吗

For the reason: Unable to find property 'ami' on class: io.shardingjdbc.core.yaml.sharding.YamlShardingConfiguration
	at org.nutz.lang.Invoking.invoke(Invoking.java:195)
	at org.nutz.lang.Mirror.invoke(Mirror.java:1120)
	at org.nutz.ioc.impl.ObjectMakerImpl$1.born(ObjectMakerImpl.java:80)
	at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:78)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:117)
	... 51 more
Caused by: org.yaml.snakeyaml.error.YAMLException: Unable to find property 'ami' on class: io.shardingjdbc.core.yaml.sharding.YamlShardingConfiguration
	at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:132)
	at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:121)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.getProperty(Constructor.java:322)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:240)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:189)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:345)
	at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182)
	at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:141)
	at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:127)
	at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:450)
	at org.yaml.snakeyaml.Yaml.loadAs(Yaml.java:444)
	at io.shardingjdbc.core.api.ShardingDataSourceFactory.unmarshal(ShardingDataSourceFactory.java:126)
	at io.shardingjdbc.core.api.ShardingDataSourceFactory.createDataSource(ShardingDataSourceFactory.java:98)
	at com.bbieat.pay.bean.MyBeans.getDao2(MyBeans.java:42)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.nutz.lang.Invoking$NullArgInvoker.invoke(Invoking.java:77)
	at org.nutz.lang.Invoking.invoke(Invoking.java:192)
	... 55 more

缩进错了

dataSources:
  ami:
    driverClassName: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@**.***.com:1522:ami
    username: *****
    password: *****
    filters: stat

org.nutz.ioc.IocException: IocBean[zhangdanModule] throw Exception when creating
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:240)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:270)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:116)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
	at org.nutz.mvc.WhaleFilter.doFilter(WhaleFilter.java:159)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
	at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:206)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.Server.handle(Server.java:530)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:347)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:256)
	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
	at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)
	at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:382)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:708)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:626)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: IocBean[zhangdanModule] fail at field=[dao2]
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:40)
	at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:67)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
	... 46 more
Caused by: org.nutz.ioc.IocException: IocBean[dao2] throw Exception when creating
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:240)
	at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:60)
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
	... 48 more
Caused by: org.nutz.lang.InvokingException: Fail to invoke [com.bbieat.pay.bean.MyBeans].getDao2() by args:
 
For the reason: null
	at org.nutz.lang.Invoking.invoke(Invoking.java:195)
	at org.nutz.lang.Mirror.invoke(Mirror.java:1120)
	at org.nutz.ioc.impl.ObjectMakerImpl$1.born(ObjectMakerImpl.java:80)
	at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:78)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:117)
	... 51 more
Caused by: java.lang.NullPointerException
	at io.shardingjdbc.core.yaml.sharding.YamlShardingRuleConfiguration.getShardingRuleConfiguration(YamlShardingRuleConfiguration.java:72)
	at io.shardingjdbc.core.yaml.sharding.YamlShardingConfiguration.getShardingRule(YamlShardingConfiguration.java:50)
	at io.shardingjdbc.core.api.ShardingDataSourceFactory.createDataSource(ShardingDataSourceFactory.java:99)
	at com.bbieat.pay.bean.MyBeans.getDao2(MyBeans.java:42)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.nutz.lang.Invoking$NullArgInvoker.invoke(Invoking.java:77)
	at org.nutz.lang.Invoking.invoke(Invoking.java:192)
	... 55 more

debug看了下conf里面没有shardingjdbc的路径,这段是不是有问题?

protected static String PRE = "shardingjdbc2.";
    @PropDoc(group = "shardingjdbc2", value = "配置文件路径", defaultValue = "shardingjdbc2.yaml")
    public static final String PROP_PATH = PRE + "path";

    @Inject
    protected PropertiesProxy conf;

shardingjdbc2.yaml 完整贴一下, 隐藏一下密码.

dataSources:
  ami: !!com.alibaba.druid.pool.DruidDataSource
    driverClassName: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@hz.bbieat.com:1522:ami
    username:
    password: 
    filters: stat
shardingRule:
                    tables:

没了??? 连tables 的缩进都不对啊...

tables下面没做分割不影响吧

    public ShardingRuleConfiguration getShardingRuleConfiguration() throws SQLException {
        ShardingRuleConfiguration result = new ShardingRuleConfiguration();
        result.setDefaultDataSourceName(defaultDataSourceName);
        for (Map.Entry<String, YamlTableRuleConfiguration> entry : tables.entrySet()) { // 这里就是报错的72行, tables不配就是null了
            YamlTableRuleConfiguration tableRuleConfig = entry.getValue();
            tableRuleConfig.setLogicTable(entry.getKey());
            result.getTableRuleConfigs().add(tableRuleConfig.build());
        }

tables下面必须至少写一个表吗?

OK,现在两个都能正常运行,谢谢大大

添加回复
请先登陆
回到顶部