Skip to content

通用配置

Peihao Yang edited this page Sep 30, 2021 · 1 revision

Sentinel 的运行需要一些配置启动项,比如启动监控日志,用户自定义日志目录,统计相关的一些设置。

配置项详解

Sentinel 支持的配置项的定义如下:

pub struct ConfigEntity {
    pub(super) version: String,
    pub(super) config: SentinelConfig,
}

// SentinelConfig represent the general configuration of Sentinel.
pub(super) struct SentinelConfig {
    pub(super) app: AppConfig,
    pub(super) log: LogConfig,
    pub(super) stat: StatConfig,
    // use_cache_time indicates whether to cache time(ms), it is false by default
    pub(super) use_cache_time: bool,
}

pub(super) struct AppConfig {
    // app_name represents the name of current running service.
    pub(super) app_name: String,
    // app_type indicates the resource_type of the service (e.g. web service, API gateway).
    pub(super) app_type: ResourceType,
}

// LogMetricConfig represents the configuration items of the metric log.
pub(super) struct LogMetricConfig {
    pub(super) single_file_max_size: u64,
    pub(super) max_file_count: u32,
    pub(super) flush_interval_sec: u32,
}

// LogConfig represent the configuration of logging in Sentinel.
pub(super) struct LogConfig {
    // logger indicates that using logger to replace default logging.
    pub(super) logger: Logger,
    // metric represents the configuration items of the metric log.
    pub(super) metric: LogMetricConfig,
}

// SystemStatConfig represents the configuration items of system statistic collector
pub(super) struct SystemStatConfig {
    // interval_ms represents the collecting interval of the system metrics collector.
    pub(super) system_interval_ms: u32,
    // load_interval_ms represents the collecting interval of the system load collector.
    pub(super) load_interval_ms: u32,
    // cpu_interval_ms represents the collecting interval of the system cpu usage collector.
    pub(super) cpu_interval_ms: u32,
    // memory_interval_ms represents the collecting interval of the system memory usage collector.
    pub(super) memory_interval_ms: u32,
}

pub(super) struct StatConfig {
    // sample_count_total and interval_ms_total is the per resource's global default statistic sliding window config
    pub(super) sample_count_total: u32,
    pub(super) interval_ms_total: u32,
    // sample_count and interval_ms is the per resource's default readonly metric statistic
    // This default readonly metric statistic must be reusable based on global statistic.
    pub(super) sample_count: u32,
    pub(super) interval_ms: u32,
    pub(super) system: SystemStatConfig,
}

字段详细解释:

key 含义 默认值 备注
version 配置的版本 v1
config.app.name 项目名称 unknown_service 可通过环境变量配置,若环境变量 SENTINEL_APP_NAME 和文件中均未配置,则标为 unknown_service
config.app.type 项目类型 0 可通过环境变量配置,若环境变量 SENTINEL_APP_TYPE 和文件中均未配置,则标为 0
config.log.logger 用户自定义Logger Logger::EnvLogger Sentinel 默认支持 env_logger 和 log4rs 两种 logger
config.log.dir 日志路径 ${user.home}/logs/csp
config.log.usePid 监控日志文件名是否带上进程 PID false
config.log.metric.max_file_count 监控日志最大文件数目 8
config.log.metric.single_file_max_size 监控日志单个文件大小上限 50 MB (1024*1024*50)
config.log.metric.flush_interval_sec 监控日志聚合和刷盘的时间频率 1s 若无特殊需要,请采用默认值。若设为 0 则关闭监控日志输出。
config.stat.system.global_statistic_sample_count_total Resource的全局滑动窗口的统计格子数 20 若无特殊需要,请采用默认值。
config.stat.system.global_statistic_interval_ms_total Resource的全局滑动窗口的间隔时间(ms) 10000 若无特殊需要,请采用默认值。
config.stat.system.metric_statistic_sample_count Resource的默认监控日志统计的滑动窗口的统计窗格数 2 若无特殊需要,请采用默认值。
config.stat.system.metric_statistic_interval_ms Resource的默认监控日志统计的滑动窗口的间隔时间 1000 若无特殊需要,请采用默认值。
config.stat.system.collect_interval_ms 系统指标收集的间隔时间 1000 若无特殊需要,请采用默认值。若设为 0 则关闭系统指标收集。
config.use_cached_time 是否使用缓存到的时间戳 (ms) true 若无特殊需要,请采用默认值。

如果不使用自适应限流模块 (core::system),可以通过设置config.stat.system.collect_interval_ms 为 0,表示关闭系统指标收集。 如果不使用监控日志,可以通过设置 config.log.metric.flush_interval_sec 为 0,表示关闭监控日志文件异步输出。

Sentinel 支持的配置方式

Sentinel 支持三种方式来初始化运行环境:yaml 文件、手动配置 config::ConfigEntity 以及环境变量。优先级是:环境变量大于手动配置。

yaml 配置文件 (WIP)

该部分内容尚未经过测试验证。

Sentinel的 API 提供了两个函数通过 yaml 文件初始化 Sentinel 运行环境:

  1. pub fn init_default() -> Result<()> 函数初始化,Sentinel 则会尝试从 SENTINEL_CONFIG_FILE_PATH 环境变量读取 path 并读取对应的文件。若未指定则默认从项目目录下的 sentinel.yml 文件读取配置;若均不存在,Sentinel 会通过环境变量读取基础的配置(如项目名称),其它配置项采用默认值。
  2. pub fn init_with_config_file(config_path: &mut String) -> Result<()> 使用用户指定路径的 yaml 文件,同时会通过环境变量读取基础的配置,然后初始化 Sentinel

如果 yaml 文件不存在,就是通过默认的全局配置项初始化 Sentinel。默认的全局配置项定义 可参考代码中的 Default Trait 实现。

YAML 配置文件示例:

version: "v1"
sentinel:
  app:
    name: sentinel-demo
    type: 0
  log:
    dir: "~/logs/csp"
    pid: false
    metric:
      max_file_count: 14
      flush_interval_sec: 1
  stat:
    system:
      collect_interval_ms: 1000

通过配置对象初始化 Sentinel 运行环境

api::init_with_config(entity: config::ConfigEntity) 函数初始化,用户需要自己制定配置项 config::ConfigEntity, Sentinel 会使用用户输入的配置项初始化。

环境变量配置项

环境变量的配置优先级是最高的,若和文件配置同时存在,则环境变量的配置会覆盖 YAML 文件的配置。Sentinel支持5个环境变量:

key 对应 yaml 配置项 含义 备注
SENTINEL_CONFIG_FILE_PATH - yaml 配置文件路径 若未指定则默认从项目目录下的 sentinel.yml 文件读取配置
SENTINEL_APP_NAME config.app.name 项目名称 若环境变量和文件中均未配置,则标为 unknown_service
SENTINEL_APP_TYPE config.app.type 项目类型 若环境变量和文件中均未配置,则标为 0
SENTINEL_LOG_DIR - 日志路径 默认路径为 ~/logs/csp
SENTINEL_LOG_USE_PID - 日志文件是否带 pid 默认为 false

以上配置项中,应用名称(config.app.name)为必需的配置项。