springcloud如何配置文件加載順序
看了網(wǎng)上很多文章,都是清一色的說(shuō),優(yōu)先加載bootstrap配置文件,然后加載application配置文件,bootstrap配置文件不能被覆蓋。
今天實(shí)際驗(yàn)證一下,配置文件真實(shí)的加載情況。
項(xiàng)目就是簡(jiǎn)單的springboot項(xiàng)目,加入springcloud相關(guān)依賴。
項(xiàng)目結(jié)構(gòu)
如下:

pom文件
如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud Alibaba 依賴 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
下面是各個(gè)配置文件
- application.properties
server.port=8081 hello1=hello1aaa hello= application.properties spring.application.name= test.application.properties
- application.yml
server:
port: 8083
hello2: hello2bbb
hello: application.yml
spring:
application:
name: test.application.yml
- bootstrap.properties
server.port = 8082 hello3=hello bp hello=bootstrap.properties spring.application.name=test.bootstrap.properties
- bootstrap.yml
server:
port: 8086
hello4: hello by
hello: bootstrap.yml
spring:
application:
name: test.bootstrap.yml
啟動(dòng)類(lèi)就不放了,直接看測(cè)試類(lèi)
@RestController
public class TestController {
@Value("${hello}")
private String hello;
@Value("${hello1}")
private String hello1;
@Value("${hello2}")
private String hello2;
@Value("${hello3}")
private String hello3;
@Value("${hello4}")
private String hello4;
@Value("${spring.application.name}")
private String applicationName;
@GetMapping("/testAll")
public String testAll(){
return hello1 + " === " + hello2
+ " === " + hello3 + " === " + hello4;
}
@GetMapping("/test")
public String test(){
return hello + " ====== " + applicationName;
}
}
按照網(wǎng)上大部分文章,啟動(dòng)的端口應(yīng)該是bootstrap配置當(dāng)中的,要么是8082,要么是8086. 下面看一下啟動(dòng)日志:

有人可能會(huì)懷疑,是不是bootstrap配置文件沒(méi)有加載,現(xiàn)在看一下testAll接口,能否獲取到每個(gè)配置文件中配置的變量:

說(shuō)明所有配置文件都是加載過(guò)的.
看一下test接口,可以看到,變量都是從application.properties文件中獲取的:

下面分析一下原因
spring官網(wǎng)的說(shuō)明

我開(kāi)始也是,光看到了這個(gè)描述忽略了前面的前提,那就是bootstrap context. 只有在bootstrap階段用到的屬性,在bootstrap配置中被加載了,才是優(yōu)先級(jí)最高。
繼續(xù)看spring官網(wǎng)的說(shuō)明:

非bootstrap階段的屬性,bootstrap配置優(yōu)先級(jí)最低。
簡(jiǎn)單的理解一下bootstrap階段,就是SpringConfig或者nacos用作配置中心的時(shí)候,讀取配置文件信息,就是bootstrap階段。
普通的springboot項(xiàng)目,讀取配置文件,是非bootstrap階段,因此bootstrap配置優(yōu)先級(jí)最低,優(yōu)先加載application配置文件。
下面再測(cè)試一下,將兩個(gè)application文件順序進(jìn)行調(diào)整,在resource目錄下,新建config目錄,將application.yml文件移到該文件夾中:

看一下日志:

可以看出來(lái),端口已經(jīng)換成yml文件中的配置。
看一下接口:

下面試一下bootstrap優(yōu)先級(jí)問(wèn)題,將bootstrap的一個(gè)配置移動(dòng)到config目錄下

修改application.yml配置:

同時(shí)修改application.properties配置文件,去掉hello參數(shù):

再次啟動(dòng)項(xiàng)目:


啟動(dòng)的接口用的是application.properties文件中的,雖然bootstrap.properties加載的順序可能靠前,但是優(yōu)先級(jí)比application配置文件要低。
下面來(lái)看接口調(diào)用接口:

可以看到,hello參數(shù)是bootstrap.properties文件中的配置,而applicationName參數(shù),是application.yml配置文件中的,證明了所有的配置文件都會(huì)加載,以及加載的優(yōu)先級(jí),確實(shí)在非bootstrap階段,bootstrap文件優(yōu)先級(jí)最低,其他的配置會(huì)按照application配置文件的加載順序,取優(yōu)先加載的配置文件中的配置。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ IDEA 創(chuàng)建 Java 項(xiàng)目及創(chuàng)建 Java 文件并運(yùn)行的詳細(xì)步驟
這篇文章主要介紹了IntelliJ IDEA 創(chuàng)建 Java 項(xiàng)目及創(chuàng)建 Java 文件并運(yùn)行的詳細(xì)步驟,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
SpringBoot整合Liquibase實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)管理和遷移
Liquibase是一個(gè)用于用于跟蹤、管理和應(yīng)用數(shù)據(jù)庫(kù)變化的開(kāi)源工具,通過(guò)日志文件(changelog)的形式記錄數(shù)據(jù)庫(kù)的變更(changeset),然后執(zhí)行日志文件中的修改,將數(shù)據(jù)庫(kù)更新或回滾(rollback)到一致的狀態(tài),本文主要介紹SpringBoot與Liquibase的集成,需要的朋友可以參考下2024-11-11
微服務(wù)通過(guò)Feign調(diào)用進(jìn)行密碼安全認(rèn)證操作
這篇文章主要介紹了微服務(wù)通過(guò)Feign調(diào)用進(jìn)行密碼安全認(rèn)證操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Java設(shè)計(jì)模式之共享模式/享元模式(Flyweight模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之共享模式/享元模式(Flyweight模式)介紹,本文講解了為什么使用共享模式/享元模式、如何使用共享模式/享元模式、Flyweight模式在XML等數(shù)據(jù)源中應(yīng)用等內(nèi)容,需要的朋友可以參考下2015-03-03
java程序運(yùn)行時(shí)內(nèi)存分配詳解
這篇文章主要介紹了java程序運(yùn)行時(shí)內(nèi)存分配詳解 ,需要的朋友可以參考下2016-07-07
Spring?Feign超時(shí)設(shè)置深入了解
Spring?Cloud中Feign客戶端是默認(rèn)開(kāi)啟支持Ribbon的,最重要的兩個(gè)超時(shí)就是連接超時(shí)ConnectTimeout和讀超時(shí)ReadTimeout,在默認(rèn)情況下,也就是沒(méi)有任何配置下,F(xiàn)eign的超時(shí)時(shí)間會(huì)被Ribbon覆蓋,兩個(gè)超時(shí)時(shí)間都是1秒2023-03-03
Jar包如何導(dǎo)入本地maven倉(cāng)庫(kù)
將本地jar包導(dǎo)入本地maven倉(cāng)庫(kù),可以通過(guò)maven命令-Dfile、-DgroupId、-DartifactId、-Dversion、-Dpackaging指定jar包的詳細(xì)信息,然后執(zhí)行命令即可2024-11-11

