Spring Cloud中配置客戶(hù)端示例詳解
Environment
Environment : PropertySources = 1:1
PropertySources : PropertySource = 1:N
[0] PropertySource (Map)
spring.application.name = spring-cloud-config-client
[1] PropertySource (Map)
spring.application.name = spring-cloud-config-client-demo
Spring Boot 配置文件
application.properties 或 application.xml
加載器: PropertiesPropertySourceLoader
application.yml 或者 application.yaml
加載器: YamlPropertySourceLoader
Environment 端點(diǎn)
請(qǐng)求 URI : /env
數(shù)據(jù)來(lái)源: EnvironmentEndpoint
Controller 來(lái)源: EnvironmentMvcEndpoint
Bootstrap 配置
參考 BootstrapApplicationListener 實(shí)現(xiàn)
注:程序啟動(dòng)參數(shù)的加載邏輯:
SpringApplication#configurePropertySources()
Bootstrap 配置文件
String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");當(dāng) spring.cloud.bootstrap.name 存在時(shí),使用該配置項(xiàng),否則,使用 "bootstrap" 作為默認(rèn)
## application.properties ## 通過(guò)調(diào)整 spring.cloud.bootstrap.enabled = false,嘗試關(guān)閉 bootstrap 上下文 ## 實(shí)際測(cè)試結(jié)果,沒(méi)有效果 spring.cloud.bootstrap.enabled = false
注意:
BootstrapApplicationListener加載實(shí)際早于ConfigFileApplicationListener原因是:
ConfigFileApplicationListener的 Order = Ordered.HIGHEST_PRECEDENCE + 10(第十一位)
BootstrapApplicationListener的 Order = Ordered.HIGHEST_PRECEDENCE + 5(第六位)
如果需要調(diào)整 控制 Bootstrap 上下文行為配置,需要更高優(yōu)先級(jí),也就是說(shuō) Order 需要 < Ordered.HIGHEST_PRECEDENCE + 5 (越小越優(yōu)先),比如使用程序啟動(dòng)參數(shù):
--spring.cloud.bootstrap.enabld = true
調(diào)整 Bootstrap 配置
調(diào)整 Bootstrap 配置文件名稱(chēng)
調(diào)整程序啟動(dòng)參數(shù)
--spring.cloud.bootstrap.name=spring-cloud
bootstrap 配置文件名稱(chēng)發(fā)生了改變"spring-cloud",現(xiàn)有三個(gè)文件:
application.properties
spring.application.name = spring-cloud-config-client
bootstrap.properties
spring.application.name = spring-cloud-config-client-demo
spring-cloud.properties
spring.application.name = spring-cloud
運(yùn)行結(jié)果(部分):
"applicationConfig: [classpath:/application.properties]": {
"spring.cloud.bootstrap.enabled": "false",
"endpoints.env.sensitive": "false",
"spring.application.name": "spring-cloud-config-client"
},
...
"applicationConfig: [classpath:/spring-cloud.properties]": {
"spring.application.name": "spring-cloud-config-client"
}
調(diào)整 Bootstrap 配置文件路徑
保留 Bootstrap 配置文件名稱(chēng) 程序啟動(dòng)參數(shù):
--spring.cloud.bootstrap.name=spring-cloud
調(diào)整 Bootstrap 配置文件路徑 程序啟動(dòng)參數(shù):
--spring.cloud.bootstrap.location=config
現(xiàn)有四個(gè)文件:
application.properties
spring.application.name = spring-cloud-config-client
bootstrap.properties
spring.application.name = spring-cloud-config-client-demo
spring-cloud.properties
spring.application.name = spring-cloud
config/spring-cloud.properties
spring.application.name = spring-cloud-2
實(shí)際結(jié)果:
"applicationConfig: [classpath:/application.properties]": {
"spring.cloud.bootstrap.enabled": "false",
"endpoints.env.sensitive": "false",
"spring.application.name": "spring-cloud-config-client"
},
...
"applicationConfig: [classpath:/config/spring-cloud.properties]": {
"spring.application.name": "spring-cloud-config-client"
},
"applicationConfig: [classpath:/spring-cloud.properties]": {
"spring.application.name": "spring-cloud-config-client"
},
覆蓋遠(yuǎn)程配置屬性
默認(rèn)情況,Spring Cloud 是允許覆蓋的, spring.cloud.config.allowOverride=true
通過(guò)程序啟動(dòng)參數(shù),調(diào)整這個(gè)值為"false"
--spring.cloud.config.allowOverride=false
啟動(dòng)后,重新Postman 發(fā)送 POST 請(qǐng)求,調(diào)整 spring.application.name 值為 "spring-cloud-new"
注意官方文檔的說(shuō)明:the remote property source has to grant it permission by setting
spring.cloud.config.allowOverride=true(it doesn’t work to set this locally).
自定義 Bootstrap 配置
- 創(chuàng)建
META-INF/spring.factories文件(類(lèi)似于 Spring Boot 自定義 Starter) - 自定義 Bootstrap 配置 Configuration
?import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
?
import java.util.HashMap;
import java.util.Map;
?
/**
* Bootstrap 配置 Bean
*
*
* @since Configuration
*/
@Configuration
public class MyConfiguration implements ApplicationContextInitializer {
?
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
// 從 ConfigurableApplicationContext 獲取 ConfigurableEnvironment 實(shí)例
ConfigurableEnvironment environment = applicationContext.getEnvironment();
// 獲取 PropertySources
MutablePropertySources propertySources = environment.getPropertySources();
// 定義一個(gè)新的 PropertySource,并且放置在首位
propertySources.addFirst(createPropertySource());
?
}
?
private PropertySource createPropertySource() {
?
Map<String, Object> source = new HashMap<>();
?
source.put("name", "小馬哥");
?
PropertySource propertySource = new MapPropertySource("my-property-source", source);
?
return propertySource;
?
}
}配置 META-INF/spring.factories 文件,關(guān)聯(lián)Key org.springframework.cloud.bootstrap.BootstrapConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration= \ com.segmentfault.springcloudlesson2.boostrap.MyConfiguration
自定義 Bootstrap 配置屬性源
實(shí)現(xiàn) PropertySourceLocator
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.env.*;
?
import java.util.HashMap;
import java.util.Map;
?
/**
* 自定義 {@link PropertySourceLocator} 實(shí)現(xiàn)
*
* @since PropertySourceLocator
*/
public class MyPropertySourceLocator implements PropertySourceLocator {
?
@Override
public PropertySource<?> locate(Environment environment) {
?
if (environment instanceof ConfigurableEnvironment) {
?
ConfigurableEnvironment configurableEnvironment = ConfigurableEnvironment.class.cast(environment);
?
// 獲取 PropertySources
MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
// 定義一個(gè)新的 PropertySource,并且放置在首位
propertySources.addFirst(createPropertySource());
?
}
return null;
}
?
private PropertySource createPropertySource() {
?
Map<String, Object> source = new HashMap<>();
?
source.put("spring.application.name", "小馬哥的 Spring Cloud 程序");
// 設(shè)置名稱(chēng)和來(lái)源
PropertySource propertySource = new MapPropertySource("over-bootstrap-property-source", source);
?
return propertySource;
?
}
}配置 META-INF/spring.factories
org.springframework.cloud.bootstrap.BootstrapConfiguration= \ com.segmentfault.springcloudlesson2.boostrap.MyConfiguration,\ com.segmentfault.springcloudlesson2.boostrap.MyPropertySourceLocator
到此這篇關(guān)于Spring Cloud 之配置客戶(hù)端的文章就介紹到這了,更多相關(guān)Spring Cloud 配置客戶(hù)端內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring通過(guò)jdbc連接數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了spring通過(guò)jdbc連接數(shù)據(jù)庫(kù)的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
解決SpringBoot的@DeleteMapping注解的方法不被調(diào)用問(wèn)題
這篇文章主要介紹了解決SpringBoot的@DeleteMapping注解的方法不被調(diào)用問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Java編寫(xiě)日志手機(jī)號(hào)脫敏工具類(lèi)
在開(kāi)發(fā)過(guò)程中,很容易將用戶(hù)敏感信息,例如手機(jī)號(hào)碼、身份證等,打印在日志平臺(tái),本文將利用Java編寫(xiě)一個(gè)日志手機(jī)號(hào)脫敏工具類(lèi),感興趣的可以了解下2024-12-12
Java生成動(dòng)態(tài)版驗(yàn)證碼的方法實(shí)例
這篇文章主要給大家介紹了利用Java生成動(dòng)態(tài)版驗(yàn)證碼的方法實(shí)例,本文生成的是GIF格式 + 干擾元素,讓驗(yàn)證碼破解難度又上了一個(gè)層次,文中給出了詳細(xì)的示例代碼,并在文末給出了完整的實(shí)例代碼供大家下載學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。2017-04-04
SpringBoot使用ip2region獲取地理位置信息的方法
這篇文章主要介紹了SpringBoot使用ip2region獲取地理位置信息的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
基于Java的度分秒坐標(biāo)轉(zhuǎn)純經(jīng)緯度坐標(biāo)的漂亮國(guó)基地信息管理的方法
本文以java語(yǔ)言為例,詳細(xì)介紹如何管理漂亮國(guó)的基地信息,為下一步全球的空間可視化打下堅(jiān)實(shí)的基礎(chǔ),首先介紹如何對(duì)數(shù)據(jù)進(jìn)行去重處理,然后介紹在java當(dāng)中如何進(jìn)行度分秒位置的轉(zhuǎn)換,最后結(jié)合實(shí)現(xiàn)原型進(jìn)行詳細(xì)的說(shuō)明,感興趣的朋友跟隨小編一起看看吧2024-06-06
Springboot POI導(dǎo)出Excel(瀏覽器)
這篇文章主要為大家詳細(xì)介紹了Springboot POI導(dǎo)出Excel,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Java將對(duì)象寫(xiě)入文件讀出_序列化與反序列化的實(shí)例
下面小編就為大家?guī)?lái)一篇Java將對(duì)象寫(xiě)入文件讀出_序列化與反序列化的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08

