Skip to content

配置源概述

Apq.Cfg 支持多种配置源,可以灵活组合使用。

配置源分类

本地配置源

从本地文件或环境读取配置:

配置源NuGet 包说明
JSONApq.Cfg (内置)JSON 格式配置文件
YAMLApq.Cfg.YamlYAML 格式配置文件
XMLApq.Cfg.XmlXML 格式配置文件
INIApq.Cfg.IniINI 格式配置文件
TOMLApq.Cfg.TomlTOML 格式配置文件
环境变量Apq.Cfg (内置)系统环境变量

远程配置源

从远程配置中心读取配置:

配置源NuGet 包说明
ConsulApq.Cfg.ConsulHashiCorp Consul
RedisApq.Cfg.RedisRedis 键值存储
ApolloApq.Cfg.Apollo携程 Apollo 配置中心
VaultApq.Cfg.VaultHashiCorp Vault
EtcdApq.Cfg.EtcdEtcd 分布式键值存储
ZookeeperApq.Cfg.ZookeeperApache Zookeeper
NacosApq.Cfg.Nacos阿里巴巴 Nacos

配置源选择指南

开发环境

推荐使用本地文件配置:

csharp
var cfg = new CfgBuilder()
    .AddJson("config.json", level: 0, writeable: false)
    .AddJson("config.Development.json", level: 1, writeable: false, optional: true)
    .AddEnvironmentVariables(level: 2, prefix: "APP_")
    .Build();

生产环境

推荐使用远程配置中心:

csharp
var cfg = new CfgBuilder()
    .AddJson("config.json", level: 0, writeable: false)  // 基础配置
    .AddSource(new ConsulCfgSource("http://consul:8500", "myapp/config", level: 10))  // 动态配置
    .AddSource(new VaultCfgSource("https://vault:8200", "secret/myapp", level: 15))   // 敏感配置
    .Build();

混合部署

csharp
var cfg = new CfgBuilder()
    // 本地基础配置
    .AddJson("config.json", level: 0, writeable: false)
    // 环境特定配置
    .AddJson($"config.{env}.json", level: 1, writeable: false, optional: true)
    // 远程配置(可选,用于动态更新)
    .AddSource(new ConsulCfgSource("http://consul:8500", "myapp/config", level: 10, optional: true))
    // 环境变量覆盖
    .AddEnvironmentVariables(level: 20, prefix: "APP_")
    .Build();

配置源特性对比

特性JSONYAMLConsulRedisApollo
动态重载
分布式
版本控制
灰度发布
权限控制
审计日志

配置源优先级

使用 level 参数控制优先级,数值越大优先级越高:

csharp
var cfg = new CfgBuilder()
    .AddJson("base.json", level: 0, writeable: false)           // 优先级 0(最低)
    .AddYaml("override.yaml", level: 1, writeable: false)       // 优先级 1
    .AddSource(new ConsulCfgSource(..., level: 10))             // 优先级 10
    .AddEnvironmentVariables(level: 20, prefix: "APP_")         // 优先级 20(最高)
    .Build();

下一步

选择您需要的配置源了解详细用法:

基于 MIT 许可发布