Com.Ctrip.Framework.Apollo.Configuration 3.0.0-rc.1

This is a prerelease version of Com.Ctrip.Framework.Apollo.Configuration.
dotnet add package Com.Ctrip.Framework.Apollo.Configuration --version 3.0.0-rc.1
                    
NuGet\Install-Package Com.Ctrip.Framework.Apollo.Configuration -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.Configuration" 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.Configuration" Version="3.0.0-rc.1" />
                    
Directory.Packages.props
<PackageReference Include="Com.Ctrip.Framework.Apollo.Configuration" />
                    
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.Configuration --version 3.0.0-rc.1
                    
#r "nuget: Com.Ctrip.Framework.Apollo.Configuration, 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.Configuration@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.Configuration&version=3.0.0-rc.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Com.Ctrip.Framework.Apollo.Configuration&version=3.0.0-rc.1&prerelease
                    
Install as a Cake Tool

一、准备工作

如果想将传统的config配置(如web.config)转成json配置,可以使用config2json工具

1.1 环境要求

3.0 目标框架为 .NET Framework 4.7.1 和 .NET 8。旧版 net40、net45、netstandard2.0 和 netstandard2.1 不再提供。

1.2 必选设置

Apollo客户端依赖于AppId,Environment等环境信息来工作,所以请确保阅读下面的说明并且做正确的配置:

默认配置依赖于Microsoft.Extensions.Configuration包,文档和Demo使用Json作为配置源,自定义配置源请参考微软官方文档

1.2.0 指定到网络主机的最大连接数

由于Long Poll会占用连接数量,因此可能需要配置最大连接数

仅.NET Framework需要配置

1.2.1 AppId

AppId是应用的身份信息,是从服务端获取配置的一个重要信息。

请确保appsettings.json有AppID的配置,其中内容形如:

{
  "apollo": {
    "AppId": "SampleApp"
  }
}

注:AppId是用来标识应用身份的唯一id,格式为string。

1.2.2 Environment

Apollo支持应用在不同的环境有不同的配置,所以Environment是另一个从服务器获取配置的重要信息。

请确保appsettings.json有Env的配置,其中内容形如:

{
  "apollo": {
     "Env": "PRO"
  }
}

目前,env支持以下几个值(大小写不敏感):

  • DEV
    • Development environment
  • FAT
    • Feature Acceptance Test environment
  • UAT
    • User Acceptance Test environment
  • PRO
    • Production environment

1.2.3 服务地址

Apollo客户端针对不同的环境会从不同的服务器获取配置,所以请确保在appsettings.json正确配置了服务器地址(MetaServer,不需要配置Env),其中内容形如:

{
  "apollo": {
    "MetaServer": "http://生产环境域名:8080"
  }
}

为了可以与Asp.net Core的环境配置文件想结合,比如可以在appsettings.Development.json中添加配置

{
  "apollo": {
    "MetaServer": "http://localhost:8080"
  }
}

当然也可以支持将所有的环境对应的meta server地址配置

{
  "apollo": {
    "Meta": {
      "DEV": "http://81.68.181.139:8080/",
      "FAT": "http://81.68.181.139:8080/",
      "UAT": "http://81.68.181.139:8080/",
      "PRO": "http://81.68.181.139:8080/"
    }
  }
}

1.2.4 本地缓存路径

Apollo客户端会把从服务端获取到的配置在本地文件系统缓存一份,用于在遇到服务不可用,或网络不通的时候,依然能从本地恢复配置,不影响应用正常运行。

本地缓存路径位于C:\opt\data\{appId}\config-cache,所以请确保C:\opt\data\目录存在,且应用有读写权限。

1.2.5 可选设置

Label(灰度标签)

设置 Apollo:Label 参与 Apollo 标签灰度规则匹配;不设置时不发送标签。不同标签的实例如果共用同一台机器,应配置不同的 LocalCacheDir,避免离线时读取彼此的本地缓存。

Cluster(集群)

Apollo支持配置按照集群划分,也就是说对于一个appId和一个环境,对不同的集群可以有不同的配置。

如果需要使用这个功能,你可以通过以下方式来指定运行时的集群:

  • 我们可以在appsettings.json文件中设置Cluster来指定运行时集群(注意大小写)
{
  "apollo": {
    "Cluster": "SomeCluster"
  }
}

Cluster Precedence(集群顺序)

  1. 如果Cluster和DataCenter同时指定:

    • 我们会首先尝试从Cluster指定的集群加载配置
    • 如果没找到任何配置,会尝试从DataCenter指定的集群加载配置
    • 如果还是没找到,会从默认的集群(default)加载
  2. 如果只指定了Cluster:

    • 我们会首先尝试从Cluster指定的集群加载配置
    • 如果没找到,会从默认的集群(default)加载
  3. 如果只指定了DataCenter:

    • 我们会首先尝试从DataCenter指定的集群加载配置
    • 如果没找到,会从默认的集群(default)加载
  4. 如果Cluster和DataCenter都没有指定:

    • 我们会从默认的集群(default)加载配置

1.3 使用非Properies格式的namespace

内部使用namespace的后缀来判断namespace类型,比如application.json时,会使用json格式来解析数据,内部默认实现了json和xml两种格式,可覆盖,yml和yaml可使用Apollo.ConfigAdapter.Yaml包,其他格式需要自行实现。

  1. 实现IConfigAdapter或者继承ContentConfigAdapter
  2. 使用ConfigAdapterRegister.AddAdapter注册实现的类的实例(Properties不能被覆盖)

二、引入方式

安装包Com.Ctrip.Framework.Apollo.Configuration

三、客户端用法

参考配置

{
  "apollo": {
    "AppId": "apollo.net",
    "MetaServer": "http://localhost:8080/",
    "Namespaces": [ "some namespace", "application.json", "application" ]
  }
}

3.1 修改Program.cs文件

3.1.1 配置在appsettings.json中

    WebHost.CreateDefaultBuilder(args)
+       .ConfigureAppConfiguration(builder => builder
+           .AddApollo(builder.Build().GetSection("apollo")))
        .UseStartup<Startup>()

3.1.2 配置在环境变量或者参数中(建议运行在Docker中使用此方式)

    WebHost.CreateDefaultBuilder(args)
+       .ConfigureAppConfiguration((cotnext, builder) => builder
+           .AddApollo(cotnext.Configuration.GetSection("apollo")))
        .UseStartup<Startup>()

3.2 监听配置变化事件

本项目不再提供原生 Apollo 配置变更通知接口。需要感知配置变化时,请使用 IConfiguration 提供的变更通知机制,例如 IOptionsMonitor 或 ChangeToken。

3.3 Demo

apollo.net项目中有多个样例客户端的项目:

四、FAQ

4.1 如何允许类似Sping的PlaceHolder功能

dotnet add package PW.Extensions.Configuration.Placeholder

4.2 Apollo内部HttpClient如何配置代理

    WebHost.CreateDefaultBuilder(args)
+       .ConfigureAppConfiguration(builder =>
+       {
+           var apollo = builder.Build().GetSection("apollo").Get<ApolloOptions>();
+           apollo.HttpMessageHandler = new HttpClientHandler
+           {
+               UseProxy = true,
+               Proxy = new WebProxy(new Uri("http://代理地址"))
+           };
+
+           builder
+               .AddApollo(apollo)
+               .AddNamespace("RD.SharedConfiguration")
+               .AddDefault();
+       })
        .UseStartup<Startup>()

4.3 如何跳过meta service的服务发现

用于解决apollo服务端部署在docker中时,不能在容器外面获取配置的问题。也可以直接指定IP或IP+Port

{
    "apollo": {
+       "ConfigServer": ["http://81.68.181.139:8080/"]
    }
}

4.4 如何使用访问密钥

配置对应的环境的Secret即可

{
    "apollo": {
+       "Secret": "服务端配置的值"
    }
}
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 (69)

Showing the top 5 NuGet packages that depend on Com.Ctrip.Framework.Apollo.Configuration:

Package Downloads
EachShow.Host

主机启动程序

PiratesCore

湖南医标通信息科技有限公司

GoldCloud.Infrastructure.Configuration

Package Description

Zq.Utils.Core

.NET Standard2.0、.NET Standard2.1、.NET5、.NET6版本工具类

Amo.Lib.CoreApi

Dotnet Core Api, quick start, Nlog, DI, Swagger, Service Register(Apollo).Need Amo.Lib

GitHub repositories (8)

Showing the top 8 popular GitHub repositories that depend on Com.Ctrip.Framework.Apollo.Configuration:

Repository Stars
anjoy8/Blog.Core
💖 ASP.NET Core 8.0 全家桶教程,前后端分离后端接口,vue教程姊妹篇,官方文档:
microsurging/surging
Surging is a micro-service engine that provides a lightweight, high-performance, modular RPC request pipeline. support Event-based Asynchronous Pattern and reactive programming.
trueai-org/module-shop
一个基于 .NET 8.0 构建的简单、跨平台、模块化的商城系统
guoming/Hummingbird
分布式锁,分布式ID,分布式消息队列、配置中心、注册中心、服务注册发现、超时、重试、熔断、负载均衡
overtly/core-grpc
C# Grpc、Consul结合驱动封装,Overt.Core.Grpc基于Consul/Grpc实现服务注册服务发现,支持dotnetcore / framework,可快速实现基于Grpc的微服务;另外Overt.Core.Grpc.H2基于Consul/Grpc.Net实现服务注册服务发现,支持Net5.0, 内部有完整案例,包含服务端Server 客户端 Client,core+grpc, netcore+grpc, dotnetcore+grpc
LeonKou/NetPro
🌈An enhanced version with clean architecture of asp.netcore,efficiently manage startup,Support for netcore3.1/6.0
PeyShine/Demo.MicroServer
一个基于AspDotNetCore+Swagger+Consul+Polly+Ocelot+IdentityServer4+Exceptionless+Apollo+SkyWalking的微服务开发框架
witskeeper/geektime
Version Downloads Last Updated
3.0.0-rc.1 0 12/6/2026
2.11.0 88,770 12/15/2024
2.10.2 278,101 8/8/2023
2.10.1 136,227 2/20/2023
2.10.0 6,787 2/8/2023
2.9.1 22,547 12/5/2022
2.9.0 48,583 9/26/2022
2.8.0 51,884 8/21/2022
2.7.0 91,981 5/19/2022
2.6.3 14,623 5/6/2022
2.6.2 90,879 1/21/2022
2.6.1 14,488 1/4/2022
2.5.0 69,380 11/8/2021
2.4.1.1 118,330 8/20/2021
2.4.1 775,754 8/12/2020
2.3.0 231,074 12/19/2019
Loading failed