AgileConfig.Client 1.7.4

There is a newer version of this package available.
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                
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="AgileConfig.Client" Version="1.7.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AgileConfig.Client --version 1.7.4                
#r "nuget: AgileConfig.Client, 1.7.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.
// Install AgileConfig.Client as a Cake Addin
#addin nuget:?package=AgileConfig.Client&version=1.7.4

// Install AgileConfig.Client as a Cake Tool
#tool nuget:?package=AgileConfig.Client&version=1.7.4                

AgileConfig_Client

AgileConfig 的客户端,.net core standard2.0实现 。

Nuget Nuget Nuget

使用客户端

安装客户端

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段则判定存活,其它都视为失败 可选

服务的注册

当配置好客户端后,启动对应的应用程序,服务信息会自动注册到服务端并且开始心跳。如果服务正确注册到服务端,控制台的服务管理界面可以查看: alternate text is missing from this package README image

服务发现

现在服务已经注册上去了,那么怎么才能拿到注册中心所有的服务呢?同样非常简单,在程序内只要注入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 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. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (23)

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.7.5 230 12/29/2024
1.7.4 228 12/20/2024
1.7.3 16,373 3/21/2024
1.7.2 6,982 1/9/2024
1.7.1 11,637 10/6/2023
1.7.0 2,518 9/12/2023
1.6.19 8,904 7/16/2023
1.6.18 868 6/28/2023
1.6.17 4,902 5/28/2023
1.6.16 821 5/25/2023
1.6.15 1,375 5/2/2023
1.6.14 364 4/28/2023
1.6.13 3,303 4/12/2023
1.6.12 3,559 3/6/2023
1.6.11 51,122 12/27/2022
1.6.10 930 12/15/2022
1.6.9 25,747 9/17/2022
1.6.8 5,670 9/1/2022
1.6.2.2 28,396 6/4/2022 1.6.2.2 has at least one vulnerability with critical severity.
1.6.2.1 3,127 5/13/2022 1.6.2.1 has at least one vulnerability with critical severity.
1.6.2 768 5/12/2022 1.6.2 has at least one vulnerability with critical severity.
1.6.1 11,204 4/15/2022 1.6.1 has at least one vulnerability with critical severity.
1.6.0 1,070 4/5/2022 1.6.0 has at least one vulnerability with critical severity.
1.6.0-preview 530 3/30/2022 1.6.0-preview has at least one vulnerability with critical severity.
1.2.1.10 4,849 3/17/2022 1.2.1.10 has at least one vulnerability with critical severity.
1.2.1.9 2,873 2/9/2022 1.2.1.9 has at least one vulnerability with critical severity.
1.2.1.7 1,739 1/12/2022 1.2.1.7 has at least one vulnerability with critical severity.
1.2.1.5 5,422 12/7/2021 1.2.1.5 has at least one vulnerability with critical severity.
1.2.1.4 1,694 11/11/2021 1.2.1.4 has at least one vulnerability with critical severity.
1.2.1.3 25,454 10/25/2021 1.2.1.3 has at least one vulnerability with critical severity.
1.2.1 1,073 10/23/2021 1.2.1 has at least one vulnerability with critical severity.
1.1.9 4,737 9/22/2021 1.1.9 has at least one vulnerability with critical severity.
1.1.8.11 10,448 8/18/2021 1.1.8.11 has at least one vulnerability with critical severity.
1.1.8.10 865 8/17/2021 1.1.8.10 has at least one vulnerability with critical severity.
1.1.8.9 989 8/15/2021 1.1.8.9 has at least one vulnerability with critical severity.
1.1.8.8 10,033 7/14/2021 1.1.8.8 has at least one vulnerability with critical severity.
1.1.8.7 897 7/14/2021 1.1.8.7 has at least one vulnerability with critical severity.
1.1.8.6 1,624 7/6/2021 1.1.8.6 has at least one vulnerability with critical severity.
1.1.8.5 6,530 5/19/2021 1.1.8.5 has at least one vulnerability with critical severity.
1.1.8.4 1,262 4/23/2021 1.1.8.4 has at least one vulnerability with critical severity.
1.1.8.3 878 4/23/2021 1.1.8.3 has at least one vulnerability with critical severity.
1.1.8.2 887 4/22/2021 1.1.8.2 has at least one vulnerability with critical severity.
1.1.8.1 885 4/21/2021 1.1.8.1 has at least one vulnerability with critical severity.
1.1.8 927 4/20/2021 1.1.8 has at least one vulnerability with critical severity.
1.1.7 869 4/20/2021 1.1.7 has at least one vulnerability with critical severity.
1.1.6 4,024 4/18/2021 1.1.6 has at least one vulnerability with critical severity.
1.1.5 1,459 3/15/2021 1.1.5 has at least one vulnerability with critical severity.
1.1.4 982 3/11/2021 1.1.4 has at least one vulnerability with critical severity.
1.1.3 1,090 3/1/2021 1.1.3 has at least one vulnerability with critical severity.
1.1.2 1,411 1/3/2021 1.1.2 has at least one vulnerability with critical severity.
1.1.1 1,410 12/30/2020 1.1.1 has at least one vulnerability with critical severity.
1.1.0 2,311 10/8/2020 1.1.0 has at least one vulnerability with critical severity.
1.0.11 1,516 7/4/2020 1.0.11 has at least one vulnerability with critical severity.
1.0.10 1,046 6/19/2020 1.0.10 has at least one vulnerability with critical severity.
1.0.9 1,035 6/15/2020 1.0.9 has at least one vulnerability with critical severity.
1.0.8 1,062 6/8/2020 1.0.8 has at least one vulnerability with critical severity.