不思议探求者 2023-10-08 10:34 采纳率: 100%
浏览 25
已结题

spring boot 框架整合MyBatisPlu、ShardingSphere,实现 MySQL主从读写分离(一主一从)出现错误

我在学习使用spring boot 框架整合MyBatisPlu、ShardingSphere、来实现 MySQL主从读写分离(一主一从),其中主库为本地库从库为本地安装的Docker库。已经过测试:本地机器均可以连接主与从库

但是当我运行项目时得到了如下错误:

WARN 9040 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'dataSource': Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class
2023-10-08T10:16:13.361+08:00  INFO 9040 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-10-08T10:16:13.443+08:00  INFO 9040 --- [  restartedMain] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-10-08T10:16:13.469+08:00 ERROR 9040 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
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 a suitable driver class


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 (no profiles are currently active).

下面是我的配置信息
application.yml:


server:
    port: 8080

spring:
    shardingsphere:
        datasource:
            # 数据源名称,多数据源以逗号分隔
            names:
                master,slave
            #主数据源
            master:
                type: com.alibaba.druid.pool.DruidDataSource
                driver-class-name: com.mysql.cj.jdbc.Driver
                url: jdbc:mysql://localhost:3306/rw?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
                username: root
                password: 5508769123
            #从数据源
            slave:
                type: com.alibaba.druid.pool.DruidDataSource
                driver-class-name: com.mysql.cj.jdbc.Driver
                url: jdbc:mysql://localhost:3309/rw?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
                username: root
                password: 5508769123
        masterslave:
            #读写分离配置
            load-balance-algorithm-type: round_robin #轮询
            #最终的数据源名称
            name: dataSource
            #主数据源名称
            master-data-source-name: master
            #从库数据源名称列表,多个逗号分隔
            slave-data-source-names: slave

        # 属性配置
        props:
            sql:
                show: true #开启SQL显示,默认为false
    main:
        allow-bean-definition-overriding: true


mybatis-plus:
    configuration:
        map-underscore-to-camel-case: true
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    global-config:
        db-config:
            id-type: ASSIGN_ID

pom.xml


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.39</version>
        </dependency>

        <!-- 导入读写分离坐标 -->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC1</version>
        </dependency>
  • 写回答

11条回答 默认 最新

  • hydk-ctrl 2023-10-08 14:32
    关注

    这个报错是因为SpringBoot自动配置时,检测到我们添加了Mysql、Mybatis等和数据库相关的依赖包,但是我们的配置文件中没有添加数据库的相关配置导致的。

    根据你提供的信息,一般从3个方面解决:

    1. application.yml中缺少datasource的相关配置或者配置错误
    2. 在SpringBootApplication注解中进行数据库配置的排除,添加 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
    3. 你自己写的datasource配置类错误,这个需要你检查一下,或者把代码贴出来看看
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(10条)

报告相同问题?

问题事件

  • 系统已结题 10月17日
  • 已采纳回答 10月9日
  • 创建了问题 10月8日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀