dynamic-datasource icon indicating copy to clipboard operation
dynamic-datasource copied to clipboard

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

Open ldcsaa opened this issue 1 year ago • 6 comments

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();
    }
}

ldcsaa avatar Apr 08 '24 06:04 ldcsaa

我觉得应该让他改。

huayanYu avatar Apr 08 '24 07:04 huayanYu

我觉得应该让他改。

你们可否沟通一下~

ldcsaa avatar Apr 08 '24 20:04 ldcsaa

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

MRwuyan avatar Apr 11 '24 14:04 MRwuyan

我的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,再次启动可以正常启动,我不知道跟这个话题有没有关系,但是我想知道为什么会导致出现这个情况,感谢

oah1021 avatar Apr 12 '24 03:04 oah1021

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

MRwuyan avatar Apr 12 '24 05:04 MRwuyan

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

Hwwwww-dev avatar Apr 13 '24 15:04 Hwwwww-dev

问题一样,能否迭代?

Umenezumi avatar May 20 '24 02:05 Umenezumi

问题一样,能否迭代?

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

huayanYu avatar May 20 '24 02:05 huayanYu

问题一样,能否迭代?

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

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

Umenezumi avatar May 20 '24 02:05 Umenezumi