Commit d9a857a4 authored by hanbing's avatar hanbing

Flink 任务环境变量占位符解析

parent 02a1ac3b
...@@ -4,8 +4,11 @@ import org.apache.commons.lang3.StringUtils; ...@@ -4,8 +4,11 @@ import org.apache.commons.lang3.StringUtils;
import java.io.*; import java.io.*;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.UUID; import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PropertiesHelper implements Serializable { public class PropertiesHelper implements Serializable {
private Properties properties; private Properties properties;
...@@ -33,6 +36,8 @@ public class PropertiesHelper implements Serializable { ...@@ -33,6 +36,8 @@ public class PropertiesHelper implements Serializable {
InputStream inputStream = PropertiesHelper.class.getResourceAsStream("/config.properties"); InputStream inputStream = PropertiesHelper.class.getResourceAsStream("/config.properties");
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, Charset.defaultCharset())); BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, Charset.defaultCharset()));
properties.load(bf); properties.load(bf);
// 环境变量解析
resolvePlaceholders();
bf.close(); bf.close();
inputStream.close(); inputStream.close();
...@@ -48,6 +53,27 @@ public class PropertiesHelper implements Serializable { ...@@ -48,6 +53,27 @@ public class PropertiesHelper implements Serializable {
} }
return this.properties; return this.properties;
} }
private void resolvePlaceholders() {
Pattern pattern = Pattern.compile("\\$\\{([^:}]+):([^}]+)\\}");
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String key = (String) entry.getKey();
String value = (String) entry.getValue();
Matcher matcher = pattern.matcher(value);
StringBuffer resolvedValue = new StringBuffer();
while (matcher.find()) {
String envKey = matcher.group(1);
String defaultValue = matcher.group(2);
String envValue = System.getProperty(envKey, System.getenv(envKey));
if (envValue == null) {
envValue = defaultValue;
}
matcher.appendReplacement(resolvedValue, envValue);
}
matcher.appendTail(resolvedValue);
properties.setProperty(key, resolvedValue.toString());
}
}
public synchronized Properties getConsumerProperties() { public synchronized Properties getConsumerProperties() {
Properties consumerProps = new Properties(); Properties consumerProps = new Properties();
......
config_name=config_dev.properties config_name=config_${my.profile:dev}.properties
\ No newline at end of file \ No newline at end of file
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
<!-- 加载配置文件 --> <!-- 加载配置文件 -->
<!--<context:property-placeholder location="classpath:db.properties"/>--> <!--<context:property-placeholder location="classpath:db.properties"/>-->
<context:property-placeholder location="classpath:db/dev/db.properties"/> <context:property-placeholder location="classpath:db/${my.profile:dev}/db.properties"/>
<!-- 配置数据源 --> <!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment