feat: 添加时间同步配置功能至安装脚本

- 在 install.sh 中新增 sync_time 函数,配置系统时间同步,设置时区为 Asia/Shanghai,并安装 chrony。
- 配置 NTP 服务器为阿里云和腾讯云,确保时间同步的准确性。
- 更新主函数以调用时间同步配置,优化安装流程。
This commit is contained in:
2025-12-24 03:31:35 +08:00
parent 7a104bbe42
commit c9c4da01b6
5 changed files with 189 additions and 267 deletions

View File

@@ -67,11 +67,10 @@ func GetNodeLocation() (country, province, city, isp string) {
}
type Reporter struct {
cfg *config.Config
client *http.Client
logger *zap.Logger
stopCh chan struct{}
beijingTZ *time.Location
cfg *config.Config
client *http.Client
logger *zap.Logger
stopCh chan struct{}
}
func NewReporter(cfg *config.Config) *Reporter {
@@ -80,22 +79,13 @@ func NewReporter(cfg *config.Config) *Reporter {
// 初始化节点信息(从配置文件读取)
InitNodeInfo(cfg)
// 加载北京时间时区
beijingTZ, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
// 如果加载失败使用UTC+8手动创建
beijingTZ = time.FixedZone("CST", 8*60*60)
logger.Warn("加载时区失败使用UTC+8", zap.Error(err))
}
return &Reporter{
cfg: cfg,
client: &http.Client{
Timeout: 10 * time.Second,
},
logger: logger,
stopCh: make(chan struct{}),
beijingTZ: beijingTZ,
logger: logger,
stopCh: make(chan struct{}),
}
}
@@ -104,9 +94,8 @@ func (r *Reporter) Start(ctx context.Context) {
r.sendHeartbeat()
for {
// 获取当前北京时间
now := time.Now().In(r.beijingTZ)
// 计算到下一分钟第1秒的时间基于北京时间
// 计算到下一分钟第1秒的时间
now := time.Now()
nextMinute := now.Truncate(time.Minute).Add(time.Minute)
nextHeartbeatTime := nextMinute.Add(1 * time.Second)
durationUntilNext := nextHeartbeatTime.Sub(now)
@@ -122,7 +111,7 @@ func (r *Reporter) Start(ctx context.Context) {
timer.Stop()
return
case <-timer.C:
// 在每分钟的第1秒发送心跳(北京时间)
// 在每分钟的第1秒发送心跳
r.sendHeartbeat()
}
timer.Stop()