服务器文件配置失效,SpringCloud2020 bootstrap 配置文件失效的解决方法

Spring Cloud 2020版本 bootstrap 配置文件(properties 或者 yml)无效

如何解决?

背景介绍

微服务是基于Spring Cloud框架搭建的,Spring Cloud Config作为服务配置中心。

业务服务只配置服务名称、启用环境和config的URL地址,其他都配置在配置中心,例如服务端口、服务注册中心地址等。可在开发环境(dev)、测试环境(test)和生产环境(prod)分别配置。

所以预想的启动流程是:先加载配置文件,再启动服务。

之前的做法是,将配置文件名称改为:bootstrap.properties。

问题

之前直接就可以用,而现在,启动的端口是8080,明显没有加载到bootstrap.properties文件,我以为我的文件名字写错了,核对了几次,确认无误,我猜想估计是bootstramp.properties配置文件没有生效。

之前的版本:

spring boot 2.3.1.RELEASE

spring cloud Hoxton.SR4

当前版本:

spring boot 2.4.2

spring cloud 2020.0.1

查找原因

根据上面出现的问题,我使用百度搜索了下,大概的原因知道了:从Spring Boot 2.4版本开始,配置文件加载方式进行了重构。

另外也有配置的默认值变化,如下:

Spring Boot 2.3.8.RELEASE

?

Spring Boot 2.4.2

?

传统解决方案

其实官网说得很明白。看下面这段:

Config First Bootstrap

To use the legacy bootstrap way of connecting to Config Server, bootstrap must be enabled via a property or the spring-cloud-starter-bootstrap starter. The property is spring.cloud.bootstrap.enabled=true. It must be set as a System Property or environment variable. Once bootstrap has been enabled any application with Spring Cloud Config Client on the classpath will connect to Config Server as follows: When a config client starts, it binds to the Config Server (through the spring.cloud.config.uri bootstrap configuration property) and initializes Spring Environment with remote property sources.

The net result of this behavior is that all client applications that want to consume the Config Server need a bootstrap.yml (or an environment variable) with the server address set in spring.cloud.config.uri (it defaults to "http://localhost:8888").

两个关键点:

1、加一个依赖:spring-cloud-starter-bootstrap

?

2、加一个配置:spring.cloud.config.uri

?

解决方案

现在,你只需要这样:

?

到此这篇关于SpringCloud2020 bootstrap 配置文件失效的解决方法的文章就介绍到这了,更多相关SpringCloud2020 bootstrap 配置内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://segmentfault.com/a/1190000039185130

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值