Akreim 2025-11-08 18:08 采纳率: 0%
浏览 20

有关使用最新版springcloud alibb启动微服务无法从nacos中获取数据库配置的问题


  <spring.boot.version>3.3.5</spring.boot.version>
    <!-- Spring Cloud 2023.x 版本 -->
    <spring.cloud.version>2023.0.3</spring.cloud.version>
    <!-- Spring Cloud Alibaba 版本 -->
    <alibaba.cloud.version>2023.0.1.3</alibaba.cloud.version>

上面是我使用的spring各个框架相关的版本,我先启动了我的nacos注册中i性能,然后当我的项目启动其中一个微服务的时候,会报如下错误:
2025-11-08T14:43:53.072+08:00 WARN 3624 --- [mainServer] [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sysTestController' defined in file [D:\Code\SpringCloud-New-BackEnd\mainServer\target\classes\com\sys\apply\controller\SysTestController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'sysConfigServiceImpl' defined in file [D:\Code\SpringCloud-New-BackEnd\mainServer\target\classes\com\sys\apply\service\impl\SysConfigServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'sysConfigMapper' defined in file [D:\Code\SpringCloud-New-BackEnd\mainServer\target\classes\com\sys\apply\mapper\SysConfigMapper.class]: Cannot resolve reference to bean 'sqlSessionTemplate' while setting bean property 'sqlSessionTemplate'
2025-11-08T14:43:53.073+08:00 INFO 3624 --- [mainServer] [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2025-11-08T14:43:53.085+08:00 INFO 3624 --- [mainServer] [ main] .s.b.a.l.ConditionEvaluationReportLogger :

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2025-11-08T14:43:53.098+08:00 ERROR 3624 --- [mainServer] [ main] 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).

也就是无法读取数据库配置的错误,但是我已经在nacos中写了数据库的配置,如果换成之前springboot2.x的版本则会正常,但是用新项目(也就是当前这个项目,springboot3.x版本的)就会出现无法从nacos中读取数据库配置的问题

这里附上我的bootstrap.yaml:

# spring配置
spring:
  application:
    name: mainServer
  cloud:
    nacos:
      # 用nacos做配置中心
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
        username: nacos
        password: szy89545
#        import-check:
#          enabled: false  # 禁用导入检查
#        extension-configs:
#          - data-id: mainServer.yaml
#            group: DEFAULT_GROUP
#            refresh: true
      discovery:
        server-addr: ${spring.cloud.nacos.config.server-addr}
        username: ${spring.cloud.nacos.config.username}
        password: ${spring.cloud.nacos.config.password}

server:
  address: 0.0.0.0
  port: 8090
  servlet:
    encoding:
      charset: UTF-8
      force: true
      enabled: true

有没有老哥知道是怎么回事

  • 写回答

5条回答 默认 最新

  • 阿里嘎多学长 2025-11-08 18:23
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    问题分析

    你使用最新版的Spring Cloud Alibaba启动微服务,无法从Nacos中获取数据库配置。

    解决方案

    1. 检查Nacos配置 确保Nacos服务已经启动,并且配置正确。检查Nacos的配置文件是否正确,是否有数据库配置项。
    2. 检查Spring Cloud Alibaba配置 检查Spring Cloud Alibaba的配置文件,是否正确配置了Nacos作为配置中心。确保配置文件中有以下配置:
    spring:
      cloud:
        nacos:
          config:
            server-addr: # Nacos 服务地址
            namespace: # Nacos 命名空间
            group: # Nacos 组
            file-extension: properties # 配置文件扩展名
    
    1. 检查数据库配置 检查数据库配置是否正确。确保数据库连接信息(如用户名、密码、连接地址等)正确。
    2. 检查Spring Boot 版本 检查Spring Boot 版本是否支持Spring Cloud Alibaba。根据你的配置文件中显示的Spring Boot 版本(3.3.5),应该支持Spring Cloud Alibaba。

    核心代码

    spring:
      cloud:
        nacos:
          config:
            server-addr: 127.0.0.1:8848
            namespace: 
            group: 
            file-extension: properties
    
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableConfigClient
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    如果以上步骤都检查过了,仍然无法解决问题,请提供更多详细信息和日志输出。

    评论

报告相同问题?

问题事件

  • 创建了问题 11月8日