-
Notifications
You must be signed in to change notification settings - Fork 2
Jdk ntp 群集时间一致性
youfan edited this page Nov 18, 2022
·
2 revisions
The Network Time Protocol (NTP) is a networking protocol for clock synchronization between computer systems over packet-switched, variable-latency data networks.
变更操作系统时间需要管理员权限,大对数情况下java 进程没有管理员权限,如果在os层面处理时间同步那么就会不通用,每个机器都需要操作一遍。Rxlib实现注入System.currentTimeMillis()方法,通过ntp来同步时间offset,这样在java 进程层面来保证时间的一致性和通用性。对全局代码无任何入侵!
@Test
public void ntpClock() {
//硬码方式获取同步时间
NtpClock clock = NtpClock.UTC;
log.info("local ts {}", clock.millis());
//手动同步
NtpClock.sync();
log.info("ntp ts {}", clock.millis());
//定时同步
RxConfig.INSTANCE.getNet().getNtp().setSyncPeriod(2000);
NtpClock.scheduleTask();
for (int i = 0; i < 10; i++) {
sleep(2500);
log.info("ntp ts {}", clock.millis());
}
//注入System.currentTimeMillis()方法,全局代码无入侵
NtpClock.TimeAdvice.transform();
}