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

news2024/10/6 16:28:14

自己部署了一个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>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/194630.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

极客星球 | Elasticsearch入门与实战技术分享

为持续夯实MobTech袤博科技的数智技术创新能力和技术布道能力&#xff0c;本期极客星球邀请了MobTech企业服务研发部工程师勤佳&#xff0c;从Elasticsearch集群安装、DSL语句讲解、深度分页、IK分词器、滚动索引等方面进行了阐述和分享。 一、集群环境安装 elasticsearch 是…

节后转岗“浪潮”来了!瞅准“趋势向上”的行业!

“跨行跨岗人员&#xff0c;怎么能顺利转行&#xff1f;”的话题一直存在&#xff01;成功人士给出一条转岗转行的原则&#xff1a;迁移到技能相近但趋势向上的岗位。那么&#xff0c;什么叫“技能相近”和“趋势向上”呢&#xff1f;让我们来看看。趋势向上除了技能相近&#…

现阶段元宇宙经常偏离原有的发展轨道,使其失去该有的功能和意义

人们总是会自然地陷入到约定俗成的俗套之中。对于元宇宙&#xff0c;同样未能免俗。即使是那些处于头部的玩家&#xff0c;亦不例外。比如&#xff0c;扎克伯格就仅仅只是将元宇宙看成了一个将脸书带离泥潭的工具&#xff0c;一味地迎合资本&#xff0c;而最终忽略了元宇宙最本…

日常避坑--input输入框type=number仍可以输入“e“,“.“等符号

问题发现在使用ElementUI的input框时候&#xff0c;我们有时候需要只让用户输入数字类型。这个时候你可能就会想到<input type"number">,思路没错&#xff0c;但是踩着坑啦。我定义了一个number类型的input框但是&#xff0c;输入框仍旧可以输入"e",…

hadoop安装(二、hadoop)(备忘)

hadoop安装hadoop更改文件配置配置core-site.xml配置hadoop-env.sh配置hdfs-site.xml配置mapred-site.xml配置yarn-site.xml配置环境hadoop安装安装hadoop 紧接上文&#xff0c;解压过的hadoop內部文件为 再进入etc内部的hadoop 修改hadoop313的权限 在/opt目录下&#xff0…

分享24个网页游戏源代码,总有一个是你想要的

分享24个网页游戏源代码 24个游戏源代码下载链接&#xff1a;https://pan.baidu.com/s/1gYJlj8enJbh5mFS_wMaZBA?pwd4ncb 提取码&#xff1a;4ncb 下面是项目的名字&#xff0c;我放了一些图片&#xff0c;大家下载后可以看到。 Html5JS网页版捕鱼达人游戏 HTML5水果忍者游戏…

杂谈---名言警句记录

我们总喜欢拿顺其自然来敷衍人生道路上的荆棘与坎坷,却很少承认真正的顺其自然其实是竭尽所能之后的不强求,而并非两手一摊的不作为.没有那一次巨大的历史灾难,不是以历史的进步作为补偿.今日长缨在手,何时缚住苍龙?男人遇到真爱时第一反应是胆怯&#xff0c;女人遇到真爱的第…

这份2023软件测试面试技巧,助你拿下满意offer

求职&#xff0c;就像打仗&#xff0c;不仅是一场挑战自己的战斗&#xff0c;也是一场与用人单位的较量。而求职者只有准备足够充分&#xff0c;才能在这场毫无硝烟的“战场”上取得胜利。那么软件测试面试需要做哪些准备以及软件测试面试需要哪些技巧呢&#xff1f;1、熟悉岗位…

JDBC数据库连接池

目录JDBC原理图1. 数据库连接的5种方式2.ResultSet底层3.Statement sql注入3.1 PreparedStatement4. Jdbc工具类4.1 工具类应用5.Jdbc事务6.批处理6.1 批处理原理7.数据库连接池7.1 c3p07.2 德鲁伊7.2.1 德鲁伊工具类8.Apache-DBUtils工具类8.1多条查询&#xff0c;BeanListHan…

Web3中文|火遍全网的去中心化推特「Damus」是什么?(附操作指南)

Damus是一个建立在去中心化网络上的社交软件&#xff0c;被称为“推特杀手”&#xff0c;现已在苹果应用商店上线。 1月31日&#xff0c;Damus团队在推特上证实了这一消息&#xff0c;此前该团队称已经被苹果公司拒绝了至少三次。 不久之后&#xff0c;Twitter联合创始人Jack…

Anaconda3安装

Anaconda3安装 step1 下载Anaconda 网址&#xff1a;https://www.anaconda.com/products/individual 点击红色方框中的Download下载最新版本的Anaconda软件&#xff0c;可选择windows/Linux/Mac系统。 Step2 安装过程 双击.exe文件 2.选择Next>,进行下一步。 3.点击 …

论文投稿指南——中文核心期刊推荐(水产、渔业)

【前言】 &#x1f680; 想发论文怎么办&#xff1f;手把手教你论文如何投稿&#xff01;那么&#xff0c;首先要搞懂投稿目标——论文期刊 &#x1f384; 在期刊论文的分布中&#xff0c;存在一种普遍现象&#xff1a;即对于某一特定的学科或专业来说&#xff0c;少数期刊所含…

蓝海彤翔董事长鲁永泉荣获太湖科学城功能片区2022年度表彰

新春伊始、喜讯传来。昨天&#xff08;1月31日&#xff09;&#xff0c;太湖科学城功能片区推进“敢为、敢闯、敢干、敢首创”动员会在太湖光子科技园举行。会上&#xff0c;78家企业和30名优秀企业家及个人获得表彰&#xff0c;蓝海彤翔董事长、蓝海创意云创始人鲁永泉荣获太湖…

Git最佳实践-Git flow

Git分支管理背景 Git是当下最流行的版本管理系统&#xff0c;阮一峰在自己的博文中提到过&#xff1a;“如果你严肃对待编程&#xff0c;就必定会使用版本管理工具”。Git操作是基于分支的&#xff0c;当下环境衍生出多种优秀的分支管理策略&#xff0c;其目的就是要保证不同分…

【深度学习】docker中安装ssh服务,并使用vscode连接操作其文件夹

文章目录前言1. docker容器安装ssh服务1.1. 安装docker的ssh1.2.在容器终端下依次执行如下命令&#xff1a;1.3.回到宿主机终端后&#xff0c;依次执行如下命令&#xff1a;1.4. 使用 ssh 客户端工具&#xff08;如 MobaXterm&#xff09;连接容器2、vscode连入docker总结前言 …

scrapy-1

1.scrapy Scrapy是一个为了爬取网站数据&#xff0c;提取结构性数据而编写的应用框架。 可以应用在包括数据挖掘&#xff0c;信息处理 或存储历史数据等一系列的程序中。 2.scrapy项目的创建以及运行 1.创建scrapy项目&#xff1a;终端输入 scrapy startproject 项目名称 2.…

OpenMMLab 计算机视觉 # day1: 计算机视觉基础与OpenMMLab开源算法体系

相关资源: github 第一课 计算机视觉与 OpenMMLab 开源算法体系 张子豪 计算机视觉基础 计算机视觉&#xff1a;让计算机理解图像、视频。 计算机视觉的三大基础任务&#xff1a;图像分类(图像识别)、目标检测、图像分割任务。 根据目标数量&#xff0c;计算机视觉任务也分…

Bahdanau 注意力

在预测词元时&#xff0c;如果不是所有输入词元都是相关的&#xff0c;那么具有Bahdanau注意力的循环神经网络编码器-解码器会有选择地统计输入序列的不同部分。这是通过将上下文变量视为加性注意力池化的输出来实现的。 在循环神经网络编码器-解码器中&#xff0c;Bahdanau注…

Centos8中安装配置DVWA靶场环境详细流程

一、准备内容本文在Centos8中安装配置DVWA靶场&#xff0c;该靶场启动需具备【LinuxApacheMysqlPhp】四大环境&#xff1a;所以在后续的安装配置DVWA靶场时遇到问题首先需要排查Apache、Mysql、Php这三大环境是否正常启动&#xff08;若不能正常启动则需逐一排查解决&#xff0…

漏洞深度分析|CVE-2023-24162 hutool XML反序列化漏洞

项目介绍 Hutool是一个小而全的Java工具类库&#xff0c;通过静态方法封装&#xff0c;降低相关API的学习成本&#xff0c;提高工作效率&#xff0c;使Java拥有函数式语言般的优雅&#xff0c;让Java语言也可以“甜甜的”。 Hutool中的工具方法来自每个用户的精雕细琢&#x…