sharding4.1.1 版本只做分库分表,报错需要配置MasterSlaveDataSource

方便更快捷的说明问题,可以按需填写(可删除)

使用环境:

springboot 2.1.5
mybatis-plus 3.1.5
sharding-jdcb 4.1.1

场景、问题:

[2022-05-18 11:54:00.028 maxrocky-cloud-service-transfer INFO  ,, main org.apache.catalina.core.StandardService Stopping service [Tomcat]
[2022-05-18 11:54:00.645 maxrocky-cloud-service-transfer ERROR ,, main org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter 

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

Description:

A component required a bean of type 'org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.MasterSlaveDataSource' that could not be found.


Action:

Consider defining a bean of type 'org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.MasterSlaveDataSource' in your configuration.

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

Process finished with exit code 1

已进行操作:

# shardingjdbc 配置
  shardingsphere:
    datasource:
      names: ds0,ds1,ds2
      ds0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/shard_one?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
        username: root
        password: root
      ds1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/shard_two?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
        username: root
        password: root
      ds2:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/shard_three?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
        username: root
        password: root
    sharding:
      tables:
        sys_user:
          actual-data-nodes: ds$->{0..2}.sys_user$->{0..2}
          table-strategy:
            inline:
              sharding-column: id
              algorithm-expression: sys_user$->{id % 2}
          keyGenerator:
            type: SNOWFLAKE
            column: id
      default-database-strategy:
        inline:
          sharding-column: id
          algorithm-expression: sys_user$->{id % 2}
  # 动态数据源配置
  datasource:
    dynamic:
      datasource:
        master:
          type: com.alibaba.druid.pool.DruidDataSource
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://localhost:3306/maxrocky?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
          username: root
          password: root
      # 指定默认数据源名称 此处指定 master 为默认数据源。
      primary: master

现状:

搭建是按照sharding的官方demo搭建的。我只做分库分表,不需要主从,也不需要读写分离。

京ICP备2021015875号