You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Config Server客户端无法拉取配置文件求助

Spring Config Server客户端无法获取配置导致启动失败

我是Spring Config Server新手,搭建了Config Server和名为account-service的配置客户端:

  • Config Server的resources目录下创建了bootstrap.yml,并在resources/config目录下放了account-service.yml存储该服务的配置
  • account-service项目的resources目录下创建了bootstrap.yml

通过http://localhost:8888/account-service.yml能正常获取到配置内容,但启动account-service时,虽然能连接Config Server,却无法加载配置,导致Cassandra数据库连接属性缺失,应用启动失败。以下是相关配置、依赖及日志:


Config Server配置文件

resources/bootstrap.yml

server:
  port: 8888
spring:
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config
  profiles:
    active: native

resources/config/account-service.yml

server:
  port: 8081
microservice:
  cassandra:
    contact:
      point: 127.0.0.1
    keyspace:
      name: springcloud
    port: 9042
    username: cassandra
    password: cassandra

Account Service配置文件

resources/bootstrap.yml

spring:
  application:
    name: account-service # 对应Config Server中的account-service.yml
  cloud:
    config:
      profile: native
      uri: http://localhost:8888 # Config Server地址

依赖信息

Account Service父依赖

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <spring.cloud.version>2023.0.0</spring.cloud.version>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

Config Server依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

Account Service启动日志

2024-02-05T20:04:01.952+03:00  INFO 5086 --- [  restartedMain] c.c.c.ConfigServicePropertySourceLocator : 从服务器获取配置: http://localhost:8888
2024-02-05T20:04:02.060+03:00  INFO 5086 --- [  restartedMain] c.c.c.ConfigServicePropertySourceLocator : 找到环境: name=application, profiles=[default], label=null, version=null, state=null
2024-02-05T20:04:02.060+03:00  INFO 5086 --- [  restartedMain] b.c.PropertySourceBootstrapConfiguration : 找到属性源: [BootstrapPropertySource {name='bootstrapProperties-configClient'}]
2024-02-05T20:04:02.063+03:00  INFO 5086 --- [  restartedMain] c.a.a.AccountServiceApplication          : 未设置激活的profile,使用默认profile: "default"
2024-02-05T20:04:02.255+03:00  INFO 5086 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : 以DEFAULT模式启动Spring Data Cassandra仓库。
2024-02-05T20:04:02.260+03:00  INFO 5086 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Spring Data仓库扫描完成,耗时2毫秒。找到0个Cassandra仓库接口。
2024-02-05T20:04:02.371+03:00  INFO 5086 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : 以DEFAULT模式启动Spring Data Reactive Cassandra仓库。
2024-02-05T20:04:02.378+03:00  INFO 5086 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Spring Data仓库扫描完成,耗时6毫秒。找到0个Reactive Cassandra仓库接口。
2024-02-05T20:04:02.379+03:00  INFO 5086 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : 以DEFAULT模式启动Spring Data Cassandra仓库。
2024-02-05T20:04:02.385+03:00  INFO 5086 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Spring Data仓库扫描完成,耗时5毫秒。找到1个Cassandra仓库接口。
2024-02-05T20:04:02.452+03:00  INFO 5086 --- [  restartedMain] o.s.cloud.context.scope.GenericScope     : BeanFactory id=e6a017c7-8b55-3032-b079-c077a9801003
2024-02-05T20:04:02.619+03:00  INFO 5086 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat初始化完成,端口8080 (http)
2024-02-05T20:04:02.623+03:00  INFO 5086 --- [  restartedMain] o.apache.catalina.core.StandardService   : 启动服务 [Tomcat]
2024-02-05T20:04:02.623+03:00  INFO 5086 --- [  restartedMain] o.apache.catalina.core.StandardEngine    : 启动Servlet引擎: [Apache Tomcat/10.1.18]
2024-02-05T20:04:02.642+03:00  INFO 5086 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : 初始化Spring嵌入式WebApplicationContext
2024-02-05T20:04:02.642+03:00  INFO 5086 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : 根WebApplicationContext初始化完成,耗时574毫秒
2024-02-05T20:04:02.704+03:00  WARN 5086 --- [  restartedMain] ConfigServletWebServerApplicationContext : 上下文初始化遇到异常,取消刷新尝试: org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为'accountController'的bean失败,该bean定义在文件[/Users/ademgalitekin/Desktop/Software/Self Projects/microservice/parent/account-service/target/classes/com/adamsware/accountservice/api/AccountController.class]: 通过构造器参数0表达的依赖不满足: 创建名为'accountService'的bean失败,该bean定义在文件[/Users/ademgalitekin/Desktop/Software/Self Projects/microservice/parent/account-service/target/classes/com/adamsware/accountservice/service/AccountService.class]: 通过构造器参数0表达的依赖不满足: 创建名为'accountRepository'的bean失败,该bean定义在com.adamsware.accountservice.repository.AccountRepository,声明在CassandraRepositoriesRegistrar.EnableCassandraRepositoriesConfiguration上的@EnableCassandraRepositories中: 设置bean属性'cassandraTemplate'时无法引用bean 'cassandraTemplate'
2024-02-05T20:04:02.705+03:00  INFO 5086 --- [  restartedMain] o.apache.catalina.core.StandardService   : 停止服务 [Tomcat]
2024-02-05T20:04:02.715+03:00  INFO 5086 --- [  restartedMain] .s.b.a.l.ConditionEvaluationReportLogger : 

ApplicationContext启动失败。要显示条件评估报告,请使用'debug'参数重新运行应用。
2024-02-05T20:04:02.724+03:00 ERROR 5086 --- [  restartedMain] o.s.boot.SpringApplication               : 应用启动失败

org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为'accountController'的bean失败,该bean定义在文件[/Users/ademgalitekin/Desktop/Software/Self Projects/microservice/parent/account-service/target/classes/com/adamsware/accountservice/api/AccountController.class]: 通过构造器参数0表达的依赖不满足: 创建名为'accountService'的bean失败,该bean定义在文件[/Users/ademgalitekin/Desktop/Software/Self Projects/microservice/parent/account-service/target/classes/com/adamsware/accountservice/service/AccountService.class]: 通过构造器参数0表达的依赖不满足: 创建名为'accountRepository'的bean失败,该bean定义在com.adamsware.accountservice.repository.AccountRepository,声明在CassandraRepositoriesRegistrar.EnableCassandraRepositoriesConfiguration上的@EnableCassandraRepositories中: 设置bean属性'cassandraTemplate'时无法引用bean 'cassandraTemplate'

问题分析与解决方案

从日志可见,客户端实际请求的是application-default配置,而非预期的account-service配置,核心问题有两点:

1. 客户端未启用Bootstrap机制

Spring Cloud 2022.0.0及后续版本默认禁用Bootstrap,导致bootstrap.yml未被加载,客户端无法识别spring.application.name配置,只能默认请求application配置。

2. 客户端配置参数错误

spring.cloud.config.profile是指定要加载的配置profile,而非Config Server的运行模式。你设置的native是Config Server自身的运行模式,客户端不需要该参数。


修复步骤

步骤1:给Account Service添加Bootstrap依赖

在Account Service的pom.xml中添加:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

步骤2:修正Account Service的bootstrap.yml

移除错误的profile: native配置,若无需特定profile,可留空或指定default:

spring:
  application:
    name: account-service
  cloud:
    config:
      uri: http://localhost:8888
      # 若配置文件为account-service.yml(无profile后缀),profile可省略,默认使用default
      # profile: default

步骤3:验证Config Server配置文件位置

确保account-service.yml放在Config Server的resources/config目录下,与search-locations: classpath:/config配置对应。

步骤4:重启服务

先重启Config Server,再重启Account Service,此时客户端将正确获取account-service配置,Cassandra连接属性会被加载,解决启动失败问题。


内容的提问来源于stack exchange,提问作者Adem Galitekin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 16:57:03