springboot shardingjdbc与druid数据源冲突解决

首先看错误信息

cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class]: Invocation of init method failed; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

如上所示,DruidDataSourceAutoConfigure Failed to determine a suitable driver class,即druid找不到mysql driver,然而mysql的驱动包啥的都没问题,于是直接点进DruidDataSourceAutoConfigure查看源码

如上,标红表明druid是根据spring.datasource.druid找jdbc属性的,如果not found,则根据spring.datasource找jdbc属性,一般而言这是不会出现错误的。但是我这里使用了shardingjdbc,配置如下:

spring:
  shardingsphere:
    datasource:
      names: ds0,ds1
      ds0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://xxx:3306/ds0
        username: xxx
        password: xxx
      ds1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://xxx:3306/ds1
        username: xxx
        password: xxx
    sharding:
      tables:
        user:
          actual-data-nodes: ds${0..1}.user${0..1}
          database-strategy:
            inline:
              sharding-column: id
              algorithm-expression: ds${id % 2}
          table-strategy:
            inline:
              sharding-column: id
              algorithm-expression: user$->{id % 2}
          key-generator:
            type: SNOWFLAKE
            column: order_id
    props:
      sql:
        show: true
      executor:
        size: 12

就很显然了,他根据spring.datasource.druid或者spring.datasource确实找不到,因为我的结构是spring.shardingsphere.datasource。

解决方式1:

如果我们用的jar包是druid-spring-boot-starter,则在启动类上排除druid自动配置

@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})

解决方式2:

不用


    com.alibaba
    druid-spring-boot-starter
    1.1.22

改为


    com.alibaba
    druid
    1.1.22

即可

 

你可能感兴趣的:(druid,shardingjdbc)