欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCloud2020 bootstrap 配置文件失效的解決方法

 更新時(shí)間:2021年02月07日 08:56:24   作者:馮文議  
這篇文章主要介紹了SpringCloud2020 bootstrap 配置文件失效的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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

如何解決?

背景介紹

微服務(wù)是基于Spring Cloud框架搭建的,Spring Cloud Config作為服務(wù)配置中心。

業(yè)務(wù)服務(wù)只配置服務(wù)名稱、啟用環(huán)境和config的URL地址,其他都配置在配置中心,例如服務(wù)端口、服務(wù)注冊(cè)中心地址等??稍陂_(kāi)發(fā)環(huán)境(dev)、測(cè)試環(huán)境(test)和生產(chǎn)環(huán)境(prod)分別配置。

所以預(yù)想的啟動(dòng)流程是:先加載配置文件,再啟動(dòng)服務(wù)。

之前的做法是,將配置文件名稱改為:bootstrap.properties。

問(wèn)題

之前直接就可以用,而現(xiàn)在,啟動(dòng)的端口是8080,明顯沒(méi)有加載到bootstrap.properties文件,我以為我的文件名字寫(xiě)錯(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}

到此這篇關(guān)于SpringCloud2020 bootstrap 配置文件失效的解決方法的文章就介紹到這了,更多相關(guān)SpringCloud2020 bootstrap 配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JSON 格式的弊端與解決方法(真實(shí)示例)

    JSON 格式的弊端與解決方法(真實(shí)示例)

    JSON 格式是目前最流行的數(shù)據(jù)交互格式,廣泛應(yīng)用于前后端分離的系統(tǒng)。但也有一些場(chǎng)合不適合使用 JSON 格式,這篇文章主要介紹了JSON 格式的弊端與解決方法,需要的朋友可以參考下
    2022-09-09
  • Spring Boot集成Shiro實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限的完整步驟

    Spring Boot集成Shiro實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限的完整步驟

    這篇文章主要給大家介紹了關(guān)于Spring Boot集成Shiro實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • IntelliJ IDEA之配置JDK的4種方式(小結(jié))

    IntelliJ IDEA之配置JDK的4種方式(小結(jié))

    這篇文章主要介紹了IntelliJ IDEA之配置JDK的4種方式(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Java同步鎖synchronized用法的最全總結(jié)

    Java同步鎖synchronized用法的最全總結(jié)

    這篇文章主要介紹了Java同步鎖synchronized用法的最全總結(jié),需要的朋友可以參考下,文章詳細(xì)講解了Java同步鎖Synchronized的使用方法和需要注意的點(diǎn),希望對(duì)你有所幫助
    2023-03-03
  • 詳解Java的Hibernate框架中的緩存與原生SQL語(yǔ)句的使用

    詳解Java的Hibernate框架中的緩存與原生SQL語(yǔ)句的使用

    這篇文章主要介紹了Java的Hibernate框架中的緩存與原生SQL語(yǔ)句的使用,Hibernate是Java的SSH三大web開(kāi)發(fā)框架之一,需要的朋友可以參考下
    2015-12-12
  • Java實(shí)現(xiàn)字符串和輸入流的相互轉(zhuǎn)換

    Java實(shí)現(xiàn)字符串和輸入流的相互轉(zhuǎn)換

    這篇文章主要介紹了Java實(shí)現(xiàn)字符串和輸入流的相互轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 解決無(wú)法解析javax.servlet的方法

    解決無(wú)法解析javax.servlet的方法

    最近在創(chuàng)建一個(gè)servlet時(shí),自動(dòng)生成的代碼中出現(xiàn)servlet無(wú)法解析的提示,令我無(wú)法正常使用servlet里的方法,在對(duì)各個(gè)步驟進(jìn)行查看后,發(fā)現(xiàn)了問(wèn)題所在,需要的朋友可以參考下
    2021-05-05
  • 最新評(píng)論