【BUG分析】微服务无法读取Nacos中的共享配置

项目场景

基于Spring Cloud微服务的商城系统。

使用Nacos进行统一配置管理,在bootstrap.xml中读取配置参数。


问题描述

购物车微服务可以读取Nacos中的共享mybatis配置,商品管理微服务却读不到,启动报错提示无法配置数据库源:

powershell 复制代码
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2025-03-12 08:37:45.930 ERROR 11556 --- [           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 (the profiles local are currently active).


Process finished with exit code 1

可能的原因分析

检查bootstrap.yaml中的Nacos服务器地址和共享配置文件名字:

yaml 复制代码
spring:
  application:
    name: item-service  # 微服务名称
  profiles:
    active: dev
  cloud:
    nacos:
      server-addr: 192.168.119.128:8848
      config:
        file-extension: yaml  # 文件后缀名
        shared-configs:   # 共享配置
          - data-id: shared-jdbc.yaml # 共享mybatis配置
          - data-id: shared-log.yaml  # 共享日志配置
          - data-id: shared-swagger.yaml  # 共享日志配置
          - data-id: shared-seata.yaml

检查Nacos配置文件shared-jdbc.yaml:

yaml 复制代码
spring:
  datasource:
    url: jdbc:mysql://${hm.db.host:192.168.119.128}:${hm.db.port:3306}/${hm.db.database}?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: ${hm.db.un:root}
    password: ${hm.db.pw:123}
mybatis-plus:
  configuration:
    default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler
  global-config:
    db-config:
      update-strategy: not_null
      id-type: auto

检查application.yaml中的数据库名称:

yaml 复制代码
server:
  port: 8081
hm:
  db:
    database: hm-item
  swagger:
    title: "商城商品服务接口文档"
    package: com.hmall.item.controller

检查pom.xml的相关依赖:

xml 复制代码
		<!--Nacos统一配置管理-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!--读取bootstrap文件-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

疑问:为什么要把配置参数写在bootstrap.yaml中,而不直接写在application.yaml中?

  1. 加载顺序
    • bootstrap.yaml:在Spring Boot应用启动时,bootstrap.yaml会先于application.yaml加载。这使得Nacos配置中心的相关参数(如服务器地址、命名空间等)能在应用启动初期被读取,确保后续配置加载时Nacos已准备就绪。
    • application.yaml:application.yaml的加载在bootstrap.yaml之后,适合存放应用自身的配置。
  2. 配置优先级
    • bootstrap.yaml:bootstrap.yaml中的配置优先级高于application.yaml,确保Nacos配置参数不会被application.yaml中的配置覆盖。
  3. 环境隔离
    • bootstrap.yaml:通常用于与环境相关的配置,如Nacos服务器地址等,便于在不同环境中切换。
    • application.yaml:适合存放与环境无关的应用配置。
  4. 配置中心集成
    • bootstrap.yaml:在集成配置中心时,bootstrap.yaml用于指定配置中心的位置和认证信息,确保应用启动时能正确连接到配置中心并获取配置。

总结:将Nacos配置参数放在bootstrap.yaml中,主要是为了确保这些配置能在应用启动初期加载,并且不会被其他配置覆盖,同时便于环境隔离和配置中心集成。

相关推荐
张彦峰ZYF1 分钟前
查询接口调优:无缓存、强实时约束下的系统化性能工程
架构·业务缓存与数据库缓冲机制·强一致、读己之写·读己之写和有界陈旧语义·端到端延迟分段·一致性路由
海宇数据2 分钟前
零信任架构实战:基于海宇车型识别精准构建自动化违章处理网关
运维·人工智能·架构·自动化
CubeSandbox12 分钟前
沙箱是选项,不是标配:花椒 Agent 平台的架构思考与 Cube 实践
大数据·人工智能·架构
Thomas.Sir13 分钟前
第6课:Nacos核心原理 & 2025版适配SpringCloud环境搭建
spring cloud·微服务
Dawson Zhu1 小时前
一种多Agent权限管控与风险控制架构
人工智能·语言模型·架构·aigc·agi
海宇AI1 小时前
零信任架构实战:基于海宇学历核验版构建自动化高并发资信评估网关
人工智能·微服务·架构·自动化
阳明山水1 小时前
因果嵌入与流水线范式的本质差异
人工智能·深度学习·算法·机器学习·架构
她的男孩2 小时前
头像换了三次还是旧图:秒传 + 预签名 URL + 私有文件权限,四层缓存叠出一个 Bug
java·后端·架构
EatFan2 小时前
Spring Boot 4 升级避坑指南:依赖变化、Undertow 弃用、4.0.5 补丁与 Java 25 虚拟线程落地
java·spring boot·微服务·虚拟线程·spring boot 4
萧瑟余晖2 小时前
Dubbo 集群容错与负载均衡详解
架构·负载均衡·dubbo