提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.env.config; |
H |
2 |
|
|
3 |
import cn.hutool.core.util.StrUtil; |
|
4 |
import com.iailab.framework.common.util.collection.SetUtils; |
|
5 |
import com.iailab.framework.env.core.util.EnvUtils; |
|
6 |
import org.springframework.boot.SpringApplication; |
|
7 |
import org.springframework.boot.env.EnvironmentPostProcessor; |
|
8 |
import org.springframework.core.env.ConfigurableEnvironment; |
|
9 |
|
|
10 |
import java.util.Set; |
|
11 |
|
|
12 |
import static com.iailab.framework.env.core.util.EnvUtils.HOST_NAME_VALUE; |
|
13 |
|
|
14 |
/** |
|
15 |
* 多环境的 {@link EnvEnvironmentPostProcessor} 实现类 |
|
16 |
* 将 iailab.env.tag 设置到 nacos 等组件对应的 tag 配置项,当且仅当它们不存在时 |
|
17 |
* |
|
18 |
* @author iailab |
|
19 |
*/ |
|
20 |
public class EnvEnvironmentPostProcessor implements EnvironmentPostProcessor { |
|
21 |
|
|
22 |
private static final Set<String> TARGET_TAG_KEYS = SetUtils.asSet( |
|
23 |
"spring.cloud.nacos.discovery.metadata.tag" // Nacos 注册中心 |
|
24 |
// MQ TODO |
|
25 |
); |
|
26 |
|
|
27 |
@Override |
|
28 |
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { |
|
29 |
// 0. 设置 ${HOST_NAME} 兜底的环境变量 |
|
30 |
String hostNameKey = StrUtil.subBetween(HOST_NAME_VALUE, "{", "}"); |
|
31 |
if (!environment.containsProperty(hostNameKey)) { |
|
32 |
environment.getSystemProperties().put(hostNameKey, EnvUtils.getHostName()); |
|
33 |
} |
|
34 |
|
|
35 |
// 1.1 如果没有 iailab.env.tag 配置项,则不进行配置项的修改 |
|
36 |
String tag = EnvUtils.getTag(environment); |
|
37 |
if (StrUtil.isEmpty(tag)) { |
|
38 |
return; |
|
39 |
} |
|
40 |
// 1.2 需要修改的配置项 |
|
41 |
for (String targetTagKey : TARGET_TAG_KEYS) { |
|
42 |
String targetTagValue = environment.getProperty(targetTagKey); |
|
43 |
if (StrUtil.isNotEmpty(targetTagValue)) { |
|
44 |
continue; |
|
45 |
} |
|
46 |
environment.getSystemProperties().put(targetTagKey, tag); |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
} |