Com.Ctrip.Framework.Apollo.ConfigAdapter.Yaml 3.0.0-rc.1

This is a prerelease version of Com.Ctrip.Framework.Apollo.ConfigAdapter.Yaml.
dotnet add package Com.Ctrip.Framework.Apollo.ConfigAdapter.Yaml --version 3.0.0-rc.1
                    
NuGet\Install-Package Com.Ctrip.Framework.Apollo.ConfigAdapter.Yaml -Version 3.0.0-rc.1
                    
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="Com.Ctrip.Framework.Apollo.ConfigAdapter.Yaml" Version="3.0.0-rc.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Com.Ctrip.Framework.Apollo.ConfigAdapter.Yaml" Version="3.0.0-rc.1" />
                    
Directory.Packages.props
<PackageReference Include="Com.Ctrip.Framework.Apollo.ConfigAdapter.Yaml" />
                    
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 Com.Ctrip.Framework.Apollo.ConfigAdapter.Yaml --version 3.0.0-rc.1
                    
#r "nuget: Com.Ctrip.Framework.Apollo.ConfigAdapter.Yaml, 3.0.0-rc.1"
                    
#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 Com.Ctrip.Framework.Apollo.ConfigAdapter.Yaml@3.0.0-rc.1
                    
#: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=Com.Ctrip.Framework.Apollo.ConfigAdapter.Yaml&version=3.0.0-rc.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Com.Ctrip.Framework.Apollo.ConfigAdapter.Yaml&version=3.0.0-rc.1&prerelease
                    
Install as a Cake Tool

Apollo配置中心的.Net客户端,更多关于Apollo配置中心的介绍,可以查看Apollo配置中心Wiki。

本地 Demo 和集成测试

安装并启动 Docker,首次运行需要拉取 mysql 和 nobodyiam/apollo-quick-start:3.0.0 镜像。集成测试通过 Testcontainers 自动启动隔离的 Apollo 和 MySQL,使用随机端口,结束后删除容器;不需要预先启动远程 Apollo。

每个 Demo 启动时都会自动创建独立的 Apollo 和 MySQL 容器,并在退出时清理,无需单独启动服务。传统 ASP.NET Demo 在首次请求触发应用启动时创建容器。容器使用随机端口,每次启动都会重新初始化数据。

数据库初始化脚本来自官方 Apollo Quick Start,测试数据定义在 demo\Apollo.TestServer\Sql\03-fixture.sql。

一、框架集成

  1. 与Microsoft.Extensions.Configuration集成请参考Com.Ctrip.Framework.Apollo.Configuration文档,完全支持IConfiguration的变动通知
  2. 与System.Configuration.ConfigurationManager集成(.NET Framework 4.7.1)请参考Com.Ctrip.Framework.Apollo.ConfigurationManager文档
  3. 如果想将传统的config配置(如web.config)转成json配置,可以使用config2json工具

二、日志输出

默认Sdk内部的日志不会输出,需要输出日志的请设置Com.Ctrip.Framework.Apollo.Logging.LogManager.LogFactory属性。

内部内实现了控制台日志

LogManager.UseConsoleLogging(logLevel);

三、客户端设计

client-architecture

上图简要描述了Apollo客户端的实现原理:

  1. 客户端和服务端保持了一个长连接,从而能第一时间获得配置更新的推送。(通过Http Long Polling实现)
  2. 客户端还会定时从Apollo配置中心服务端拉取应用的最新配置。
    • 这是一个fallback机制,为了防止推送机制失效导致配置不更新
    • 客户端定时拉取会上报本地版本,所以一般情况下,对于定时拉取的操作,服务端都会返回304 - Not Modified
    • 定时频率默认为每5分钟拉取一次,客户端也可以通过App.config设置Apollo.RefreshInterval或者appsettings.json设置Apollo:RefreshInterval来覆盖,单位为毫秒。
    • 支持 Apollo 2.5 及以上服务端的增量配置同步;遇到无法处理的增量响应时会尝试重新获取完整配置。
  3. 客户端从Apollo配置中心服务端获取到应用的最新配置后,会保存在内存中
  4. 客户端会把从服务端获取到的配置在本地文件系统缓存一份
    • 在遇到服务不可用,或网络不通的时候,依然能从本地恢复配置
  5. 应用程序可以从Apollo客户端获取最新的配置、订阅配置更新通知

四、本地开发模式

当开发环境无法连接Apollo服务器的时候,会降级为读取本地配置文件,请先在普通模式下使用Apollo,这样Apollo会自动创建该目录并在目录下生成配置文件。

Apollo不会实时监测文件内容是否有变化,所以如果修改了配置,需要重启应用生效。

4.1 本地配置目录

本地配置目录位于:

  • Mac/Linux: /opt/data/{appId}/config-cache
  • Windows: C:\opt\data\{appId}\config-cache

appId就是应用的appId,如100004458。

请确保该目录存在,且应用程序对该目录有读权限。

【小技巧】 推荐的方式是先在普通模式下使用Apollo,这样Apollo会自动创建该目录并在目录下生成配置文件。

4.2 本地配置文件

本地配置文件需要按照一定的文件名格式放置于本地配置目录下,文件名格式如下:

{appId}+{cluster}+{namespace}.json

  • appId就是应用自己的appId,如100004458
  • cluster就是应用使用的集群,一般在本地模式下没有做过配置的话,就是default
  • namespace就是应用使用配置namespace,一般是application client-local-cache

文件内容以json格式存储,比如如果有两个key,一个是request.timeout,另一个是batch,那么文件内容就是如下格式:

{
    "request.timeout":"1000",
    "batch":"2000"
}
Product Compatible and additional computed target framework versions.
.NET 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 was computed.  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. 
.NET Framework net471 is compatible.  net472 was computed.  net48 was computed.  net481 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
3.0.0-rc.1 0 12/6/2026
2.11.0 2,419 12/15/2024
2.9.0 2,244 9/26/2022
2.8.0 776 8/21/2022
2.6.1 1,419 1/11/2022
2.6.0 609 12/30/2021
2.5.0 676 11/8/2021
2.2.0 2,851 11/15/2019
2.1.0 898 9/19/2019