You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
5.3 KiB
C#
134 lines
5.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Dapper;
|
|
using MySql.Data.MySqlClient;
|
|
using log4net;
|
|
|
|
// 文件用途:从 CNC 系统配置表 cnc_sys_config 读取运行时配置,并覆盖默认 CollectorConfig 的设置
|
|
// 设计目标:在独立进程的采集服务中,不依赖仓储层,直接从数据库加载运行时参数
|
|
namespace CncCollector.Config
|
|
{
|
|
/// <summary>
|
|
/// 运行时配置加载器:从 cnc_sys_config 表读取配置,覆盖 CollectorConfig 的默认值
|
|
/// </summary>
|
|
public static class ConfigLoader
|
|
{
|
|
private static readonly ILog _log = LogManager.GetLogger(typeof(ConfigLoader));
|
|
|
|
/// <summary>
|
|
/// 从数据库加载运行时配置并覆盖给定的 CollectorConfig 实例的默认值。
|
|
/// </summary>
|
|
/// <param name="connectionString">MySQL/MariaDB 连接字符串</param>
|
|
/// <param name="config">需要被覆盖的 CollectorConfig 实例(来自 CNC 采集服务)</param>
|
|
public static void LoadRuntimeConfig(string connectionString, CncModels.Entity.CollectorConfig config)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(connectionString))
|
|
{
|
|
_log.Warn("配置加载失败:连接字符串为空");
|
|
return;
|
|
}
|
|
|
|
if (config == null)
|
|
{
|
|
_log.Warn("配置加载失败:传入的 CollectorConfig 为 null");
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
using (var conn = new MySqlConnection(connectionString))
|
|
{
|
|
conn.Open();
|
|
// 需要加载的键集合
|
|
var keys = new[]
|
|
{
|
|
"collector_api_port",
|
|
"collector_api_key",
|
|
"heartbeat_interval",
|
|
"config_poll_interval",
|
|
"daily_summary_time",
|
|
"collect_retry_count",
|
|
"collect_retry_interval",
|
|
"collect_fail_alert_threshold"
|
|
};
|
|
|
|
foreach (var key in keys)
|
|
{
|
|
var value = conn.QueryFirstOrDefault<string>(
|
|
"SELECT config_value FROM cnc_sys_config WHERE config_key = @Key",
|
|
new { Key = key });
|
|
if (value == null) continue;
|
|
Apply(config, key, value);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.Error("加载运行时配置异常", ex);
|
|
}
|
|
}
|
|
|
|
private static void Apply(CncModels.Entity.CollectorConfig config, string key, string value)
|
|
{
|
|
try
|
|
{
|
|
switch (key)
|
|
{
|
|
case "collector_api_port":
|
|
SetIntProperty(config, nameof(config.ApiPort), value);
|
|
break;
|
|
case "collector_api_key":
|
|
SetStringProperty(config, nameof(config.ApiKey), value);
|
|
break;
|
|
case "heartbeat_interval":
|
|
SetIntProperty(config, nameof(config.HeartbeatInterval), value);
|
|
break;
|
|
case "config_poll_interval":
|
|
SetIntProperty(config, nameof(config.ConfigPollInterval), value);
|
|
break;
|
|
case "daily_summary_time":
|
|
SetTimeSpanProperty(config, nameof(config.DailySummaryTime), value);
|
|
break;
|
|
case "collect_retry_count":
|
|
SetIntProperty(config, nameof(config.CollectRetryCount), value);
|
|
break;
|
|
case "collect_retry_interval":
|
|
SetIntProperty(config, nameof(config.CollectRetryIntervalSeconds), value);
|
|
break;
|
|
case "collect_fail_alert_threshold":
|
|
SetIntProperty(config, nameof(config.CollectFailAlertThreshold), value);
|
|
break;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// 忽略单项配置解析失败,继续加载其他配置
|
|
}
|
|
}
|
|
|
|
private static void SetIntProperty(CncModels.Entity.CollectorConfig obj, string propName, string value)
|
|
{
|
|
if (obj == null) return;
|
|
var p = obj.GetType().GetProperty(propName);
|
|
if (p == null || !p.CanWrite) return;
|
|
if (int.TryParse(value, out var v)) p.SetValue(obj, v);
|
|
}
|
|
|
|
private static void SetStringProperty(CncModels.Entity.CollectorConfig obj, string propName, string value)
|
|
{
|
|
if (obj == null) return;
|
|
var p = obj.GetType().GetProperty(propName);
|
|
if (p == null || !p.CanWrite) return;
|
|
p.SetValue(obj, value);
|
|
}
|
|
|
|
private static void SetTimeSpanProperty(CncModels.Entity.CollectorConfig obj, string propName, string value)
|
|
{
|
|
if (obj == null) return;
|
|
var p = obj.GetType().GetProperty(propName);
|
|
if (p == null || !p.CanWrite) return;
|
|
if (TimeSpan.TryParse(value, out var ts)) p.SetValue(obj, ts);
|
|
}
|
|
}
|
|
}
|