AgileConfig.Client
1.7.4
See the version list below for details.
dotnet add package AgileConfig.Client --version 1.7.4
NuGet\Install-Package AgileConfig.Client -Version 1.7.4
<PackageReference Include="AgileConfig.Client" Version="1.7.4" />
<PackageVersion Include="AgileConfig.Client" Version="1.7.4" />
<PackageReference Include="AgileConfig.Client" />
paket add AgileConfig.Client --version 1.7.4
#r "nuget: AgileConfig.Client, 1.7.4"
#:package AgileConfig.Client@1.7.4
#addin nuget:?package=AgileConfig.Client&version=1.7.4
#tool nuget:?package=AgileConfig.Client&version=1.7.4
AgileConfig_Client
AgileConfig 的客户端,.net core standard2.0实现 。
使用客户端
安装客户端
Install-Package AgileConfig.Client
☢️☢️☢️如果你的程序是Framework的程序请使用AgileConfig.Client4FR这个专门为Framework打造的client。使用当前版本有可能死锁造成cpu100% 的风险。☢️☢️☢️
初始化客户端
以asp.net core mvc项目为例:
在appsettings.json文件内配置agileconfig的连接信息。
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
//agile_config
"AgileConfig": {
"appId": "app",
"secret": "xxx",
"nodes": "http://localhost:5000,http://localhost:5001"//多个节点使用逗号分隔,
"name": "client1",
"tag": "tag1",
"env": "DEV",
"httpTimeout": "100",
"cache": {
"directory": "agile/config"
}
}
}
配置项说明
| 配置项名称 | 数据类型 | 配置项说明 | 是否必填 | 备注 |
|---|---|---|---|---|
| appid | string | 应用ID | 是 | 对应后台管理中应用的应用ID |
| secret | string | 应用密钥 | 是 | 对应后台管理中应用的密钥 |
| nodes | string | 应用配置节点 | 是 | 存在多个节点则使用逗号,分隔 |
| name | string | 连接客户端的自定义名称 | 否 | 方便在agile配置中心后台对当前客户端进行查阅与管理 |
| tag | string | 连接客户端自定义标签 | 否 | 方便在agile配置中心后台对当前客户端进行查阅与管理 |
| env | string | 配置中心的环境 | 否 | 通过此配置决定拉取哪个环境的配置信息;如果不配置,服务端会默认返回第一个环境的配置 |
| cache | string | 客户端的配置缓存设置 | 否 | 通过此配置可对拉取到本地的配置项文件进行相关设置 |
| cache:enabled | bool | 是否在本地缓存上一次拉取的配置 | 否 | 默认 true |
| cache:directory | string | 客户端的配置缓存文件存储地址配置 | 否 | 如设置了此目录则将拉取到的配置项cache文件存储到该目录,否则直接存储到站点根目录 |
| cache:config_encrypt | bool | 客户端缓存文件加密设置 | 否 | 如果设置为true则对缓存的文件内容进行加密 |
| httpTimeout | int | http请求超时时间 | 否 | 配置 client 发送 http 请求的时候的超时时间,默认100s |
UseAgileConfig
在 program 类上使用 UseAgileConfig 扩展方法,该方法会配置一个 AgileConfig 的配置源。
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseAgileConfig(e => Console.WriteLine($"configs {e.Action}"))
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
读取配置
AgileConfig支持asp.net core 标准的IConfiguration,跟IOptions模式读取配置。还支持直接通过AgileConfigClient实例直接读取:
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IConfiguration _IConfiguration;
private readonly IOptions<DbConfigOptions> _dbOptions;
private readonly IConfigClient _IConfigClient;
public HomeController(ILogger<HomeController> logger, IConfiguration configuration, IOptions<DbConfigOptions> dbOptions, IConfigClient configClient)
{
_logger = logger;
_IConfiguration = configuration;
_dbOptions = dbOptions;
_IConfigClient = configClient;
}
public IActionResult Index()
{
return View();
}
/// <summary>
/// 使用IConfiguration读取配置
/// </summary>
/// <returns></returns>
public IActionResult ByIConfiguration()
{
var userId = _IConfiguration["userId"];
var dbConn = _IConfiguration["db:connection"];
ViewBag.userId = userId;
ViewBag.dbConn = dbConn;
return View();
}
/// <summary>
/// 直接使用ConfigClient的实例读取配置
/// </summary>
/// <returns></returns>
public IActionResult ByInstance()
{
var userId = _IConfigClient["userId"];
var dbConn = _IConfigClient["db:connection"];
ViewBag.userId = userId;
ViewBag.dbConn = dbConn;
return View("ByInstance");
}
/// <summary>
/// 使用Options模式读取配置
/// </summary>
/// <returns></returns>
public IActionResult ByOptions()
{
var dbConn = _dbOptions.Value.connection;
ViewBag.dbConn = dbConn;
return View("ByOptions");
}
}
使用服务注册&发现
在 appsettings.json 的 AgileConfig 节点添加 serviceRegister 节点:
"AgileConfig": {
"appId": "test_app",
"secret": "test_app",
"nodes": "http://agileconfig_server.xbaby.xyz/",
"name": "client123",
"tag": "tag123",
"serviceRegister": { //服务注册信息,如果不配置该节点,则不会启动任何跟服务注册相关的服务 可选
"serviceId": "net6", //服务id,全局唯一,用来唯一标示某个服务 ,client 1.6.12 开始不再必填。如果不填,则自动生产一个 guid
"serviceName": "net6MVC服务测试", //服务名,可以重复,某个服务多实例部署的时候这个serviceName就可以重复
"ip": "127.0.0.1", //服务的ip 可选
"port": 5005, //服务的端口 可选
}
其中 appId , secret 等配置同原来配置中心的使用方式没有任何改变。
serviceRegister 节点描述的是服务注册信息(如果删除这个节点那么服务注册功能就不会启动):
- serviceId
服务id,全局唯一,用来唯一标示某个服务,client 1.6.12 开始不再必填。如果不填,则自动生产一个 guid - serviceName
服务名,可以重复,某个服务多实例部署的时候这个serviceName就可以重复 - ip
服务的ip 可选 - port
服务的端口 可选 - metaData
一个字符串数组,可以携带一些服务的相关信息,如版本等 可选 - alarmUrl
告警地址 可选。
如果某个服务出现异常情况,如一段时间内没有心跳,那么服务端会往这个地址 POST 一个请求并且携带服务相关信息,用户可以自己去实现提醒功能,比如发短信,发邮件等:
{
"serviceId":"0001",
"serviceName":"xxxx",
"time":"2022-01-01T12:00:000",
"status":"Unhealty",
"message": "服务不健康"
}
- heartbeat:mode
指定心跳的模式,server/client 。server代表服务端主动检测,client代表客户端主动上报。不填默认client模式 可选 - heartbeat:interval
心跳的间隔,默认时间30s 可选 - heartbeat:url
心跳模式为 server 的时候需要填写健康检测地址,如果是httpstatus为200段则判定存活,其它都视为失败 可选
服务的注册
当配置好客户端后,启动对应的应用程序,服务信息会自动注册到服务端并且开始心跳。如果服务正确注册到服务端,控制台的服务管理界面可以查看:
服务发现
现在服务已经注册上去了,那么怎么才能拿到注册中心所有的服务呢?同样非常简单,在程序内只要注入IDiscoveryService 接口就可以通过它拿到所有的注册的服务。
public interface IDiscoveryService
{
string DataVersion { get; }
List<ServiceInfo> UnHealthyServices { get; }
List<ServiceInfo> HealthyServices { get; }
List<ServiceInfo> Services { get; }
Task RefreshAsync();
}
除了接口内置的方法,还有几个扩展方法方便用户使用,比如随机一个服务:
public static class DiscoveryServiceExtension
{
public static IEnumerable<ServiceInfo> GetByServiceName(this IDiscoveryService ds, string serviceName)
{
return ds.Services.GetByServiceName(serviceName);
}
public static ServiceInfo GetByServiceId(this IDiscoveryService ds, string serviceId)
{
return ds.Services.GetByServiceId(serviceId);
}
public static ServiceInfo RandomOne(this IDiscoveryService ds, string serviceName)
{
return ds.Services.RandomOne(serviceName);
}
}
联系我
有什么问题可以mail我:minj.zhou@gmail.com 也可以加qq群:1022985150
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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 Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.Configuration (>= 3.1.1)
- Microsoft.Extensions.Configuration.CommandLine (>= 3.1.1)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 3.1.1)
- Microsoft.Extensions.Configuration.FileExtensions (>= 3.1.0)
- Microsoft.Extensions.Configuration.Json (>= 3.1.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1.13)
- Microsoft.Extensions.Hosting.Abstractions (>= 3.1.1)
- Microsoft.Extensions.Logging (>= 3.1.1)
- Microsoft.Extensions.Logging.Abstractions (>= 3.1.1)
- Microsoft.Extensions.Logging.Console (>= 3.1.1)
NuGet packages (27)
Showing the top 5 NuGet packages that depend on AgileConfig.Client:
| Package | Downloads |
|---|---|
|
HandeSoft.Web.Core
Package Description |
|
|
Moci.CorDll
Package Description |
|
|
Zq.Utils.Core
.NET Standard2.0、.NET Standard2.1、.NET5、.NET6版本工具类 |
|
|
HwApp.AgileConfig
HwApp AgileConfig Integration |
|
|
Sze.Base.Core
Package Description |
GitHub repositories (3)
Showing the top 3 popular GitHub repositories that depend on AgileConfig.Client:
| Repository | Stars |
|---|---|
|
colinin/abp-next-admin
这是基于vue-vben-admin 模板适用于abp vNext的前端管理项目
|
|
|
LeonKou/NetPro
🌈An enhanced version with clean architecture of asp.netcore,efficiently manage startup,Support for netcore3.1/6.0
|
|
|
aehyok/.NET8.0
net8.0 efcore mysql redis rabbitmq 微服务
|
| Version | Downloads | Last Updated |
|---|---|---|
| 1.8.0 | 3,783 | 9/23/2025 |
| 1.7.6 | 12,735 | 1/11/2025 |
| 1.7.5 | 1,632 | 12/29/2024 |
| 1.7.4 | 581 | 12/20/2024 |
| 1.7.3 | 26,369 | 3/21/2024 |
| 1.7.2 | 9,078 | 1/9/2024 |
| 1.7.1 | 15,876 | 10/6/2023 |
| 1.7.0 | 2,830 | 9/12/2023 |
| 1.6.19 | 10,182 | 7/16/2023 |
| 1.6.18 | 1,175 | 6/28/2023 |
| 1.6.17 | 7,499 | 5/28/2023 |
| 1.6.16 | 1,394 | 5/25/2023 |
| 1.6.15 | 1,731 | 5/2/2023 |
| 1.6.14 | 613 | 4/28/2023 |
| 1.6.13 | 5,487 | 4/12/2023 |
| 1.6.12 | 4,760 | 3/6/2023 |
| 1.6.11 | 64,504 | 12/27/2022 |
| 1.6.10 | 1,222 | 12/15/2022 |
| 1.6.9 | 37,906 | 9/17/2022 |
| 1.6.8 | 8,313 | 9/1/2022 |