1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

sync & commit in greenwich

This commit is contained in:
fangjian0423
2019-10-29 14:57:54 +08:00
parent 12b9091583
commit 980bc3fd7a
94 changed files with 2633 additions and 780 deletions

View File

@@ -15,60 +15,65 @@
*/
package com.alibaba.cloud.sentinel.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.*;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
/**
* @author zhuhonghan
*/
public class GatewayEnvironmentPostProcessor implements EnvironmentPostProcessor {
private final static String SENTINEL_FILTER_ENABLED = "spring.cloud.sentinel.filter.enabled";
private final static String PROPERTY_SOURCE_NAME = "defaultProperties";
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication springApplication) {
addDefaultPropertySource(environment);
}
private final static String SENTINEL_FILTER_ENABLED = "spring.cloud.sentinel.filter.enabled";
private void addDefaultPropertySource(ConfigurableEnvironment environment) {
private final static String PROPERTY_SOURCE_NAME = "defaultProperties";
Map<String, Object> map = new HashMap<String, Object>();
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication springApplication) {
addDefaultPropertySource(environment);
}
configureDefaultProperties(map);
private void addDefaultPropertySource(ConfigurableEnvironment environment) {
addOrReplace(environment.getPropertySources(), map);
}
Map<String, Object> map = new HashMap<String, Object>();
private void configureDefaultProperties(Map<String, Object> source) {
// Required Properties
source.put(SENTINEL_FILTER_ENABLED, "false");
}
configureDefaultProperties(map);
private void addOrReplace(MutablePropertySources propertySources,
Map<String, Object> map) {
MapPropertySource target = null;
if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME);
if (source instanceof MapPropertySource) {
target = (MapPropertySource) source;
for (String key : map.keySet()) {
if (!target.containsProperty(key)) {
target.getSource().put(key, map.get(key));
}
}
}
}
if (target == null) {
target = new MapPropertySource(PROPERTY_SOURCE_NAME, map);
}
if (!propertySources.contains(PROPERTY_SOURCE_NAME)) {
propertySources.addLast(target);
}
}
addOrReplace(environment.getPropertySources(), map);
}
private void configureDefaultProperties(Map<String, Object> source) {
// Required Properties
source.put(SENTINEL_FILTER_ENABLED, "false");
}
private void addOrReplace(MutablePropertySources propertySources,
Map<String, Object> map) {
MapPropertySource target = null;
if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME);
if (source instanceof MapPropertySource) {
target = (MapPropertySource) source;
for (String key : map.keySet()) {
if (!target.containsProperty(key)) {
target.getSource().put(key, map.get(key));
}
}
}
}
if (target == null) {
target = new MapPropertySource(PROPERTY_SOURCE_NAME, map);
}
if (!propertySources.contains(PROPERTY_SOURCE_NAME)) {
propertySources.addLast(target);
}
}
}