利用actuator/refresh 实现配置动态刷新
1. 相关依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--用于监控和管理应用自身信息, 可以动态refresh配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--用于配置管理-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2.actuator 配置:
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
shutdown:
enabled: true
configprops:
enabled: true
3.测试类
@Slf4j
@Component
@RefreshScope
public class Schedule {
@Value("${schedule.fixed-delay:10000}")
private long fixedDelay = 20000;
@Scheduled(initialDelay = 1000, fixedDelayString = "${schedule.fixed-delay:10000}")
public void fixDelay(){
log.info("fixDelay, cron:{}, now:{}", fixedDelay, System.currentTimeMillis());
}
}
4.bootstrap.yml 配置如下:
schedule:
fixed-delay: 5000
5.日志如下:
2020-05-20 17:42:26.191 [scheduling-1] INFO [LOGID:] [cn.me.edge.codebase.schedule.Schedule:22] - fixDelay, cron:5000, now:1589967746191
2020-05-20 17:42:31.196 [scheduling-1] INFO [LOGID:] [cn.me.edge.codebase.schedule.Schedule:22] - fixDelay, cron:5000, now:1589967751196
2020-05-20 17:42:36.200 [scheduling-1] INFO [LOGID:] [cn.me.edge.codebase.schedule.Schedule:22] - fixDelay, cron:5000, now:1589967756199
2020-05-20 17:42:41.206 [scheduling-1] INFO [LOGID:] [cn.me.edge.codebase.schedule.Schedule:22] - fixDelay, cron:5000, now:1589967761205
6. 修改配置文件并refresh
schedule:
fixed-delay: 7000
# curl --request POST http://localhost:8888/actuator/refresh
返回 ["schedule.fixed-delay"]
但是此时发现schedule停止运行了😓
7.正确处理方式
@Configuration
@EnableScheduling
@Slf4j
@RefreshScope
public class SchedulingConfiguration implements ApplicationListener<RefreshScopeRefreshedEvent> {
@Autowired
private Schedule schedule;
@Scheduled(initialDelay = 1000, fixedDelayString = "${schedule.fixed-delay:10000}")
public void doSchedule(){
schedule.fixDelay();
}
@Override
public void onApplicationEvent(RefreshScopeRefreshedEvent refreshScopeRefreshedEvent) {
}
}
@Slf4j
@Component
@RefreshScope
public class Schedule {
@Value("${schedule.fixed-delay:10000}")
private long fixedDelay = 20000;
public void fixDelay(){
log.info("fixDelay, cron:{}, now:{}", fixedDelay, System.currentTimeMillis());
}
}
日志
2020-05-20 17:52:32.145 [scheduling-1] INFO [LOGID:] [cn.me.edge.codebase.schedule.Schedule:22] - fixDelay, cron:7000, now:1589968352145
2020-05-20 17:52:39.148 [scheduling-1] INFO [LOGID:] [cn.me.edge.codebase.schedule.Schedule:22] - fixDelay, cron:7000, now:1589968359148
2020-05-20 17:52:46.150 [scheduling-1] INFO [LOGID:] [cn.me.edge.codebase.schedule.Schedule:22] - fixDelay, cron:7000, now:1589968366150
2020-05-20 17:52:53.154 [scheduling-1] INFO [LOGID:] [cn.me.edge.codebase.schedule.Schedule:22] - fixDelay, cron:7000, now:1589968373154
2020-05-20 17:53:00.156 [scheduling-1] INFO [LOGID:] [cn.me.edge.codebase.schedule.Schedule:22] - fixDelay, cron:7000, now:1589968380155
网友评论