baomidou / dynamic-datasource

dynamic datasource for springboot 多数据源 动态数据源 主从分离 读写分离 分布式事务
https://www.kancloud.cn/tracy5546/dynamic-datasource/2264611
Apache License 2.0
4.69k stars 1.19k forks source link

druid 1.2.22 的 DruidDataSourceAutoConfigure 改动导致与 dynamic-datasource 不兼容 #632

Closed ldcsaa closed 4 months ago

ldcsaa commented 6 months ago

duird 1.2.22 之前的版本,DruidDataSourceAutoConfigure 定义了 DataSource Bean,该Bean定义会被 dynamic-datasource 的 DataSource Bean覆盖,所以没有问题。

public class DruidDataSourceAutoConfigure {
    private static final Logger LOGGER = LoggerFactory.getLogger(DruidDataSourceAutoConfigure.class);

    /**
     * Not setting initMethod of annotation {@code @Bean} is to avoid failure when inspecting
     * the bean definition at the build time. The {@link DruidDataSource#init()} will be called
     * at the end of {@link DruidDataSourceWrapper#afterPropertiesSet()}.
     *
     * @return druid data source wrapper
     */
    @Bean
    @ConditionalOnMissingBean
    public DataSource dataSource() {
        LOGGER.info("Init DruidDataSource");
        return new DruidDataSourceWrapper();
    }
}

但 duird 1.2.22 的 DruidDataSourceAutoConfigure 把 Bean 类型改为了 DruidDataSourceWrapper,dynamic-datasource 不能覆盖这个Bean定义。所以,由于没有相应的druid配置,导致加载 DruidDataSourceWrapper Bean 失败。

public class DruidDataSourceAutoConfigure {
    private static final Logger LOGGER = LoggerFactory.getLogger(DruidDataSourceAutoConfigure.class);

    /**
     * Not setting initMethod of annotation {@code @Bean} is to avoid failure when inspecting
     * the bean definition at the build time. The {@link DruidDataSource#init()} will be called
     * at the end of {@link DruidDataSourceWrapper#afterPropertiesSet()}.
     *
     * @return druid data source wrapper
     */
    @Bean
    @ConditionalOnMissingBean
    public DruidDataSourceWrapper dataSource() {
        LOGGER.info("Init DruidDataSource");
        return new DruidDataSourceWrapper();
    }
}
huayanYu commented 6 months ago

我觉得应该让他改。

ldcsaa commented 6 months ago

我觉得应该让他改。

你们可否沟通一下~

MRwuyan commented 5 months ago

搞了2天,发现这个坑麻了

oah1021 commented 5 months ago

我的druid版本是1.1.22 , dynamic 版本是3.2.0 下面是我的yaml配置

spring:
  autoconfigure:
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
  datasource:
    dynamic:
      primary: test1
      datasource:
        test1:
          url: jdbc:mysql://192.168.1.1:5/test1
          username: root
          password: root
        test2:
          url: jdbc:mysql://192.168.1.1:5/test2
          username: root
          password: root

当我启动时,会抛出如下错误

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine suitable jdbc url

Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (the profiles test are currently active).

Disconnected from the target VM, address: '127.0.0.1:14021', transport: 'socket'

Process finished with exit code 1

我将dynamic 的版本降到了2.5.6,再次启动可以正常启动,我不知道跟这个话题有没有关系,但是我想知道为什么会导致出现这个情况,感谢

MRwuyan commented 5 months ago

就是一楼说的原因啊,druid版本改成1.2.21也行

HwwwwwLemon commented 5 months ago

启动类加个注解也可以暂时解决 @SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)

Umenezumi commented 4 months ago

问题一样,能否迭代?

huayanYu commented 4 months ago

问题一样,能否迭代?

@Umenezumi 联系druid哦。 我这边不会更改

Umenezumi commented 4 months ago

问题一样,能否迭代?

@Umenezumi 联系 druid 哦。 我这边不会更改

看了下 druid 那边,确实不需要您修改,他们会在下一个版本解决这个问题,劳烦您到时更新 Readme ,避免大家使用 1.2.22 版本