Spring Cloud中配置客戶端示例詳解
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 端點
請求 URI : /env
數(shù)據(jù)來源: EnvironmentEndpoint
Controller 來源: EnvironmentMvcEndpoint
Bootstrap 配置
參考 BootstrapApplicationListener 實現(xiàn)
注:程序啟動參數(shù)的加載邏輯:
SpringApplication#configurePropertySources()
Bootstrap 配置文件
String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");當(dāng) spring.cloud.bootstrap.name 存在時,使用該配置項,否則,使用 "bootstrap" 作為默認
## application.properties ## 通過調(diào)整 spring.cloud.bootstrap.enabled = false,嘗試關(guān)閉 bootstrap 上下文 ## 實際測試結(jié)果,沒有效果 spring.cloud.bootstrap.enabled = false
注意:
BootstrapApplicationListener加載實際早于ConfigFileApplicationListener原因是:
ConfigFileApplicationListener的 Order = Ordered.HIGHEST_PRECEDENCE + 10(第十一位)
BootstrapApplicationListener的 Order = Ordered.HIGHEST_PRECEDENCE + 5(第六位)
如果需要調(diào)整 控制 Bootstrap 上下文行為配置,需要更高優(yōu)先級,也就是說 Order 需要 < Ordered.HIGHEST_PRECEDENCE + 5 (越小越優(yōu)先),比如使用程序啟動參數(shù):
--spring.cloud.bootstrap.enabld = true
調(diào)整 Bootstrap 配置
調(diào)整 Bootstrap 配置文件名稱
調(diào)整程序啟動參數(shù)
--spring.cloud.bootstrap.name=spring-cloud
bootstrap 配置文件名稱發(fā)生了改變"spring-cloud",現(xiàn)有三個文件:
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
運行結(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 配置文件名稱 程序啟動參數(shù):
--spring.cloud.bootstrap.name=spring-cloud
調(diào)整 Bootstrap 配置文件路徑 程序啟動參數(shù):
--spring.cloud.bootstrap.location=config
現(xiàn)有四個文件:
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
實際結(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"
},
覆蓋遠程配置屬性
默認情況,Spring Cloud 是允許覆蓋的, spring.cloud.config.allowOverride=true
通過程序啟動參數(shù),調(diào)整這個值為"false"
--spring.cloud.config.allowOverride=false
啟動后,重新Postman 發(fā)送 POST 請求,調(diào)整 spring.application.name 值為 "spring-cloud-new"
注意官方文檔的說明: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文件(類似于 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 實例
ConfigurableEnvironment environment = applicationContext.getEnvironment();
// 獲取 PropertySources
MutablePropertySources propertySources = environment.getPropertySources();
// 定義一個新的 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 配置屬性源
實現(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} 實現(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();
// 定義一個新的 PropertySource,并且放置在首位
propertySources.addFirst(createPropertySource());
?
}
return null;
}
?
private PropertySource createPropertySource() {
?
Map<String, Object> source = new HashMap<>();
?
source.put("spring.application.name", "小馬哥的 Spring Cloud 程序");
// 設(shè)置名稱和來源
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 之配置客戶端的文章就介紹到這了,更多相關(guān)Spring Cloud 配置客戶端內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決SpringBoot的@DeleteMapping注解的方法不被調(diào)用問題
這篇文章主要介紹了解決SpringBoot的@DeleteMapping注解的方法不被調(diào)用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
SpringBoot使用ip2region獲取地理位置信息的方法
這篇文章主要介紹了SpringBoot使用ip2region獲取地理位置信息的相關(guān)知識,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
基于Java的度分秒坐標(biāo)轉(zhuǎn)純經(jīng)緯度坐標(biāo)的漂亮國基地信息管理的方法
本文以java語言為例,詳細介紹如何管理漂亮國的基地信息,為下一步全球的空間可視化打下堅實的基礎(chǔ),首先介紹如何對數(shù)據(jù)進行去重處理,然后介紹在java當(dāng)中如何進行度分秒位置的轉(zhuǎn)換,最后結(jié)合實現(xiàn)原型進行詳細的說明,感興趣的朋友跟隨小編一起看看吧2024-06-06
Springboot POI導(dǎo)出Excel(瀏覽器)
這篇文章主要為大家詳細介紹了Springboot POI導(dǎo)出Excel,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05

