独立配置文件定义及使用
构建大前提配置,build参数中把resources相关的构建带上
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
1.方式1
配置文件定义
单独抽取独立的配置文件,而非写到springboot的全局配置文件application.properties中,看如何使用的
配置文件resources/scheduler.properties
# master execute thread num
#master.exec.threads=100
# master execute task number in parallel
#master.exec.task.num=20
# master dispatch task number
#master.dispatch.task.num = 3
# master heartbeat interval
#master.heartbeat.interval=10
# master commit task retry times
#master.task.commit.retryTimes=5
# master commit task interval
#master.task.commit.interval=1000
# only less than cpu avg load, master server can work. default value -1 : the number of cpu cores * 2
#master.max.cpuload.avg=-1
# only larger than reserved memory, master server can work. default value : physical memory * 1/10, unit is G.
#master.reserved.memory=0.3
# master listen port
#master.listen.port=5678
配置类ExecutorConfig引用配置文件scheduler.properties
使用核心注解@PropertySource(value = "classpath:scheduler.properties")来实现
@Component
@PropertySource(value = "classpath:scheduler.properties")
public class ExecutorConfig {
@Value("${master.exec.threads:50}")
private int masterExecThreads;
@Value("${master.exec.task.num:100}")
private int masterExecTaskNum;
@Value("${master.heartbeat.interval:10}")
private int masterHeartbeatInterval;
@Value("${master.task.commit.retryTimes:5}")
private int masterTaskCommitRetryTimes;
@Value("${master.dispatch.task.num :3}")
private int masterDispatchTaskNumber;
@Value("${master.task.commit.interval:1000}")
private int masterTaskCommitInterval;
@Value("${master.listen.port:5678}")
private int listenPort;
public int getListenPort() {
return listenPort;
}
public void setListenPort(int listenPort) {
this.listenPort = listenPort;
}
public int getMasterExecThreads() {
return masterExecThreads;
}
public void setMasterExecThreads(int masterExecThreads) {
this.masterExecThreads = masterExecThreads;
}
public int getMasterExecTaskNum() {
return masterExecTaskNum;
}
public void setMasterExecTaskNum(int masterExecTaskNum) {
this.masterExecTaskNum = masterExecTaskNum;
}
public int getMasterHeartbeatInterval() {
return masterHeartbeatInterval;
}
public void setMasterHeartbeatInterval(int masterHeartbeatInterval) {
this.masterHeartbeatInterval = masterHeartbeatInterval;
}
public int getMasterTaskCommitRetryTimes() {
return masterTaskCommitRetryTimes;
}
public void setMasterTaskCommitRetryTimes(int masterTaskCommitRetryTimes) {
this.masterTaskCommitRetryTimes = masterTaskCommitRetryTimes;
}
public int getMasterTaskCommitInterval() {
return masterTaskCommitInterval;
}
public void setMasterTaskCommitInterval(int masterTaskCommitInterval) {
this.masterTaskCommitInterval = masterTaskCommitInterval;
}
public int getMasterDispatchTaskNumber() {
return masterDispatchTaskNumber;
}
public void setMasterDispatchTaskNumber(int masterDispatchTaskNumber) {
this.masterDispatchTaskNumber = masterDispatchTaskNumber;
}
}
方式2
配置文件
resources/notification.template
【${level}】【${metric}】目标:${endpoint},${startTime}至${endTime}连续${slideCount}次${operator}设定阈值:[${threshold}]
【应用】${appEnName}
【标签】${labels}
【时间】${alarmTime}
代码加载使用配置文件
@Override
public void afterPropertiesSet() throws Exception {
// 加载通知模板
URL url = this.getClass().getClassLoader().getResource("notification.template");
if(url == null){
throw new FileNotFoundException("@@ 找不到notification.template");
}
String configContent = IOUtils.toString(url.openStream(), StandardCharsets.UTF_8);;
}
原创:做时间的朋友