Apq.Cfg.Apollo 1.0.4

dotnet add package Apq.Cfg.Apollo --version 1.0.4
                    
NuGet\Install-Package Apq.Cfg.Apollo -Version 1.0.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Apq.Cfg.Apollo" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Apq.Cfg.Apollo" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="Apq.Cfg.Apollo" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Apq.Cfg.Apollo --version 1.0.4
                    
#r "nuget: Apq.Cfg.Apollo, 1.0.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Apq.Cfg.Apollo@1.0.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Apq.Cfg.Apollo&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=Apq.Cfg.Apollo&version=1.0.4
                    
Install as a Cake Tool

Apq.Cfg.Apollo

NuGet

Apollo 配置中心支持,为 Apq.Cfg 提供从 Apollo 读取配置的能力,支持热重载。

安装

dotnet add package Apq.Cfg.Apollo

快速开始

using Apq.Cfg;
using Apq.Cfg.Apollo;

var cfg = CfgBuilder.Create()
    .AddJson("config.json", level: 0)
    .AddApollo(options =>
    {
        options.AppId = "my-app";
        options.MetaServer = "http://localhost:8080";
        options.Cluster = "default";
        options.Namespaces = new[] { "application" };
        options.EnableHotReload = true;
    }, level: 10)
    .Build();

// 读取配置
var value = cfg.Get("Database:Host");

// 订阅配置变更
cfg.ConfigChanges.Subscribe(change =>
{
    Console.WriteLine($"配置变更: {change.Key} = {change.NewValue}");
});

配置选项

选项 类型 默认值 说明
AppId string "" Apollo 应用 ID
MetaServer string http://localhost:8080 Meta Server 地址
Cluster string default 集群名称
Namespaces string[] ["application"] 命名空间列表
Secret string? null 访问密钥(可选)
EnableHotReload bool true 是否启用热重载
ConnectTimeout TimeSpan 10 秒 连接超时时间
LongPollingTimeout TimeSpan 90 秒 长轮询超时时间
DataFormat ApolloDataFormat Properties 配置数据格式

多命名空间

Apollo 支持从多个命名空间读取配置:

.AddApollo(options =>
{
    options.AppId = "my-app";
    options.MetaServer = "http://localhost:8080";
    options.Namespaces = new[] { "application", "common", "database" };
})

当配置多个命名空间时,非 application 命名空间的配置键会自动添加命名空间前缀:

// application 命名空间的配置
var appName = cfg.Get("App:Name");

// common 命名空间的配置
var logLevel = cfg.Get("common:Logging:Level");

// database 命名空间的配置
var connStr = cfg.Get("database:ConnectionString");

热重载

启用 EnableHotReload 后,配置源会使用 Apollo 的长轮询通知机制监听配置变更,当配置发生变化时自动更新。

// 启用热重载
.AddApollo(options =>
{
    options.EnableHotReload = true;
    options.LongPollingTimeout = TimeSpan.FromSeconds(90);
})

认证

Secret 签名认证

.AddApollo(options =>
{
    options.AppId = "my-app";
    options.MetaServer = "http://localhost:8080";
    options.Secret = "your-app-secret";
})

多层级配置

Apollo 配置源可以与其他配置源组合使用,通过 level 参数控制优先级:

var cfg = CfgBuilder.Create()
    .AddJson("config.json", level: 0)           // 基础配置
    .AddJson("config.local.json", level: 1)     // 本地覆盖
    .AddApollo(options =>                        // Apollo 远程配置(最高优先级)
    {
        options.AppId = "my-app";
        options.MetaServer = "http://apollo:8080";
    }, level: 10)
    .Build();

配置写入

注意:Apollo 配置源不支持通过 API 写入配置。如需修改配置,请通过 Apollo 管理界面操作。

// 以下代码会抛出 NotSupportedException
cfg.Set("Key", "Value");
await cfg.SaveAsync();  // 抛出异常

简化用法

// 使用简化的扩展方法
var cfg = CfgBuilder.Create()
    .AddApollo("my-app", "http://localhost:8080",
        namespaces: new[] { "application", "common" },
        level: 10)
    .Build();

与其他配置中心对比

特性 Apollo Nacos Consul Etcd
写入支持
多命名空间
多集群
灰度发布
本地缓存 ✅(内置)

依赖

无外部依赖,使用 HTTP API 直接与 Apollo 通信。

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 84 12/26/2025