覆蓋遠程配置屬性
默認情況,Spring Cloud是允許覆蓋的,spring.cloud.config.allowOverride=true
通過程序啓動參數,調整這個值爲"false"
--spring.cloud.config.allowOverride=true
啓動後,重新Postman發送POST請求,調整spring.application.name值爲"spring-cloud-new"
自定義Bootstrap配置
- 創建
META-INF/spring.factories
文件{類似於Spring boot 自定義Starter} - 自定義Bootstrap配置Configurtion
package com.segmentfault.springcloudlesson1.bootstrap;
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
*/
@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", "shuai");
PropertySource propertySource = new MapPropertySource("my-property-source", source);
return propertySource;
}
}
- 配置
MEAT-INF/spring.factories
文件,關聯Key
org.springframework.cloud.bootstrapConfiguration= \
com.segmentfault.springcloudlesson2.bootstrap.Myconfiguration
自定義Bootstrap配置屬性源
- 實現PropertySourceLocator
package com.segmentfault.springcloudlesson1.bootstrap;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.env.*;
import java.util.HashMap;
import java.util.Map;
/**
* 自定義{@link 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", "shuaishuai");
//設置名稱和來源
PropertySource propertySource = new MapPropertySource("over-bootstrap", source);
return propertySource;
}
}
這是個錯誤步驟,目的是瞭解此方式
- MyPropertySourceLocator暴露成 Spring Bean
@Bean
public MyPropertySourceLocator myPropertySourceLocator() {
return new MyPropertySourceLocator();
}
2配置META-INF/spring.factories
org.springframework.cloud.bootstrapConfiguration= \
com.segmentfault.springcloudlesson1.bootstrap.MyConfiguration,\
com.segmentfault.springcloudlesson1.bootstrap.MyPropertySourceLocator