溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringCloud2020 bootstrap 配置文件失效如何解決

發(fā)布時(shí)間:2021-02-07 19:12:33 來(lái)源:億速云 閱讀:505 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)SpringCloud2020 bootstrap 配置文件失效如何解決,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

問(wèn)題

之前直接就可以用,而現(xiàn)在,啟動(dòng)的端口是8080,明顯沒(méi)有加載到bootstrap.properties文件,我以為我的文件名字寫錯(cuò)了,核對(duì)了幾次,確認(rèn)無(wú)誤,我猜想估計(jì)是bootstramp.properties配置文件沒(méi)有生效。

之前的版本:

  • spring boot 2.3.1.RELEASE

  • spring cloud Hoxton.SR4

當(dāng)前版本:

  • spring boot 2.4.2

  • spring cloud 2020.0.1

查找原因

根據(jù)上面出現(xiàn)的問(wèn)題,我使用百度搜索了下,大概的原因知道了:從Spring Boot 2.4版本開(kāi)始,配置文件加載方式進(jìn)行了重構(gòu)。

另外也有配置的默認(rèn)值變化,如下:

Spring Boot 2.3.8.RELEASE

package org.springframework.cloud.bootstrap;
public class BootstrapApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
 public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
  ConfigurableEnvironment environment = event.getEnvironment();
  if ((Boolean)environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class, true)) {

Spring Boot 2.4.2

package org.springframework.cloud.util;
public abstract class PropertyUtils {
 public static boolean bootstrapEnabled(Environment environment) {
  return (Boolean)environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class, false) || MARKER_CLASS_EXISTS;
 }

傳統(tǒng)解決方案

其實(shí)官網(wǎng)說(shuō)得很明白。看下面這段:

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").

兩個(gè)關(guān)鍵點(diǎn):

1、加一個(gè)依賴:spring-cloud-starter-bootstrap

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

2、加一個(gè)配置:spring.cloud.config.uri

bootstrap.properties

# 應(yīng)用名稱
spring.application.name=erwin-cloud-user
# 啟用環(huán)境
spring.profiles.active=dev

# 配置文件
spring.cloud.config.label=${spring.application.name}
spring.cloud.config.name=${spring.application.name}
spring.cloud.config.profile=${spring.profiles.active}
spring.cloud.config.uri=http://localhost:9000

解決方案

現(xiàn)在,你只需要這樣:

application.properties

# 應(yīng)用名稱
spring.application.name=erwin-cloud-user
# 啟用環(huán)境
spring.profiles.active=dev

spring.config.import=optional:configserver:http://localhost:9000

spring.cloud.config.label=${spring.application.name}
spring.cloud.config.name=${spring.application.name}
spring.cloud.config.profile=${spring.profiles.active}

上述就是小編為大家分享的SpringCloud2020 bootstrap 配置文件失效如何解決了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI