apache / shardingsphere

Distributed SQL transaction & query engine for data sharding, scaling, encryption, and more - on any database.
Apache License 2.0
19.8k stars 6.7k forks source link

springboot2.1.1 with mybatis,druid dataSource duplicate #1900

Closed imhager closed 5 years ago

imhager commented 5 years ago

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.

imhager commented 5 years ago

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.tuser$->{0..1} sharding.jdbc.config.sharding.tables.t_user.table-strategy.inline.sharding-column=internal_id sharding.jdbc.config.sharding.tables.tuser.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

imhager commented 5 years ago

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

KomachiSion commented 5 years ago

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.

imhager commented 5 years ago

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?

KomachiSion commented 5 years ago

@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

KomachiSion commented 5 years ago

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

KomachiSion commented 5 years ago

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.

imhager commented 5 years ago

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?

KomachiSion commented 5 years ago

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?

imhager commented 5 years ago

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.

KomachiSion commented 5 years ago

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

KomachiSion commented 5 years ago

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

leyou240 commented 5 years ago

@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.

KomachiSion commented 5 years ago

@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