Shardingsphere: springboot2.1.1 with mybatis,druid dataSource duplicate

Created on 20 Feb 2019  ·  14Comments  ·  Source: apache/shardingsphere

Question

Which version of Sharding-Jdbc do you using?(您使用的Sharding-Jdbc版本为?)

sharding-jdbc-spring-boot-starter:3.1.0
springboot: 2.1.1.release
druid-spring-boot-starter:1.1.10
mybatis-spring-boot-starter:1.3.2

Expected behavior (您预期的结果是)

正常运行

Actual behavior (实际运行的结果是)

The bean 'dataSource', defined duplicate.

Steps to reproduce the behavior (可重现问题的操作步骤)


APPLICATION FAILED TO START


Description:

The bean 'dataSource', defined in class path resource [io/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class] and overriding is disabled.

won't fix

Most helpful comment

You can use spring.main.allow-bean-definition-overriding=true configuration to resolve this error

@imhager. Can this configuration solve your problem? If still has questions, please let us know.

All 14 comments

sharding.jdbc.datasource.names=ds0

sharding.jdbc.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds0.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8&serverTimezone=GMT%2B8&useUnicode=true&useSSL=false
sharding.jdbc.datasource.ds0.username=root
sharding.jdbc.datasource.ds0.password=123456

============== 分表 =========

sharding.jdbc.config.sharding.tables.t_user.actual-data-nodes=ds0.t_user_$->{0..1}
sharding.jdbc.config.sharding.tables.t_user.table-strategy.inline.sharding-column=internal_id
sharding.jdbc.config.sharding.tables.t_user.table-strategy.inline.algorithm-expression=user_$->{internal_id.longValue() % 2}

未配置分片规则的表将通过默认数据源定位

sharding.jdbc.config.sharding.default-data-source-name=ds0

打印sql

sharding.jdbc.config.sharding.props.sql.show=true

After testing, sharding-jdbc-spring-boot-starter:3.1.0 does not support spring-boot 2.x, so sad.

We suggest to use ShardingSphere with versions [1.5.0.RELEASE,2.0.0.M1) for spring-boot,
and versions [4.3.6.RELEASE,5.0.0.M1) for spring springframework.

So sorry for the trouble.

We suggest to use ShardingSphere with versions [1.5.0.RELEASE,2.0.0.M1) for spring-boot,
and versions [4.3.6.RELEASE,5.0.0.M1) for spring springframework.

So sorry for the trouble.

When is the 2.1.x version supported?

@imhager,
As my test, ShardingSphere supports 2.1.1.RELEASE spring boot.
The error because the conflict with druid-spring-boot-starter and sharding-jdbc-spring-boot-starter

You can use spring.main.allow-bean-definition-overriding=true configuration to resolve this error

You can use spring.main.allow-bean-definition-overriding=true configuration to resolve this error

@imhager. Can this configuration solve your problem? If still has questions, please let us know.

You can use spring.main.allow-bean-definition-overriding=true configuration to resolve this error

@imhager. Can this configuration solve your problem? If still has questions, please let us know.

thank you for your help.

The bean 'dataSource', defined in class path resource [io/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

if i set this value, shardingjdbc/SpringBootConfiguration.DataSource and DruidDataSourceAutoConfigure.DataSource will not affect each other, is it?

No, DruidDataSourceAutoConfigure.DataSource will be override by shardingjdbc/SpringBootConfiguration.DataSource.
It means the bean 'dataSource' will be created by shardingjdbc/SpringBootConfiguration.DataSource instead of DruidDataSourceAutoConfigure.DataSource.

I don't know your situation, so there is no guarantee that it will not affect you.
Would you like try first in test environment? Or describe why you use DruidDataSourceAutoConfigure.DataSource?

No, DruidDataSourceAutoConfigure.DataSource will be override by shardingjdbc/SpringBootConfiguration.DataSource.
It means the bean 'dataSource' will be created by shardingjdbc/SpringBootConfiguration.DataSource instead of DruidDataSourceAutoConfigure.DataSource.

I don't know your situation, so there is no guarantee that it will not affect you.
Would you like try first in test environment? Or describe why you use DruidDataSourceAutoConfigure.DataSource?

thanks ,i'll try my test.

@imhager, have any progress for this issue? If no feedback, we will close this issue tomorrow.

If no response more than 7 days , we will close it.

@KomachiSion I encounter same problem and use the configuration you figure out. Works well. But I noticed that . This may be make some problem do not konw.
So I do not think this is the best way.

@leyou240, Do you have any idea for this problem?
We prefer you can open a new issue to show your solution.
And We welcome you to subscribe to the dev mailing list and initiate a discussion for this problem.

BTW, this is a closed issue, we should not discuss in here

Was this page helpful?
0 / 5 - 0 ratings