【已解决】SpringCloudConfig客户端启动无法读取到配置参数

自己部署了一个Spring Cloud微服务项目,实践Spring Cloud Config分布式配置组件,按照Spring Cloud Config 资料Config:Spring Cloud分布式配置组件 先后创建了Eureka注册中心服务、 Spring Cloud Config Server服务、 Spring Cloud Config Client客户端,在最后启动 Spring Client Config Client 客户端时,客户端始终无法访问 Config Server服务,读取上传在Gitee上的配置文件的内容。

在Baidu、 Google搜索了大量资料,问题是最终解决了,但是这其中的原因,还需要继续探讨。

Eureka注册中心和Spring Cloud Config Server的配置内容就不多讲,可参考Eureka:Spring Cloud服务注册与发现组件Config:Spring Cloud分布式配置组件,启动了 Config Server 服务,并用浏览器访问,上传在Gitee上的参数文件的内容是可以正常获取到的

在这里插入图片描述

我们重点说一下Spring Cloud Config Client的配置,yml文件配置如下:

server:
  port: 3355 #端口号
spring:
  application:
    name: spring-cloud-config-client #服务名
  cloud:
    config:
      label: master #分支名称
      name: application  #配置文件名称,application-dev.yml 中的 config
      profile: dev  #环境名  application-dev.yml 中的 dev
      #这里不要忘记添加 http:// 否则无法读取
      uri: http://localhost:3344 #Spring Cloud Config 服务端(配置中心)地址

eureka:
  client: #将客户端注册到 eureka 服务列表内
    service-url:
      defaultZone: http://localhost:9900/eureka

新增Controller类,用于测试配置文件内容的读取

@RestController
@RequestMapping("/config/client")
public class ConfigClientController {

    @Value("${server.port}")
    private String serverPort;

    @Value("${config.info}")
    private String configInfo;

    @Value("${config.version}")
    private String configVersion;

    @GetMapping(value = "/getConfig")
    public String getConfig() {
        return "info:" + configInfo + "<br/>version:" 
        	+ configVersion + "<br/>port:" + serverPort;
    }

}

Spring Cloud Config Client客户端在启动的时候控制台报错:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'config.info' in value "${config.info}"
	at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180) ~[spring-core-5.3.23.jar:5.3.23]
	at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-5.3.23.jar:5.3.23]
	at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239) ~[spring-core-5.3.23.jar:5.3.23]
	at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-5.3.23.jar:5.3.23]
	at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:191) ~[spring-context-5.3.23.jar:5.3.23]
	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:936) ~[spring-beans-5.3.23.jar:5.3.23]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1332) ~[spring-beans-5.3.23.jar:5.3.23]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.23.jar:5.3.23]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:656) ~[spring-beans-5.3.23.jar:5.3.23]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) ~[spring-beans-5.3.23.jar:5.3.23]
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.23.jar:5.3.23]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.23.jar:5.3.23]
	... 16 common frames omitted

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

Process finished with exit code 1

遂检查配置文件,对照资料教程看是不是自己写错了。在检查 Config Client 模块的配置文件时发现,资料上创建的配置文件名称是 bootstrap.yml 而非 application.yml

遂把配置文件名改为 bootstrap.yml, 重新启动,发现没有报之前的错误了。但是服务也没有正常运行起来,而是直接停止了,控制台输出:

2023-02-01 20:42:52.002  INFO 15096 --- [           main] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 30
2023-02-01 20:42:52.005  INFO 15096 --- [           main] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
2023-02-01 20:42:52.012  INFO 15096 --- [           main] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1675255372011 with initial instances count: 2
2023-02-01 20:42:52.015  INFO 15096 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application SPRING-CLOUD-CONFIG-CLIENT with eureka with status UP
2023-02-01 20:42:52.016  INFO 15096 --- [           main] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1675255372016, current=UP, previous=STARTING]
2023-02-01 20:42:52.018  INFO 15096 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SPRING-CLOUD-CONFIG-CLIENT/DESKTOP-A5PHDVG:spring-cloud-config-client:3355: registering service...
2023-02-01 20:42:52.033  INFO 15096 --- [           main] c.s.c.ConfigClientApplication            : Started ConfigClientApplication in 10.224 seconds (JVM running for 10.92)
2023-02-01 20:42:52.040  INFO 15096 --- [ionShutdownHook] o.s.c.n.e.s.EurekaServiceRegistry        : Unregistering application SPRING-CLOUD-CONFIG-CLIENT with eureka with status DOWN
2023-02-01 20:42:52.040  INFO 15096 --- [ionShutdownHook] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1675255372040, current=DOWN, previous=UP]
2023-02-01 20:42:52.042  INFO 15096 --- [ionShutdownHook] com.netflix.discovery.DiscoveryClient    : Shutting down DiscoveryClient ...
2023-02-01 20:42:52.076  INFO 15096 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SPRING-CLOUD-CONFIG-CLIENT/DESKTOP-A5PHDVG:spring-cloud-config-client:3355 - registration status: 204
2023-02-01 20:42:52.077  INFO 15096 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SPRING-CLOUD-CONFIG-CLIENT/DESKTOP-A5PHDVG:spring-cloud-config-client:3355: registering service...
2023-02-01 20:42:52.080  INFO 15096 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SPRING-CLOUD-CONFIG-CLIENT/DESKTOP-A5PHDVG:spring-cloud-config-client:3355 - registration status: 204
2023-02-01 20:42:52.081  INFO 15096 --- [ionShutdownHook] com.netflix.discovery.DiscoveryClient    : Unregistering ...
2023-02-01 20:42:52.085  INFO 15096 --- [ionShutdownHook] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SPRING-CLOUD-CONFIG-CLIENT/DESKTOP-A5PHDVG:spring-cloud-config-client:3355 - deregister  status: 200
2023-02-01 20:42:52.091  INFO 15096 --- [ionShutdownHook] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient
Disconnected from the target VM, address: '127.0.0.1:58233', transport: 'socket'

Process finished with exit code 0

其中有一句 Registering application SPRING-CLOUD-CONFIG-CLIENT with eureka with status UP

查询资料,在这篇文章SpringCloud中Client向Eureka注册中心注册服务成功后不久就Unregistering(Unregistering application 服务名 with eureka with)中有提出解决办法

虽然 Config Client 子模块依赖的父模块中,pom文件已经引入了spring-boot-web 依赖,但是依旧要在 Config Client 子模块的pom文件上加上 spring-boot-web 依赖

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

再次启动,服务启动成功

23-02-01 20:51:37.092  INFO 16848 --- [           main] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
2023-02-01 20:51:37.098  INFO 16848 --- [           main] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1675255897097 with initial instances count: 1
2023-02-01 20:51:37.100  INFO 16848 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application SPRING-CLOUD-CONFIG-CLIENT with eureka with status UP
2023-02-01 20:51:37.100  INFO 16848 --- [           main] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1675255897100, current=UP, previous=STARTING]
2023-02-01 20:51:37.103  INFO 16848 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SPRING-CLOUD-CONFIG-CLIENT/DESKTOP-A5PHDVG:spring-cloud-config-client:3355: registering service...
2023-02-01 20:51:37.138  INFO 16848 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 3355 (http) with context path ''
2023-02-01 20:51:37.139  INFO 16848 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 3355
2023-02-01 20:51:37.164  INFO 16848 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SPRING-CLOUD-CONFIG-CLIENT/DESKTOP-A5PHDVG:spring-cloud-config-client:3355 - registration status: 204
2023-02-01 20:51:38.241  INFO 16848 --- [           main] c.s.c.ConfigClientApplication            : Started ConfigClientApplication in 12.417 seconds (JVM running for 13.157)

Eureka注册中心

在这里插入图片描述

浏览器调用接口

在这里插入图片描述

那么,为什么Spring Cloud Config Client 的配置文件为什么要用 bootstrap.yml, 而不是 application ? 这里有一篇文章有说明SpringCloud Config - client连接server的设置写在application.yml, 导致属性无法解析

Bootstrap.yml (bootstrap.properties) 是在application.yml (application.properties)之前加载的。它通常用于“使用SpringCloud Config Server时,应在bootstrap.yml中指定spring.application.name和spring.cloud.config.server.git.uri”以及一些加密/解密信息。

Spring Cloud会创建一个Bootstrap Context(由bootstrap.yml加载),作为Spring应用的Application Context(由application.yml加载)的父上下文。初始化的时候,Bootstrap Context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的EnvironmentBootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。

例如,当使用SpringCloud Config时,通常从服务器加载“真正的”配置数据。为了获取URL(和其他连接配置,如密码等),您需要一个较早的或“bootstrap”配置。因此,您将配置服务器属性放在bootstrap.yml中,该属性用于加载实际配置数据(通常覆盖application.yml [如果存在]中的内容)。


补充:

在刚开始启动Spring Cloud Config Client 时,控制台提示:

Description:

No spring.config.import property has been defined

Action:

Add a spring.config.import=configserver: property to your configuration.
	If configuration is not required add spring.config.import=optional:configserver: instead.
	To disable this check, set spring.cloud.config.enabled=false or 
	spring.cloud.config.import-check.enabled=false.

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

Process finished with exit code 1

stackoverflow上有篇文章No spring.config.import property has been defined中给出解决办法,在pom文件中加上依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: SpringCloudConfig是一个用于管理应用配置的分布式配置中心,可以帮助你从多个环境中获取和管理配置。它可以从Git存储库,本地文件系统或远程配置服务器加载配置数据,并以服务端和客户端的形式将配置数据公开给应用程序。 ### 回答2: Spring Cloud Config 是一个分布式配置管理解决方案,可以将所有微服务的配置统一管理,并通过配置中心动态更新配置,实现了可持续集成和快速迭代的需求。Spring Cloud Config 提供了一个基于 Git 仓库的配置中心,可以将微服务的配置文件存储在中心仓库中,并通过访问该仓库获取配置信息。 Spring Cloud Config 的架构包括三个主要组件:Config ServerConfig Client 和 Git 仓库。Config Server配置中心的核心组件,是一个独立的微服务,负责管理配置信息并提供 REST 接口给其他微服务获取配置Config Client 是每个微服务中的库,用于获取从 Config Server 中获取配置信息,并将其应用到当前微服务中。Git 仓库用于存储配置文件,Config Server 可以从该仓库中读取配置文件,并将其暴露给 Config Client。 使用 Spring Cloud Config 可以带来以下优势: 1. 集中管理:所有微服务的配置信息都可以集中管理,方便维护和更新。 2. 动态更新:当配置发生变化时,无需重启微服务,Config Client 可以主动获取最新配置,并应用到当前微服务中。 3. 版本控制:通过将配置文件存储在 Git 仓库中,可以实现对配置文件的版本控制,方便查看历史修改记录以及回滚操作。 4. 高可用性:使用 Config Server 集群可以实现配置中心的高可用性,提供更好的服务可靠性。 总之,Spring Cloud Config 提供了一种灵活、可靠的配置管理解决方案,可以帮助开发团队更好地管理微服务的配置,提高开发效率和系统可靠性。 ### 回答3: Spring Cloud Config 是一个分布式配置管理工具,它提供了集中式的配置管理,能够方便地管理各种应用的配置信息。 Spring Cloud Config 分为服务端和客户端两个部分。服务端负责管理各个应用的配置信息,而客户端则将配置信息集成到对应的应用中。通过集中管理配置信息,可以实现应用的动态更新和灵活部署。 Spring Cloud Config 支持多种存储方式,包括本地文件系统、Git 仓库和远程配置中心等。可以根据需求选择不同的存储方式,并通过简单的配置即可集成到应用中。 使用 Spring Cloud Config,可以实现配置文件的版本控制、变更管理和安全控制。配置文件的版本控制可以方便地回滚到之前的配置信息,变更管理可以记录配置文件的修改历史,而安全控制可以保护配置文件的访问权限。 通过 Spring Cloud Config,可以将配置信息与应用代码分离,实现应用的解耦和可扩展性。配置信息的集中管理可以减少配置文件的冗余,提高配置的复用性。 另外,Spring Cloud Config 还提供了高可用的配置服务,可以通过集群部署来实现配置服务的负载均衡和容错能力。 总之,Spring Cloud Config 配置中心为分布式系统的配置管理提供了可靠和高效的解决方案,可以帮助开发者实现应用的动态配置和快速部署。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值